tl;dr:如何基于基于注释的配置类创建 Spring 上下文,同时提供活动配置文件?
我正在尝试使用带有使用注释指定的配置的类来创建 Spring 上下文。
org.springframework.context.ApplicationContext context =
new org.springframework.context.annotation.AnnotationConfigApplicationContext( com.initech.ReleaserConfig.class );
然而,当它启动时,它会崩溃,因为它找不到所需的 bean,但该 bean 确实存在:尽管仅在某个配置文件下"myProfile"
.
如何指定活动配置文件?我有一种感觉我需要使用一个org.springframework.context.annotation.AnnotationConfigApplicationContext(org.springframework.beans.factory.support.DefaultListableBeanFactory)
构造函数,但我不确定哪个是合适的。
PS-我没有使用 Spring Boot,而是使用 Spring 4.2.4.RELEASE。
这应该可以解决问题...
final AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
appContext.getEnvironment().setActiveProfiles( "myProfile" );
appContext.register( com.initech.ReleaserConfig.class );
appContext.refresh();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)