Java Spring 3.1中的默认配置文件
在我的应用程序中,我用Java Spring 3.1中的默认配置文件,java,spring,profiles,Java,Spring,Profiles,在我的应用程序中,我用@Profile(“prod”)和@Profile(“demo”)注释了bean。 正如您所猜测的:),第一个用于连接到生产数据库的bean,第二个用于注释使用一些伪数据库(HashMap或其他任何东西)的bean,以加快开发速度 我想要的是默认配置文件(“prod”),如果它没有被“其他东西”覆盖,则将始终使用该配置文件 在我的web.xml中最好有: <context-param> <param-name>spring.profiles
@Profile(“prod”)
和@Profile(“demo”)
注释了bean。
正如您所猜测的:),第一个用于连接到生产数据库的bean,第二个用于注释使用一些伪数据库(HashMap
或其他任何东西)的bean,以加快开发速度
我想要的是默认配置文件(“prod”
),如果它没有被“其他东西”覆盖,则将始终使用该配置文件
在我的web.xml中最好有:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
但可悲的是,这是行不通的。你知道我怎么能做到吗?在“我的所有环境”上设置-Dspring.profiles.active=“prod”
不是一个选项。在web.xml中将生产环境定义为默认配置文件
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</context-param>
您可以将web.xml设置为过滤资源,并让maven从maven概要文件设置中填充此值—这就是我们所做的
在pom中筛选所有资源(如果没有${}标记,则可以执行taht)
或者简单地用任何maven命令-pdemo
我的经验是
@Profile("default")
只有在没有识别出其他概要文件的情况下,bean才会被添加到上下文中。如果您传入不同的配置文件,例如-Dspring.profiles.active=“demo”
,此配置文件将被忽略。关于设置默认生产配置文件已发布@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>
org.eclipse.jetty
jetty maven插件
spring.profiles.active
演示
< /代码> 您也可以考虑删除PROD配置文件,使用@配置文件(“.demo”)< p>我有相同的问题,但我使用程序来配置Servlet上下文(servlet 3 +)。因此,我做了以下工作:
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));
}
}
在确定哪些配置文件处于活动状态时,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都属于default
profile。否他试图在web.xml中定义活动配置文件并将其定义为系统属性。在我的解决方案中,我在web.xml中配置默认配置文件,并通过系统属性覆盖/定义活动配置文件。如果没有明确的活动配置文件,将使用默认配置文件。谢谢!这正是我想要的!在任何地方都找不到它:/n这种方法有一个问题:如果在application.properties
中设置spring.profiles.default=prod
,则不会加载application-prod.properties
。这是违反直觉的。@gamliela该方法不会在application.properties
文件中设置默认配置文件。为了知道应该使用application-prod.properties
,您必须了解配置文件。这就是这种方法的作用。它在spring上下文之外定义概要文件,可以是web.xml
(默认值),也可以是通过环境变量(覆盖默认值)。@是的,我知道,但我只是说它不直观,因此有问题。由于application default.properties
被加载,我还希望application newdefault.properties
也会被加载。这不是你的解决方案的问题,这是Spring的问题…我不确定,但我认为这不起作用。IMHO jetty:run不会在过滤资源的阶段运行。当然,您需要运行mvn clean compile jetty:run-P DEMO,但它使用未编译的代码自动运行。我知道Spring 3.1概要文件的主要目标之一是生成一个WAR文件,以便在所有环境中部署。使用Maven配置文件是向以前状态的一步:每个环境都需要打包WAR文件…@edrab他要求mvn jetty:run-没有WAR文件。同意@Hurda。但他也要求在多个环境中运行该命令:混合使用Maven配置文件和Spring配置文件可能会有点误导……公认的答案取决于web.xml(这很好),但是无论您是否有web.xml,这个答案都是有效的,因此对每个人都更广泛地有用cleaner@rustyx这在AbstractEnvironment API中进行了解释:Spring Boot在本节结束时也提到了这种行为:我想如果您有两个以上的概要文件,这将不起作用,对吗?
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${spring.prfile}</param-value>
</context-param>
<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
@Profile("default")
<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>
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));
}
}