1.声明
当前内容主要用于本人复习,使用本地的h2方式的数据源(h2加载本地sql脚本),使用mysql连接的数据源
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
2. 当前的sql脚本
my-schema.sql
create table `my_test` (
`id` int (11),
`name` varchar (60)
);
my-test-data.sql
insert into `my_test` (`id`, `name`) values('1','admin');
insert into `my_test` (`id`, `name`) values('2','guest');
insert into `my_test` (`id`, `name`) values('3','user');
insert into `my_test` (`id`, `name`) values('4','manager');
3.注解配置的demo
AppConfig.java
@Configuration
public class AppConfig {
// 开发环境下的数据源
@Bean("dataSource")
@Profile("development")
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:/com/hy/java/spring/environment/profile/my-schema.sql")
.addScript("classpath:/com/hy/java/spring/environment/profile/my-test-data.sql")
.build();
}
// 生产环境下的数据源
@Bean("dataSource")
@Profile("production")
public DataSource mySqlDataSource() {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setUser("root");
mysqlDataSource.setPassword("root");
mysqlDataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
return mysqlDataSource;
}
}
ProfileAnnotationTest.java
public class ProfileAnnotationTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("development");
//context.getEnvironment().setActiveProfiles("production"); // 切换到生产环境
context.register(AppConfig.class);
context.refresh();
DataSource bean = context.getBean(DataSource.class);
try {
Connection conn = bean.getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from my_test");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("id:" + id + ",name:" + name);
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(bean);
}
}
测试结果成功!,切换到production就直接使用
4. xml的demo
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<beans profile="development">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:/com/hy/java/spring/environment/profile/my-schema.sql"/>
<jdbc:script location="classpath:/com/hy/java/spring/environment/profile/my-test-data.sql"/>
</jdbc:embedded-database>
</beans>
<beans profile="production">
<bean id="dataSource" class="com.mysql.cj.jdbc.MysqlDataSource">
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
</bean>
</beans>
</beans>
ProfileAnnotationTest.java
/**
* @description 测试和使用@Profile注解并实现按照不同的开发环境使用不同的数据源
* // 使用xml配置方式实现
* @author admin
*
*/
public class ProfileAnnotationTest {
public static void main(String[] args) {
String xml="/com/hy/java/spring/environment/profile/xml/applicationContext.xml";
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(xml);
//context.getEnvironment().setActiveProfiles("development");
context.getEnvironment().setActiveProfiles("production"); // 切换到生产环境
context.refresh();
DataSource bean = context.getBean(DataSource.class);
try {
Connection conn = bean.getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from my_test");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("id:" + id + ",name:" + name);
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(bean);
}
}
测试结果成功!
5.总结
- 使用加载本地脚本需要使用
EmbeddedDatabaseBuilder
这个来build方式加载本地脚本,在xml文件中需要使用<jdbc:embedded-database id="dataSource" type="H2">
这个来加载本地脚本 xml中使用jdbc需要导入spring-jdbc的包和xml的标头
- 使用注解方式直接在需要修饰的地方使用
@Profile即可,但是在xml中却要在beans中指定profile属性
一定要记得在特定的ApplicationContext的子类中使用getEnvironment来设置setActiveProfiles激活使用的profile,不要refresh两次
(如果使用有参数的构造函数,默认会refresh一次)