Spring4.0高级注解(二)

Spring高级装配

一 @profile注解

  • 该注解是Spring用来标明当前运行环境的注解。在实际开发过程中,开发环境,QA测试环境,线上部署环境都是不一样的,需要对每一种环境重新部署配置。尤其是从QA阶段到生产阶段,重新构建可能会带来bug并且让QA团队很不安。
  • Spring的@profile注解的作用就在于此。在Spring使用DI进行依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。Spring是等到运行的时候才确定创建或不创建哪个bean的,这样的结果就是同一个部署单元能够适用于所有的环境,没有必要进行重新构建。最常见的就是使用不同的DataSource。
//开发环境
@Configuration
@Profile("dev")
public class DevProfileConfig {

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource() {
        // 开发环境下使用内嵌数据库
        return new EmbeddedDatabaseBuilder()
                // 使用纯Java开发的DERBY数据库作为内嵌数据库
                .setType(EmbeddedDatabaseType.DERBY)
                // 创建表结构的sql文件
                .addScript("classpath:schema.sql")
                // 插入测试数据的sql文件
                .addScript("classpath:test-data.sql")
                .build();
    }
}

生产环境

@Configuration
@Profile("prod")
@PropertySource("classpath:db.properties")
public class ProdProfileConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(env.getProperty("jdbc.url"));
        ds.setDriverClassName(env.getProperty("jdbc.driverClass"));
        ds.setUsername(env.getProperty("jdbc.username"));
        ds.setPassword(env.getProperty("jdbc.password"));
        // 其他配置
        return ds;
    }
}

使用@Configuration注解来配置

@Configuration
@PropertySource("classpath:db.properties")
public class DataSourceConfig {

    @Autowired
    private Environment env;

    /**
     * 开发环境才激活
     */
    @Bean("dataSource")
    @Profile("dev")
    public DataSource embeddedDerbyDataSource(){
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.DERBY)
                .addScript("classpath:schema.sql")
                .addScript("classpath:test-data.sql")
                .build();
    }

    /**
     * 生产环境才激活
     */ 
    @Bean("dataSource")
    @Profile("prod")
    public DataSource mysqlDataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(env.getProperty("jdbc.url"));
        ds.setDriverClassName(env.getProperty("jdbc.driverClass"));
        ds.setUsername(env.getProperty("jdbc.username"));
        ds.setPassword(env.getProperty("jdbc.password"));
        return ds;
    }

}

或者用XML配置文件代替@Configuration注解

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:property-placeholder location="classpath:db.properties"/>

    <beans profile="dev">
        <jdbc:embedded-database id="dataSource">
            <jdbc:script location="classpath:schema.xml"/>
            <jdbc:script location="classpath:test-data.xml"/>
        </jdbc:embedded-database>
    </beans>
    
    <!-- 也可以通过import引入另一个xml配置,这样结构更清晰一点-->
    <beans profile="Development">
        <import resource="dev-config-context.xml"/>
    </beans>

    <beans profile="prod">
        <bean id="dataSource" 
            class="com.alibaba.druid.pool.DruidDataSource"
            destroy-method="close"
            p:url="${jdbc.url}"
            p:driverClassName="${jdbc.driverClass}"
            p:username="${jdbc.username}"
            p:password="${jdbc.password}">
        </bean>
    </beans>

</beans>

激活profile的几种方式
配置结束后就要激活profile,激活需要两个独立的properties属性:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active的值,就用它的值来确定哪个profile是激活的,如果没有设置,则使用spring.profiles.default设置的值。如果两个值都没有设置,则所有的profile都不会被激活,也就是至创建哪些没有定义在profile中的bean。
1.作为DispatcherServlet的初始化参数

<servlet>
<servlet-name>CoreServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

2.作为Web应用的上下文参数

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

3.在测试类上使用@ActiveProfiles注解激活指定的profile

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = DataSourceConfig.class)
@ActiveProfiles("dev")
public class ProfilesTest {

    @Autowired
    DataSource ds;

