Spring Profile Basic Usage

Source: Internet
Author: User

The spring profile is a concept introduced in Spring 3, which prefers to use the profile in Maven to differentiate between different environmental profiles. Spring's profile has not been used very much, and recently colleagues used spring profiles during a company's system development. But there was trouble setting up the default profile. After studying for a long time, we found the problem.

The use of Spring profile in our system is very simple and does not use the runtime feature, but defines the beans in different profile environments 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>

Then we can generally choose what environment profile to use in Web.xml, in properties, for example

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

Where are our problems? We want to choose the default profile by Jndi instead of JVM parameters, first we know that the settings for spring profile are not in the properties file because of the order in which spring is loaded. We do not want to change the system's startup parameters, so we chose the jndi way to read the default boot of profile, the key is here, we made the following settings when configuring Jndi

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

The sad discovery didn't work, and spring's log didn't have any useful hints, not yet to get profile-related log because the bean was not read. I had to get rid of profile. Related XML file restart system only to discover spring default read Jndi name is spring.profiles.default instead of spring.profiles.active .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.