Yogen Rai
Yogen Rai

Reputation: 3031

Spring Profile values getting mixed

I'm using Spring Boot 1.4.3.RELEASE and gradle 2.13 for developing an api. On local environment, 'local' profile is working perfectly, but I'm getting issue while deploying code in Linux/Unix server.

Here is my application.yml with two profiles : dev and local

---

spring:
  profiles:
    active: dev

server:
  context-path: /api/v1
  port: 8080

build:
  version: 1.0.0

cache:
  storage:
    path: '/home/user/content/storage/'


---

spring:
  profiles:
    active: local

server:
  context-path: /content-delivery/api/v1
  port: 8081

build:
  version: 1.0.0

cache:
  storage:
    path: '/Yogen/api/code/cachedData'

The command I'm using to deploy my war file is:

jdk1.8.0_112/bin/java -jar _-Dspring.profiles.active=dev content-delivery.jar

When I run my war it was working fine with only one profile, but once i added another profile, I'm getting values get mixed up causing error as below:

    01:56:16.557 INFO  ContentDeliveryApplication - The following profiles are active: dev
............
02:00:48.182 INFO  PropertyPlaceholderConfigurer - Loading properties file from file [/home/542596/content-api/resources/app-dev.properties]
    02:00:51.939 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [class org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$290c5e2d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:00:55.736 INFO  AnnotationActionEndpointMapping - Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
    02:01:25.559 INFO  PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a08e9c2b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    02:01:56.522 INFO  TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8081 (http)
    02:01:57.669 INFO  StandardService - Starting service Tomcat
    02:01:57.877 INFO  StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.6
    02:02:11.228 INFO  [/content-delivery/api/v1] - Initializing Spring embedded WebApplicationContext
    02:02:11.228 INFO  ContextLoader - Root WebApplicationContext: initialization completed in 353263 ms
    02:02:19.412 INFO  ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
    02:02:19.517 INFO  ServletRegistrationBean - Mapping servlet: 'messageDispatcherServlet' to [/services/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
    02:02:19.829 INFO  FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
    02:02:46.283 ERROR EhcacheManager - Initialize failed.
    02:02:46.284 WARN  AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
    02:02:46.285 INFO  StandardService - Stopping service Tomcat
    02:02:47.528 INFO  AutoConfigurationReportLoggingInitializer -

    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    02:02:48.103 ERROR SpringApplication - Application startup failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jsonObjectCacheManager': Invocation of init method failed; nested exception is org.ehcache.StateTransitionException: Directory couldn't be created: /Yogen/api/code/cachedData/cachedData
            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

The PropertyPlaceholderConfigurer has been configured as follows:

@Configuration
public class PropertyConfiguration {

    @Bean
    @Profile("dev")
    public static PropertyPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        configurer.setLocation(new FileSystemResource("/home/542596/content-api/resources/app-dev.properties"));
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
}
}

The console shows that properties file being read is correct also the profile is 'dev', but the port started and context-path and other values are being fetched from 'local' profile instead 'dev' profile.

What I'm missing?? Thanks.

Upvotes: 0

Views: 851

Answers (3)

Barath
Barath

Reputation: 5283

We also have another option similar to @Maxwell answer instead of passing the profile through VM property :

you can have following files:

application.yml 
application-dev.yml
application-local.yml

And define profile in application.yml :

spring:
    profiles:
       active : dev 

This ensures loading of application.yml as well as application-dev.yml.

Upvotes: 0

lane.maxwell
lane.maxwell

Reputation: 5903

Like @Barath stated, you cannot separate profile properties within a single application.yml file. Create a yml file for each profile, name it as follows

application-{profile}.yml

I would not use bootstrap.yml to specify which profile to load as you'll want to externalize this if possible, so passing it to the VM via -Dspring.profiles.active=dev would be preferable.

Upvotes: 0

Barath
Barath

Reputation: 5283

Your profiles are getting overridden.

Better Boostrap your application using bootstrap.yml

and have two application.yml as follows :

application-dev.yml
application-local.yml

set profiles in bootstrap.yml to decide which property file to load:

    spring:
         profiles:
             active : dev # choose which application properties to load dev/local

Upvotes: 1

Related Questions