繁体   English   中英

Spring 3.1 中的默认配置文件

[英]Default profile in Spring 3.1

在我的应用程序中,我使用@Profile("prod")@Profile("demo")注释了 bean。 第一个,正如您猜到的那样:),用于连接到生产数据库的 bean,第二个用于注释使用一些假数据库( HashMap或其他)的 bean - 以加快开发速度。

我想要的是默认配置文件( "prod" ),如果它没有被“ something-else ”覆盖,它将始终被使用。

完美的是在我的web.xml中:

<context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>prod</param-value>
</context-param>

然后用-Dspring.profiles.active="demo"覆盖它,这样我就可以这样做:

mvn jetty:run -Dspring.profiles.active="demo". 

但遗憾的是,这是行不通的。 知道我怎么能做到这一点? 在我的所有环境中设置-Dspring.profiles.active="prod"不是一个选项。

在 web.xml 中将您的生产环境定义为默认配置文件

<context-param>
   <param-name>spring.profiles.default</param-name>
   <param-value>prod</param-value>
</context-param>

如果你想使用不同的配置文件将其作为系统属性传递

mvn -Dspring.profiles.active="demo" jetty:run

我的经验是使用

@Profile("default")

如果没有识别出其他配置文件,bean 只会被添加到上下文中。 如果您传入不同的配置文件,例如-Dspring.profiles.active="demo" ,该配置文件将被忽略。

我有同样的问题,但我使用WebApplicationInitializer以编程方式配置 ServletContext (Servlet 3.0+)。 所以我做了以下事情:

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        // Create the 'root' Spring application context
        final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        // Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE'
        rootContext.getEnvironment().setDefaultProfiles("prod");
        rootContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        sc.addListener(new ContextLoaderListener(rootContext));
    }
}

您也可以考虑删除 PROD 配置文件,并使用 @Profile("!demo")

关于设置默认生产配置文件已经发布@andih

为 maven 码头插件设置默认配置文件的最简单方法是在插件配置中包含以下元素:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <configuration>
        <systemProperties>
            <systemProperty>
                <name>spring.profiles.active</name>
                <value>demo</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>

Spring 在确定哪些配置文件处于活动状态时提供两个单独的属性:

  • spring.profiles.active

  • spring.profiles.default

如果设置了spring.profiles.active ,则其值决定了哪些配置文件处于活动状态。 但是,如果未设置spring.profiles.active ,则 Spring 会查找spring.profiles.default.

如果spring.profiles.activespring.profiles.default都没有设置,那么就没有活动的配置文件,只有那些没有被定义为在配置文件中的 bean 被创建。任何没有指定配置文件的 bean 都属于default配置文件。

您可以将 web.xml 设置为过滤资源,并使用 maven 配置文件设置中的 maven 填充此值 - 这就是我们所做的。

在 pom 中过滤所有资源(如果你没有 ${} 标记,你可以这样做)

<webResources>
    <resource>
        <directory>src/main/webapp</directory>
        <filtering>true</filtering>
    </resource>
</webResources>

在 web.xml 放

<context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>${spring.prfile}</param-value>
</context-param>

在 pom 中创建 maven 个配置文件

<profiles>
    <profile>
        <id>DEFAULT</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile>prod</spring.profile>
        </properties>
    <profile>
    <profile>
        <id>DEMO</id>
        <properties>
            <spring.profile>demo</spring.profile>
        </properties>
    <profile>
</profiles>

现在你可以使用

mvn jetty:run -P DEMO

或者简单地使用任何 maven 命令的-P DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM