chapter03_高级装配_2_Profile与条件化Bean

  • 开发和部署可能会使用不同的数据库,Profile就在解决这个问题

  • 通过配置 profile bean,Spring可以在运行时确定使用哪个bean

  • Java Config配置

    @Profile(“xxx”)

    示例

      @Configuration
      public class DataSourceConfig {
    
      @Bean
      @Profile("dev")
      public DataSource embeddedDataSource() {
    
          return new EmbeddedDatabaseBuilder()
              .setType(EmbeddedDatabaseType.H2)
              .addScript("classpath:schema.sql")
              .addScript("classpath:test-data.sql")
              .build();
      }
    
      @Bean
      @Profile("prod")
      public DataSource jndiDataSource() {
    
          JndiObjectFactoryBean jndiObjectFactoryBean
                  = new JndiObjectFactoryBean();
          jndiObjectFactoryBean.setJndiName("jdbc/myDS");
          jndiObjectFactoryBean.setResourceRef(true);
          jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
    
          return (DataSource) jndiObjectFactoryBean.getObject();
      }
    

    }

    示例中的"dev"是用于开发时配置的数据库,其中模式(创建数据库的sql语句)定义在schema.sql中,测试数据放在test-data.sql中

  • xml配置

    示例

      <?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:jdbc="http://www.springframework.org/schema/jdbc"
             xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
             xsi:schemaLocation="http://www.springframework.org/schema/jee
                                 http://www.springframework.org/schema/jee/spring-jee.xsd
                                 http://www.springframework.org/schema/jdbc
                                 http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                                 http://www.springframework.org/schema/beans
                                 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <beans profile="dev">
            <jdbc:embedded-database id="dataSource1" type="H2">
            <jdbc:script location="classpath:schema.sql" />
            <jdbc:script location="classpath:test-data.sql" />
            </jdbc:embedded-database>
        </beans>
    
        <beans profile="prod">
            <jee:jndi-lookup id="dataSource2" lazy-init="true" jndi-name="jdbc/myDatabase" resource-ref="true" proxy-interface="javax.sql.DataSource" />
        </beans>
      </beans>
    

    和之前不同,元素可以嵌套元素,以指定不同的Profile

  • 没有指定 Profile的bean总是会创建

  • 激活Profile

    (1) 激活Profile需要两个属性: spring.profiles.activespring.profiles.default,如果都没有设置的话,那么只会加载那些没有定义Profile的bean

    (2) active和default的设置方法有以下几种

    1 DispatcherServlet的初始化参数

    2 web.xml的元素或Servlet的元素

    3 集成测试类中,使用 @ActiveProfiles注解

    示例

      <?xml ......?>
      <web-app ...>
    
          <context-param>
              <param-name>spring.profiles.default<param-name/>
              <param-value>dev</param-value>
          </context-param>
    
          <listner>
              <listener-class>
                  org.springframework.web.context.ContextLoadListener
              </listener-class>
          </listner>
    
          <servlet>
              <servlet-name>appServlet</servlet-name>
              <servlet-class>
                  org.springframework.servlet.DispatcherServlet
              </servlet-class>
              <init-param>spring.profiles.default</init-param>
              <param-value>dev</param-value>
          </servlet>
    
          <servlet-mapping>
              <servlet-name>appServlet</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      </web-app>
    
  • 条件化Bean

    (1) 有时需要处理:当某个条件发生时,装配这个bean;不满足时,不装配这个bean。于是需要条件化bean

    (2) 条件化Bean使用 @Conditional注解

    示例

      @Bean
      @Conditional(MagicExistsCondition.class)
      public MagicBean magicBean() {
          return new MagicBean();
      }
    

    (3) 其中 括号内的部分代表了一个 实现了 Condition 接口的类,这个类的matches方法决定了是否为true,为true则创建Bean

  • 条件化Bean处理自动装配的歧义性

    (1) 使用首选 @Primary

    示例1(java config)

      @Component
      @Primary
      public class IceCream implements Dessert {
      }
    

    示例2(xml)

    (2) 使用限定符 @Qualifier

    示例

      @Component
      @Qualifier("cold")
      public class IceCream implements Dessert {
      }
    
      @Autowired
      @Qualifier("cold")
      public void setDessert(Dessert dessert) {
          this.dessert = dessert;
      }
    

猜你喜欢

转载自blog.csdn.net/captxb/article/details/87863770
今日推荐