Spring Boot IoC(九)使用@Profile

版权声明:From Lay https://blog.csdn.net/Sadlay/article/details/83277191

上一篇 Spring Boot IoC(八)Bean的作用域

九、使用@Profile

在企业开发的过程中,项目往往要面临开发环境,测试环境和生成环境的切换,这样在一个互联网企业中往往需要3套以上的环境。而每一套环境的上下文是不一样的。例如,它们会有各自的数据库资源,这样就要求我们在不同的数据库之间进行切换。为了方便,Spring还提供了Profile机制,使我们可以很方便地实现各个环境之间的切换。

java配置

假设存在dev_databsetest_database两个数据库,这样可以使用注解@profile定义两个bean。

定义DataBaseConfig

package com.lay.ioc.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class DataSourceConfig {
    @Bean(name = "dataSource", destroyMethod = "close")
    @Profile("dev")
    public DataSource getDevDataSource() {
        Properties prop = new Properties();
        prop.setProperty("driver", "com.mysql.jdbc.Driver");
        prop.setProperty("url", "jdbc:mysql://localhost:3306/dev_spring_boot");
        prop.setProperty("username", "root");
        prop.setProperty("password", "123456");
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("成功加载dev数据源");
        return dataSource;
    }
    
    @Bean(name = "dataSource", destroyMethod = "close")
    @Profile("test")
    public DataSource getTestDataSource() {
        Properties prop = new Properties();
        prop.setProperty("driver", "com.mysql.jdbc.Driver");
        prop.setProperty("url", "jdbc:mysql://localhost:3306/dev_spring_boot");
        prop.setProperty("username", "root");
        prop.setProperty("password", "654321");
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("成功加载test数据源");
        return dataSource;
    }
}

注意:在没有启动Profile机制下,被@Proflie的Bean是不会加载到ioc容器中的。

为了启动Profile机制,spring提供了两个参数供我们配置。

  • spring.profiles.active :优先级高,spring首先会判定是否存在
  • spring.profiles.default:优先级低,如active 没配置才会查找

在这两个属性都没有配置的情况看下,spring将不会启动Profile机制。意味着被@Profile标注的bean不会被装配到ioc容器。

配置方式一

springboot的配置文件application.yml(或application.propertiesl)

spring: 
  profiles:
    active: dev

配置方式二:JAVA_OPTS

JAVA_OPTS=”-Dspring.profiles.active=dev“

配置方式三:IDE配置

在eclipse中的Run Configuration运行选项中,在VM arguments中添加如下

-Dspring.profiles.active=dev

属性配置

对于属性配置(properties)文件而言,在Spring Boot中存在一个约定,即允许比较方便的切换配置环境。为了把配置数据库的不同的属性文件(properties)分开,spring boot可以很好地支持切换配置文件的功能。

首先我们在配置文件目录新增 application-dev.properties文件,然后将日志配置为DEBUG级别,这样启动SpringBoot就会有很详细的日志显示。

新增并配置application-dev.properties

logging.level.root=DEBUG
logging.level.org.springframework=DEBUG
#数据库连接配置
****

按照spring boot的规则,spring.profiles.active配置的值为{profile},则它会用application-{profile}.properties去代替原来默认的application.properties文件然后启动springboot应用。

下一篇 Spring Boot IoC(十)Spring EL

猜你喜欢

转载自blog.csdn.net/Sadlay/article/details/83277191