@Profile 条件化 bean

@Profile 条件化 bean

使用 Profile 的例子:开发环境使用 H2 数据库,线上环境使用 MySql 数据库

application.properties

datasource.url=jdbc:mysql://localhost:3306/spring_test?useUnicode=true&characterEncoding=utf-8
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.username=root
datasource.password=123456
datasource.initialSize=5
datasource.maxActive=10
datasource.maxWait=6000

spring.profiles.active=prod
spring.profiles.default=dev

数据库配置文件

@Configuration
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class DataTestConfig {
    @Value("${datasource.driverClassName}")
    private String driverClassName;

    @Value("${datasource.url}")
    private String url;

    @Value("${datasource.username}")
    private String username;

    @Value("${datasource.password}")
    private String password;

    @Value("${datasource.initialSize}")
    private int initialSize;

    @Value("${datasource.maxActive}")
    private int maxActive;

    @Value("${datasource.maxWait}")
    private int maxWait;

    @Bean
    @Profile("prod")
    public DruidDataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setInitialSize(initialSize);
        ds.setMaxActive(maxActive);
        ds.setMaxWait(maxWait);
        return ds;
    }

    @Bean
    @Profile("dev")
    public DataSource embeddedDataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:schema.sql")
                .addScript("classpath:test-data.sql")
                .build();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource){
        LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
        sfb.setDataSource(dataSource);
        sfb.setPackagesToScan(new String[] { "com.seal_de.domain" });
        Properties props = new Properties();
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        props.setProperty("hibernate.show_sql", "true");
        sfb.setHibernateProperties(props);
        return sfb;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }
}

profile 说明

  • profile 可以用来条件化 bean,使用 @Profile 注解即可(xml 配置也行)

  • profile 激活机制:有 spring.profiles.active 时,激活 active 标记的;没有则激活 spring.profiles.default 标记的;都没有,则只激活没有 profile 限定的

  • 激活方式有六种:

    • 作为 DispatcherServlet 的初始化参数;

    • 作为 Web 应用的上下文参数;

    • 作为 JNDI 条目;

    • 作为环境变量;

    • 作为 JVM 的系统属性;

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

该例子说明

  • 当属性文件包含 spring.profiles.active=prod 和spring.profiles.default=dev 时,使用的是 MySql 的数据源

  • 当属性文件只有 spring.profiles.default=dev 时,使用的是 H2 的数据源

@Condition 注解是更通用的条件化 bean 的注解

    原文作者:seal_de
    原文地址: https://segmentfault.com/a/1190000009654157
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