springboot Actions to get profile

  • 2021-11-10 09:43:48
  • OfStack

Directory springboot Obtain profile Obtain profile Obtain Profilespring profile Basic Use Spring profile is very simple to use in our system What is our problem?

springboot Get profile

Get profile through code


@Component
public class ProfileUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.context = applicationContext;
}
public static String getActiveProfile(){
    String[] profiles = context.getEnvironment().getActiveProfiles();
    if(profiles.length != 0){
        return profiles[0];
    }
    return "";
}
}

Get Profile by annotation


@Profile("dev","test")
// The following configuration information is only available in the dev Environment and test The environment will take effect 
@Service

spring Basic Use of profile

Spring profile is a concept introduced by Spring 3. When developing a general system, we prefer to use profile in Maven to distinguish different environment configuration files. profile1 of Spring has not been used much. Recently, a colleague used Spring profile in the system development process of a company. However, there was trouble setting up default profile. After studying one case for a long time, the problem was discovered.

Spring profile is very simple to use in our system

The features of runtime are not used, but beans in different profile environments are defined in xml


<!-- other beans -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
    .......
    <!-- production Environment  -->
    <beans profile="production">
        <context:property-placeholder ignore-resource-not-found="true"
            location="classpath*:/application.properties" />    
    ....
    </beans>
    <!-- local Environment  -->
    <beans profile="local">
        <context:property-placeholder ignore-resource-not-found="true"
            location="classpath*:/application.properties
            ,classpath*:/application.development.properties"/>  
    ....
    </beans>

Next, we can choose which environment profile to use in web. xml and properties, for example


    <!-- web.xml -->
    <context-param>
            <param-name>spring.profiles.default</param-name>
            <param-value>production</param-value>
    </context-param>
    <!-- properties Select  -->
    System.setProperty("spring.profiles.active", "development");

What is our problem?

Because we want to choose the default profile by jndi instead of jvm parameter, first of all, we know that the settings of spring and profile cannot be in properties file because of the loading order of spring. We don't want to change the startup parameters of the system, so we chose the way of jndi to read the default startup of profile. The key comes. When configuring jndi, we made the following settings


 <Environment name="spring.profiles.active" type="java.lang.String" value="local">

The sad discovery didn't work, and there was no useful hint in log of spring. Before getting log related to profile, I hung up because I couldn't read bean. I had to remove the xml file related to profile and restart the system once, only to find that the name of jndi read by spring by default is spring. profiles. default instead of spring. profiles. active.


Related articles: