Spring 配置profile bean

@Profile 注解可以根据配置来决定创建哪个bean,用来切换环境

@Configuration

@Profile("dev")

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

     public  DataSource   dataSource(){

          return .....;

     }

}

@Profile用在类上面,它会告诉Spring这个配置类中的bean只有在dev profile激活时才会创建。如果没有激活,则带有@Bean注解的方法会被忽略

在Spring3.1中,只能将@Profile注解用在类级别上,在3.2版本开始,也可以将它用在方法级别上,与@Bean注解一同使用,这样就能在同一个类中有多个配置。如

@Configuration

publi  class  DevelopmentProfileConfig{

      @Bean(destroyMethod = "shutdown")

      @Profile("dev")

     public  DataSource   devDataSource(){

          return .....;

     }

        @Bean(destroyMethod = "shutdown")

        @Profile("prod") 

   public  DataSource   proDataSource(){

          return .....;

     }

}

这里虽然多个bean声明在一个profile中,但是只有当profile激活时,相应的bean才会被创建。没有声明profile的bean都会被创建。

XML中配置profile

如下,配置xml文件为dev环境

<?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>

如下,配置对应的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>

激活Profile

Spring在确定那个profile处于激活状态时,需要依赖spring.profiles.active 和 spring.profiles.default。如果设置spring.profiles.active属性的话,那么它的值就会被用来确定激活那个profile.如果没有设置spring.profiles.active,Spring将会查找spring.profiles.default的值。如果两个都没有设置,那么就没有要激活的profile,因此只会创建没有定义@profile的bean。

设置这两个属性的方式:

1、作为DispatcherServlet的初始化参数

在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、作为Web应用的上下文

在web.xml中增加以下代码,为上下文设置默认的profile

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

3、作为环境变量

4、作为JVM的系统属性

5、在集成测试类上,使用@ActiveProfiles注解设置

@Runwith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes={PersistenceTestConfig.class})

@ActiveProfiles("dev")

public   class   PersistenceTest{

......

}

按照上面1、2方式进行设置,所有开发人员获取到代码,都使用的是开发环境,无需其他额外的配置。当应用程序部署到QA,生产或者其他环境中,负责部署的人根据情况使用系统属性,环境变量或者JNDI设置spring.profiles.active即可,当设置spring.profiles.active以后,至于spring.profiles.default设置成什么已经无所谓了,系统优先使用spring.profiles.active中设置的profile.

active和default都使用的是profiles复数形式,说明你可以同时激活多个profile.但是这样可能意义不大。

猜你喜欢

转载自blog.csdn.net/m0_37668842/article/details/82747702