如何在弹簧中通过注释设置活动轮廓?
例如:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles( profiles = {ApplicationProfiles.TEST} )
public class CacheManagerTest {
...
}
Run Code Online (Sandbox Code Playgroud)
对于JUnit测试,这种方法很完美,但是如何初始化生产应用程序上下文呢?(我没有任何主要方法/сlasses)
Jon*_*han 12
您可以使用spring.profiles.active属性在运行时传入活动的配置文件:
-Dspring.profiles.active="profile1,profile2"
Run Code Online (Sandbox Code Playgroud)
有关配置文件的介绍,请参阅SpringSource博客文章.
根据Spring博客的说法,如果您正在使用独立的应用程序或Web应用程序,则可以通过这些方式传递活动配置文件
在Web应用程序中激活
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)
使用手动创建的上下文激活
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.load("classpath:/com/bank/config/xml/*-config.xml");
ctx.refresh();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18324 次 |
| 最近记录: |