Java Spring引导外部化属性不起作用
我已经看了下面的线程,并遵循其中给出的内容。但我的属性覆盖没有发生 我在Tomcat 8.0.33和Spring boot starter web上,并在我的setenv.sh中得到了这个Java Spring引导外部化属性不起作用,java,spring,tomcat,properties,spring-boot,Java,Spring,Tomcat,Properties,Spring Boot,我已经看了下面的线程,并遵循其中给出的内容。但我的属性覆盖没有发生 我在Tomcat 8.0.33和Spring boot starter web上,并在我的setenv.sh中得到了这个 export JAVA_OPTS="$JAVA_OPTS -Dlog.level=INFO -Dspring.config.location=file:/opt/jboss/apache-tomcat-8.0.33/overrides/ -Dspring.profiles.active=dev" 在o
export JAVA_OPTS="$JAVA_OPTS -Dlog.level=INFO -Dspring.config.location=file:/opt/jboss/apache-tomcat-8.0.33/overrides/ -Dspring.profiles.active=dev"
在overrides文件夹中,我得到了2个文件
1) application.properties
2) application-dev.properties
application.properties中只有一个条目
spring.profiles.active=dev
我看到正确的log.level被输入到我的代码中,这意味着这个命令正在工作。只是我不知道为什么我的超控没有像预期的那样发生
我的工作区中没有任何` PropertyPlaceHolderConfiguration'代码。我甚至不确定是否需要1我不使用此方法来外部化属性。首先,我将尝试对您的方法提出建议,然后向您展示我正在使用的方法 建议您的方法使用file:///而不是file://与Spring一样,我发现在冒号后没有传递三个斜杠时,它无法识别属性 我已经为您创建了一个示例项目 现在是我使用的方法 我为每个概要文件定义了一个配置文件,并将application.properties文件保存在src/main/resources下 然后在每个配置文件上使用@Profile和@PropertySource注释 例如:
@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {
@Bean
public EmailService emailService() {
return new MockEmailService();
}
@Bean
public ServletRegistrationBean h2ConsoleServletRegistration() {
ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
bean.addUrlMappings("/console/*");
return bean;
}
}
及
我还获得了一个对所有配置文件都有效的配置文件,我称之为ApplicationConfig,如下所示:
@Configuration
@EnableJpaRepositories(basePackages = "com.devopsbuddy.backend.persistence.repositories")
@EntityScan(basePackages = "com.devopsbuddy.backend.persistence.domain.backend")
@EnableTransactionManagement
@PropertySource("file:///${user.home}/.devopsbuddy/application-common.properties")
public class ApplicationConfig {
}
我的src/main/resources/application.properties文件如下所示:
spring.profiles.active=dev
default.to.address=me@example.com
token.expiration.length.minutes=120
当然,我可以通过将spring.profile.active属性作为系统属性传递来将其外部化,但就我的情况而言,目前还可以
在运行应用程序时,如果我传递“dev”概要文件,Spring将加载DevelopmentConfig类中定义的所有属性和bean以及ApplicationConfig中的所有属性和bean。如果我传递“prod”,则将加载ProductionConfig和ApplicationConfig属性
我正在完成一门关于如何创建一个具有安全性、电子邮件、数据JPA、Amazon Web服务、Stripe等功能的Spring Boot网站的课程。如果您愿意,您可以注册您的兴趣,课程开放注册时会收到通知 我不使用此方法将属性外部化。首先,我将尝试对您的方法提出建议,然后向您展示我正在使用的方法 建议您的方法使用file:///而不是file://与Spring一样,我发现在冒号后没有传递三个斜杠时,它无法识别属性 我已经为您创建了一个示例项目 现在是我使用的方法 我为每个概要文件定义了一个配置文件,并将application.properties文件保存在src/main/resources下 然后在每个配置文件上使用@Profile和@PropertySource注释 例如:
@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/.devopsbuddy/application-dev.properties")
public class DevelopmentConfig {
@Bean
public EmailService emailService() {
return new MockEmailService();
}
@Bean
public ServletRegistrationBean h2ConsoleServletRegistration() {
ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
bean.addUrlMappings("/console/*");
return bean;
}
}
及
我还获得了一个对所有配置文件都有效的配置文件,我称之为ApplicationConfig,如下所示:
@Configuration
@EnableJpaRepositories(basePackages = "com.devopsbuddy.backend.persistence.repositories")
@EntityScan(basePackages = "com.devopsbuddy.backend.persistence.domain.backend")
@EnableTransactionManagement
@PropertySource("file:///${user.home}/.devopsbuddy/application-common.properties")
public class ApplicationConfig {
}
我的src/main/resources/application.properties文件如下所示:
spring.profiles.active=dev
default.to.address=me@example.com
token.expiration.length.minutes=120
当然,我可以通过将spring.profile.active属性作为系统属性传递来将其外部化,但就我的情况而言,目前还可以
在运行应用程序时,如果我传递“dev”概要文件,Spring将加载DevelopmentConfig类中定义的所有属性和bean以及ApplicationConfig中的所有属性和bean。如果我传递“prod”,则将加载ProductionConfig和ApplicationConfig属性
我正在完成一门关于如何创建一个具有安全性、电子邮件、数据JPA、Amazon Web服务、Stripe等功能的Spring Boot网站的课程。如果您愿意,您可以注册您的兴趣,课程开放注册时会收到通知 Hi Shiv,我已经更新了我的答案,并提供了一个链接,链接到一个示例项目,演示了上述内容。供您参考,您可以在这里找到示例项目。多谢各位@马可。你的榜样很有魅力。我爱你。我把你的答案标为接受。我这里有一个新问题-。如果可能的话,请提供帮助Hi Shiv,我已经用一个示例项目的链接更新了我的答案,该项目演示了上述内容。供您参考,您可以在这里找到示例项目。多谢各位@马可。你的榜样很有魅力。我爱你。我把你的答案标为接受。我这里有一个新问题-。如果可能的话,请帮忙