1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.seasar.cubby.plugins.spring.spi; |
18 | |
|
19 | |
import org.seasar.cubby.spi.ContainerProvider; |
20 | |
import org.seasar.cubby.spi.container.Container; |
21 | |
import org.seasar.cubby.spi.container.LookupException; |
22 | |
import org.springframework.beans.factory.BeanFactoryUtils; |
23 | |
import org.springframework.context.ApplicationContext; |
24 | |
import org.springframework.context.ApplicationContextAware; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 6 | public class SpringContainerProvider implements ContainerProvider, |
33 | |
ApplicationContextAware { |
34 | |
|
35 | |
|
36 | |
private Container container; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public Container getContainer() { |
42 | 1 | return container; |
43 | |
} |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public void setApplicationContext( |
52 | |
final ApplicationContext applicationContext) { |
53 | 6 | this.container = new SpringContainerImpl(applicationContext); |
54 | 6 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 6 | private static class SpringContainerImpl implements Container { |
60 | |
|
61 | |
private final ApplicationContext applicationContext; |
62 | |
|
63 | 6 | public SpringContainerImpl(final ApplicationContext applicationContext) { |
64 | 6 | this.applicationContext = applicationContext; |
65 | 6 | } |
66 | |
|
67 | |
public <T> T lookup(final Class<T> type) throws LookupException { |
68 | 1 | String[] names = BeanFactoryUtils |
69 | |
.beanNamesForTypeIncludingAncestors(applicationContext, |
70 | |
type); |
71 | 1 | if (names == null || names.length < 1) { |
72 | 0 | throw new LookupException(type + " component not found."); |
73 | |
} |
74 | 1 | return type.cast(applicationContext.getBean(names[0], type)); |
75 | |
} |
76 | |
|
77 | |
} |
78 | |
|
79 | |
} |