Spring configuration profile bean

@Profile annotation can be determined according to the configuration which bean is created for switching environment

@Configuration

@Profile("dev")

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

     public  DataSource   dataSource(){

          return .....;

     }

}

@Profile used in the class above, it will tell the Spring bean configuration class is created only when the dev profile activated. If the method is not activated, with @Bean comment will be ignored

In Spring3.1, you can only use @Profile annotation at the class level, in the 3.2 version, it can also be used at the method level, used in conjunction with @Bean notes, so how can the same class a configuration. Such as

@Configuration

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

      @Profile("dev")

     public  DataSource   devDataSource(){

          return .....;

     }

        @Bean(destroyMethod = "shutdown")

        @Profile("prod") 

   public  DataSource   proDataSource(){

          return .....;

     }

}

In a statement here, although more bean in a profile, but only if the profile is activated, the corresponding bean will be created. The bean does not declare profile will be created.

The XML configuration profile

As follows, xml configuration file for the dev environment

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
     profile = "dev">


    <jdbc:embended-database   id="dataSource">
      ....
    </jdbc:embedded-database>
</beans>

Below, the configuration corresponding to the corresponding environment bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"
     profile = "dev">


    <beans  profile="dev">   
     <jdbc:embended-database   id="dataSource">
          ....
        </jdbc:embedded-database>
    </beans>

   <beans  profile="qa">   
     <jdbc:embended-database   id="dataSource">
          ....
        </jdbc:embedded-database>
    </beans>
</beans>

Activating Profile

Spring in determining which profile is active, you need to rely spring.profiles.active and spring.profiles.default. If spring.profiles.active property, then its value will be used to determine the activation of the profile. If no spring.profiles.active, Spring will look for the value spring.profiles.default. If both are not set, then there is no want to activate the profile, and therefore will only create the bean is not defined @profile.

Set way these two properties:

1, as the initialization parameter of DispatcherServlet

Add the following code to the web.xml:

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
            <param-name>spring.profiles.default</param-name>
            <param-value>dev</param-value> //为servlet设置默认的profile
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    

2, as a Web application context

Increase in web.xml following code, set default profile for the context

<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>

3, as environment variables

4, as the JVM system property

5, based on the integration testing, the use of annotations is provided @ActiveProfiles

@Runwith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes={PersistenceTestConfig.class})

@ActiveProfiles("dev")

public   class   PersistenceTest{

......

}

 

2 in accordance with the above manner, the code acquired all developers are used to develop environment without any additional configuration. When the application is deployed to QA, production or other environment, people responsible for the deployment of the system based on the use of property, or the environment variable settings spring.profiles.active to JNDI, when after setting spring.profiles.active, as for spring.profiles. default settings into what was no longer valid, the system precedence profile spring.profiles.active set.

active and default profiles are using the plural form to show that you can activate multiple profile at the same time. But that would have little significance.

 

Guess you like

Origin blog.csdn.net/m0_37668842/article/details/82747702