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