    @Test
    public void test() throws SQLException {
        Connection conn = ds.getConnection();
        System.out.println(conn);
        conn.close();
    }

}

4.在SpringBoot中通常会把服务打成jar包部署,可以在命令后面指定激活哪个profile

java -jar XXX.jar --spring.profiles.active=prod

@profile部分参考了以下文章:https://blog.csdn.net/Holmofy/article/details/79257398

二 条件化的bean(@Conditional注解)

  • Spring4提供了一个更加通用的基于条件的Bean的创建,即使用@Conditional注解。
  • @Conditional可以根据满足某一个特定条件来创建一个特定的Bean。比如说,当某个jar包在某个类路径下的时候,自动转配某个Bean;或者希望某个Bean在另外一个特定的Bean也声明了之后才会创建;或者希望只有某个特定的环境变量设置了之后,才会创建某个Bean。
  • Spring Boot中大量的应用到了这个条件注解,在Spring4之前,这种级别的条件化配置是很难实现的。

首先定义一个Bean,这个Bean只有在满足MagicExistCondition中定义的条件时才会创建。

@Bean
@Conditional(MagicExistCondition.class)
public MagicBean magicBean() {
    return new MagicBean();
}
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MagicExistCondition implements Condition {

    /**
     * 如果返回true,Bean将会被创建并注册到Spring容器中;否则不会创建Bean
     */
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment env = context.getEnvironment();
        // 当定义了magic属性的时候返回true
        return env.containsProperty("magic");
    }

}

注意:
1.设置给@Conditional的类可以是任意实现了Condition接口的类型,这个接口实现起来很简单,只要提供matches()方法的实现即可。如果该方法返回值为true,那么就会创建带有@Conditional注解的bean。
2.matches()方法通过给定的ConditionContext对象进而得到Environment对象,并使用这个对象检查环境中是否存在名为magic的环境属性。
Spring4对@Profile注解重构
从Spring4开始,@Profile注解进行了重构。@Profile本身也使用了@Condition注解,并引用了ProfileCondition作为Condition实现。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 使用@Conditional注解,并定义了ProfileCondition
@Conditional(ProfileCondition.class)
public @interface Profile {
    String[] value();
}

可以看一下ProfileCondition的实现

/**
 * @since 4.0
 */
class ProfileCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (context.getEnvironment() != null) {
            // 获取Profile注解的所有属性
            MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
            if (attrs != null) {
                // 遍历注解中value属性的所有元素
                for (Object value : attrs.get("value")) {
                    // 检查@Profile注解中的是否处于激活状态
                    if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
                        return true;
                    }
                }
                return false;
            }
        }
        return true;
    }

}

可以看到,ProfileCondition通过AnnotatedTypeMetadata得到了用于@Profile注解的所有属性。借助该信息,它会明确的检查value属性,该属性包含了bean的profile名称。然后通过ConditionContext得到Environment来检查该profile是否处于激活状态。

Spring的Environment

Spring的Environment包含两个关键方面:
1.profiles配置组
是一组bean的定义,对应配置文件中<beans profile="">,只有相应的profile被激活的情况下才会起作用。
2.properties环境变量
在几乎所有的应用中,Properties环境变量都扮演着非常重要的角色,且这些变量值可以来自于各种PropertySource属性源,如:properties文件、jvm虚拟机环境变量、操作系统环境变量、JNDI、Servlet上下文参数、自定义的属性对象、Map对象,等等。Environment环境对象为用户提供了方便的接口,用于配置和使用属性源。
常用的方法包括:

方法 说明
getProperty(String key) 获取属性值
containsProperty(String key) 判断属性是否存在
getPropertyAsClass(String className, Class class) 将属性解析为类
getActiveProfiles() 返回激活profile名称的数组
getDefaultProfiles() 返回默认profile名称的数组
acceptsProfiles(String… profiles) 如果environment支持给定profile的话,就返回true

猜你喜欢

转载自blog.csdn.net/LittleMoon_lyy/article/details/88090940