[英]Spring Java Config with XML config
我試圖將Spring java config與基於XML的配置一起使用,但是從未創建使用spring profile特性的bean。 您能否提出可能是根本原因的建議。 我的應用程序使用Spring 3.2。
我的Java配置類-
@Configuration
@ComponentScan({ "com.aexp.mars" })
@Profile("dev")
@PropertySource("classpath:messages.properties" )
@ImportResource("classpath:/spring/spring-profiles.xml")
public class SpringConfigDev{
private DriverManagerDataSource dataSource;
Spring XML配置-
<beans profile="dev">
<bean id="marsURLConfig" class="com.aexp.mars.common.utils.MarsURLConfig">
<property name="solrBaseURL" value="http://localhost:8080/solr"/>
</bean>
</beans>
使用主要方法中的以下代碼激活開發配置文件
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String env = "dev";
context.getEnvironment().setActiveProfiles(env);
context.register(SpringConfigDev.class);
context.refresh();
我的一個Spring組件之一使用具有MarsURLConfig類型的自動裝配,並且在下面得到了異常。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.aexp.mars.common.utils.MarsURLConfig] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
如果我從xml中刪除profile =“ dev”,則該應用程序可以正常工作
這似乎是基於Spring 3.2 Java的配置方法的已知限制。 當我們使用Java config時,Spring 3.2不支持Bean級配置文件的創建,並且由於我試圖從基於Java的配置類中加載XML,所以它對我沒有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.