如何通过适当的文件而不是通过env变量或系统属性设置活动的spring 3.1环境配置文件

Fab*_*ian 49 environment spring profiles

我们使用spring 3.1的新环境配置文件功能.我们当前通过在部署应用程序的服务器上设置环境变量spring.profiles.active = xxxxx来设置活动配置文件.

我们认为这是一个次优的解决方案,因为我们要部署的war文件应该只有一个额外的属性文件,该文件设置spring应用程序上下文应该加载的环境,因此部署不依赖于服务器上设置的某些env var.

我试图弄清楚如何做到这一点并发现:

ConfigurableEnvironment.setActiveProfiles()

我可以用来以编程方式设置配置文件,但后来我仍然不知道在何时何地执行此代码.弹簧环境加载的地方?我可以从属性文件加载我想传递给方法的参数吗?

更新:我刚刚在docs上找到了我可以实现设置活动配置文件的内容?

Tom*_*icz 51

web.xml

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

运用 WebApplicationInitializer

当您web.xml在Servlet 3.0环境中没有文件并从Java完全引导Spring 时,将使用此方法:

class SpringInitializer extends WebApplicationInitializer {

    void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.getEnvironment().setActiveProfiles("profileName");
        rootContext.register(SpringConfiguration.class);
        container.addListener(new ContextLoaderListener(rootContext));
    }
}
Run Code Online (Sandbox Code Playgroud)

SpringConfigurationclass注释的类@Configuration.

  • -Dspring.profiles.active = someprofile我认为更易于管理.因为它在某些情况下不必打开并且将应用默认值. (2认同)
  • 您可能想要使用`spring.profiles.default`.这样,可以使用系统属性`-Dspring.profiles.active`覆盖活动的配置文件 (2认同)

Fab*_*ian 40

只要可以在web.xml中静态提供配置文件名称,或者使用新的无XML配置类型,Thomasz的答案就是有效的,其中可以通过编程方式加载要从属性文件设置的配置文件.

由于我们仍然使用我进一步调查的XML版本,并找到了以下很好的解决方案,您可以ApplicationContextInitializer在其中实现自己的解决方案,只需将新的PropertySource与属性文件添加到源列表中以搜索特定于环境的配置设置.在下面的示例中,可以spring.profiles.activeenv.properties文件中设置属性.

public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class);

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        try {
            environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
            LOG.info("env.properties loaded");
        } catch (IOException e) {
            // it's ok if the file is not there. we will just log that info.
            LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

然后,您需要将该初始化程序作为参数添加到ContextLoaderListenerspring中,如下所示web.xml:

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>somepackage.P13nApplicationContextInitializer</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

您也可以将其应用于DispatcherServlet:

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextInitializerClasses</param-name>
        <param-value>somepackage.P13nApplicationContextInitializer</param-value>
    </init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)


Mic*_*aev 6

出于某种原因,只有一种方式适合我

public class ActiveProfileConfiguration implements ServletContextListener {   
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "dev");
        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
    }
Run Code Online (Sandbox Code Playgroud)

....

 <listener>
     <listener-class>somepackahe.ActiveProfileConfiguration</listener-class>
 </listener>
 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
Run Code Online (Sandbox Code Playgroud)