Spring Profile切换环境配置

当我们在开发过程中,有生产环境、开发环境、测试环境、QA环境等等,每种环境可能数据库都不同,还可能会有很多其他不同的配置,甚至说类也有可能不一样。

通过使用@Profile注解,可以让某个对象或者配置单独起作用,进行对象或者环境配置切换。

下面以代码为例:

package com.tyyd.ioc.bean.dependence.dao;

public interface IndexDao {
    void init();
}
package com.tyyd.ioc.bean.dependence.dao.impl;

import com.tyyd.ioc.bean.dependence.dao.IndexDao;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;

@Repository
@Profile("dao1")
public class IndexDaoImpl1 implements IndexDao {

    public IndexDaoImpl1() {};

    @PostConstruct
    public void init() {
        System.out.println("IndexDaoImpl1");
    }
}
package com.tyyd.ioc.bean.dependence.dao.impl;

import com.tyyd.ioc.bean.dependence.dao.IndexDao;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;

@Repository
@Profile("dao2")
public class IndexDaoImpl2 implements IndexDao {

    public IndexDaoImpl2() {};

    @PostConstruct
    public void init() {
        System.out.println("IndexDaoImpl2");
    }
}
package com.tyyd.ioc.bean.dependence.service;

public interface IndexService {
    void test();
}
package com.tyyd.ioc.bean.dependence.service.impl;

import com.tyyd.ioc.bean.dependence.dao.IndexDao;
import com.tyyd.ioc.bean.dependence.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexDao indexDao;

    public void test() {
        indexDao.init();
    }
}
package com.tyyd.ioc.bean.dependence.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
@ComponentScan(value = "com")
public class JavaConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("root");
        driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/test");
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return driverManagerDataSource;
    }
}
import com.tyyd.ioc.bean.dependence.config.JavaConfig;
import com.tyyd.ioc.bean.dependence.dao.IndexDao;
import com.tyyd.ioc.bean.dependence.service.IndexService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.getEnvironment().setActiveProfiles("dao2");
        annotationConfigApplicationContext.register(JavaConfig.class);
        annotationConfigApplicationContext.refresh();
        annotationConfigApplicationContext.getBean(IndexDao.class).getClass().getSimpleName();

    }
}

运行Test测试类,查看控制台输出结果如下:

我们更改Test测试类中的activeProfiles值,则输出结果改变了。至此我们即可理解@Profile注解的作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值