@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 配置也行)java

  • profile 激活機制:有 spring.profiles.active 時,激活 active 標記的;沒有則激活 spring.profiles.default 標記的;都沒有,則只激活沒有 profile 限定的mysql

  • 激活方式有六種:spring

    • 做爲 DispatcherServlet 的初始化參數;sql

    • 做爲 Web 應用的上下文參數;數據庫

    • 做爲 JNDI 條目;session

    • 做爲環境變量;app

    • 做爲 JVM 的系統屬性;dom

    • 在集成測試類上,使用 @ActiveProfiles 註解設置測試

該例子說明

  • 當屬性文件包含 spring.profiles.active=prod 和spring.profiles.default=dev 時,使用的是 MySql 的數據源ui

  • 當屬性文件只有 spring.profiles.default=dev 時,使用的是 H2 的數據源

@Condition 註解是更通用的條件化 bean 的註解

相關文章
相關標籤/搜索