Spring MVC –如何设置活动配置文件

在此示例中,我们将向您展示如何在Spring MVC Web应用程序中设置活动的@Profile

@Profile Examples
@Configuration
public class AppConfig {
 
	@Profile("dev")
	@Bean
	public CacheManager cacheManager() {
		//...
	}
 
	@Profile("live")
	@Bean
	public EhCacheManagerFactoryBean ehCacheCacheManager() {
		//...
	}
 
	@Profile("testdb")
	@Bean
	public DataSource dataSource() {
		//...
	}

}

要在Spring中设置活动的@Profile ,请通过spring.profiles.active系统属性定义一个值。

1. web.xml

对于普通的Web应用程序,其中包含web.xml文件。

1.1设置一个活动的配置文件。

web.xml
<context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>live</param-value>
	</context-param>

1.2设置多个活动配置文件。

web.xml
<context-param>
	    <param-name>spring.profiles.active</param-name>
	    <param-value>dev, testdb</param-value>
	</context-param>

2. Servlet 3.0+容器

对于Web应用程序没有web.xml文件,请使用以下方法之一:

2.1覆盖onStartup

MyWebInitializer.java
package com.mkyong.servlet3;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class MyWebInitializer extends
	AbstractAnnotationConfigDispatcherServletInitializer {

	//...

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		super.onStartup(servletContext);
		servletContext.setInitParameter("spring.profiles.active", "live");

		//Set multiple active profile
		//servletContext.setInitParameter("spring.profiles.active", "dev, testdb");
	}
	
}

2.2取决于要加载@Profile bean的上下文(根或servlet)。

MyWebInitializer.java
package com.mkyong.servlet3;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebInitializer extends
	AbstractAnnotationConfigDispatcherServletInitializer {

	//If the @Profile beans are loaded via root context
	@Override
	protected WebApplicationContext createRootApplicationContext() {
		
		WebApplicationContext context = 
                     (WebApplicationContext)super.createRootApplicationContext();
	    	((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("live");

		//Set multiple active profiles
		//((ConfigurableEnvironment)context.getEnvironment())
                //          .setActiveProfiles(new String[]{"live", "testdb"});

	    	return context;
	    
	}
	
	//If the @Profile beans are loaded via servlet context
	/*
	@Override
	protected WebApplicationContext createServletApplicationContext() {
		
		WebApplicationContext context = 
                     (WebApplicationContext)super.createServletApplicationContext();
	    	((ConfigurableEnvironment)context.getEnvironment()).setActiveProfiles("dev");
	    	return context;
	    
	}*/

}

参考文献

  1. Spring @Profile示例

翻译自: https://mkyong.com/spring-mvc/spring-mvc-how-to-set-active-profile/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值