当我们在开发过程中,有生产环境、开发环境、测试环境、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注解的作用。