简体   繁体   中英

how to read active profile from different location in spring boot?

I have my application.properties file in src/main/resources and i have my different active profile (application-dev.properties and application-prod.properties) in differenent directory as src/main/resources/properties.

I'm using springboot version 2.7.5

I tried to configure it in application.properties file which is in directory-src/main/resources spring.profiles.active=dev

but it is not reading the application-dev.properties from directory src/main/resources/properties

For fixing this issue i created two class

@Configuration
public class ActiveProfileConfiguration extends AbstractSecurityWebApplicationInitializer{

    private static final Logger log = LoggerFactory.getLogger(ApplicationJPAConfiguration.class);
    private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
    String profile;
        protected void setSpringProfile(ServletContext servletContext) {
    if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
        profile=System.getenv(SPRING_PROFILES_ACTIVE);
    }else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
        profile=System.getProperty(SPRING_PROFILES_ACTIVE);
    }else{
        profile="local";
    }
    log.info("***** Profile configured  is  ****** "+ profile);

    servletContext.setInitParameter("spring.profiles.active", profile);
    }
    
}

and

@Configuration
@Profile("local")
public class DevPropertyReader {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[] { new ClassPathResource("application.properties"), new ClassPathResource("EnvironmentProfiles/application-local.properties") };
    ppc.setLocations(resources);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    System.out.println("env active profile");
    return ppc;
    }
    
}

But still not able to resolve this issue.

Spring will not look for property files files in your custom /properties folder:

21.2 Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  • A /config subdir of the current directory.
  • The current directory
  • A config package
  • The classpath root

https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

The simplest solution would be to move it at the root of src/main/resources folder.

Place.properties in structure like below:

src/
├─ main/
│  ├─ resources/
│  │  ├─ application.properties
│  │  ├─ application-dev.properties
│  │  ├─ application-prod.properties

Below Dockerfile example sets the right Spring Profile when running your application:

ENTRYPOINT java -Dspring.profiles.active=prod -jar /app.jar

But there are many other options through WebApplicationInitializer, Beans XML, Maven profile, etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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