Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - Spring Boot Externalizing properties not working

I have looked at the below threads and followed things given there. Still my property override is not happening

  1. Spring Boot - Externalized properties
  2. Profile Specific Property Enablement
  3. Spring Boot External Config

I am on Tomcat 8.0.33 and Spring boot starter web and got this in my 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"

And in the overrides folder I got 2 files

1) application.properties 2) application-dev.properties

The application.properties has a single entry in it

spring.profiles.active=dev

I see that the proper log.level is fed to my code which means this command is working. Its just that I am clueless as to why my override is not happening as expected

I don't have any `PropertyPlaceholderConfigurer code in my workspace. I am not even sure if I need 1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't use this method to externalise properties. First, I'll try a suggestion for your method and then I'll show you what I'm using.

The suggestion for your method is to use file:/// instead of file:/ as with Spring I found that when not passing the three slashes after the colon it didn't recognise the property.

I've created a sample project for you, available here with instructions.

Now for the method I use.

I define a Configuration file for each profile and I keep the application.properties file under src/main/resources.

Then I use the @Profile and @PropertySource annotations on each configuration file.

For example:

@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;
}
}

And

@Configuration
@Profile("prod")
@PropertySource("file:///${user.home}/.devopsbuddy/application-prod.properties")
public class ProductionConfig {

@Bean
public EmailService emailService() {
    return new SmtpEmailService();
}
}

I have also got a Configuration file that is valid for all profiles, which I call ApplicationConfig, as follows:

@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 {
}

My src/main/resources/application.properties file looks like the following:

spring.profiles.active=dev
default.to.address=me@example.com
token.expiration.length.minutes=120

Of course I could externalise the spring.profile.active property by passing it as a system property but for my case and for now it's fine.

When running the application, if I pass the "dev" profile, Spring will load all properties and Beans defined in the DevelopmentConfig class plus all those in ApplicationConfig. If I pass "prod", the ProductionConfig and ApplicationConfig properties will be loaded instead.

I'm completing a course on how to create a Spring Boot website with Security, Email, Data JPA, Amazon Web Services, Stripe and much more. If you want, you can register your interest here and you will get notified when the course is open for enrolment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...