0
我想使用基於XML的配置的Spring java配置,但使用彈簧配置文件功能的bean永遠不會被創建。你能提出什麼可能是根本原因嗎? Spring 3.2被我的應用程序使用。Spring XML配置與XML配置
我的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 config-
<beans profile="dev">
<bean id="marsURLConfig" class="com.aexp.mars.common.utils.MarsURLConfig">
<property name="solrBaseURL" value="http://localhost:8080/solr"/>
</bean>
</beans>
dev的配置文件使用的主要方法
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String env = "dev";
context.getEnvironment().setActiveProfiles(env);
context.register(SpringConfigDev.class);
context.refresh();
內部下面的代碼激活
我的一個Spring組件使用Autowiring和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配置文件中刪除=「開發」,應用正常工作
你是否正在運行'dev'配置文件激活? –
是的,你可以看到我已經使用了註解@Profile(「dev」) – Raj
不,這不會激活配置文件。請閱讀相應的javadoc。 –