如何引用context.xml中创建的bean?

时间:2016-07-11 17:08:05

标签: java spring maven

在我的项目中,有一个主要的maven模块可以将所有内容保存在1 context.xml

我有一个单独的maven模块,我们尝试使用注释(@Component@Autowired),而不是将所有内容填充到1 context.xml中。

在我创建的课程中

@Component
public class ActiveDirectoryConfigurationStore implements ConfigurationStore<ActiveDirectoryConfiguration> {
 ....
}

java中的这个bean看起来像

public class CacheFactory implements FactoryBean<Cache>, DisposableBean {
...
}

我想引用在这个大context.xml

中创建的bean
<bean id="usersCacheFactory" class="cache.impl.CacheFactory">
        <constructor-arg index="0" value="users"/>
        <constructor-arg index="1" value="db"/>
    </bean>

如何在usersCacheFactory课程中使用@Autowired注释引用此bean ActiveDirectoryConfigurationStore? 请注意,ActiveDirectoryConfigurationStore与此bean位于不同的模块中

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用@ImportResource?例如。如下

@Component
@ImportResource(value = { "classpath:/com/yourcompany/othermodule/context.xml" })
public class ActiveDirectoryConfigurationStore implements ConfigurationStore<ActiveDirectoryConfiguration> {

 @Autowired
 @Qualifier(value="usersCacheFactory")
 CacheFactory usersCacheFactory;

 ....
}