【问题标题】:Default profile in Spring 3.1Spring 3.1 中的默认配置文件
【发布时间】:2012-04-19 22:46:39
【问题描述】:

在我的应用程序中,我有用 @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" 不是一种选择。

【问题讨论】:

    标签: java spring profiles


    【解决方案1】:

    在 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
    

    【讨论】:

    • 不,他试图在 web.xml 中定义 active 配置文件并作为系统属性。在我的解决方案中,我在 web.xml 中配置了一个 default 配置文件,并通过系统属性覆盖/定义了 active 配置文件。如果没有明确的 active 配置文件,则将使用默认配置。
    • 谢谢!这正是我想要的!在任何地方都找不到它:/
    • 这种方法的一个问题:如果你在application.properties中设置了spring.profiles.default=prod,那么application-prod.properties将不会被加载。这是违反直觉的。
    • @gamliela 该方法不会在application.properties 文件中设置默认配置文件。为了知道应该使用application-prod.properties,您必须知道配置文件。这就是这种方法的作用。它在 spring 上下文之外定义配置文件web.xml(默认)或通过环境变量(覆盖默认)。
    • @andih 是的,我知道,但我只是说它不直观,因此有问题。由于application-default.properties 被加载,我也希望application-newdefault.properties 也会被加载。这不是您的解决方案的问题,而是 Spring 的问题......
    【解决方案2】:

    我的经验是使用

    @Profile("default")
    

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

    【讨论】:

    【解决方案3】:

    我有同样的问题,但我使用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));
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 我想如果您有两个以上的个人资料,这将不起作用,对吧?
      【解决方案5】:

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

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

      <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>
      

      【讨论】:

        【解决方案6】:

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

        • spring.profiles.active

        • spring.profiles.default

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

        如果既没有设置spring.profiles.active 也没有设置spring.profiles.default,则没有活动配置文件,并且只创建那些未定义为在配置文件中的bean。任何未指定配置文件的bean 都属于@ 987654328@个人资料。

        【讨论】:

          【解决方案7】:

          您可以将您的 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
          

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

          【讨论】:

          • 我不确定,但我认为这行不通。恕我直言 jetty:run 不会运行过滤资源的阶段。
          • 因为你需要运行 mvn clean compile jetty:run -P DEMO,但是使用未编译的代码它会自动运行它
          • 我了解 Spring 3.1 Profiles 的主要目标之一是生成一个可以部署在所有环境中的 WAR 文件。使用 Maven 配置文件是回到之前的状态:每个环境都需要打包 WAR 文件...
          • @edrabc 他要求 mvn jetty:run - 没有 WAR 文件。
          • 同意@Hurda。但他也要求在多个环境中运行该命令:混合使用 Maven 配置文件和 Spring 配置文件可能会有点误导......
          猜你喜欢
          • 2016-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-08
          • 1970-01-01
          • 1970-01-01
          • 2021-02-01
          • 1970-01-01
          相关资源
          最近更新 更多