Spring加载外部的properties文件的简单实践
注意:
使用xml配置关注 1.1 1.2 1.5
使用配置类配置关注 1.1 1.3 1.4 1.5
1.1 新建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
1.2 新建applicationContext.xml )
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}" ></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>
1.3 新建DataSourceConfiguration.java (数据源类 注意文件中的Bean配置)
package com.zhan.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; import java.beans.PropertyVetoException; @PropertySource("classpath:jdbc.properties") public class DataSourceConfiguration { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String user; @Value("${jdbc.password}") private String password; @Bean("dataSource") //Spring会将当前方法的返回值以指定名称存储到Spring容器中 public DataSource getDataSource() { ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass(driver); dataSource.setJdbcUrl(url); dataSource.setUser(user); dataSource.setPassword(password); } catch (PropertyVetoException e) { e.printStackTrace(); } return dataSource; } }
1.4 编写配置类 SpringConfiguration.java
package com.zhan.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @ComponentScan("com.zhan") @Import({DataSourceConfiguration.class}) public class SpringConfiguration { }
1.5 编写Junit测试类
package com.zhan.test; import com.zhan.config.SpringConfiguration; import com.zhan.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext.xml") //@ContextConfiguration(classes = {SpringConfiguration.class}) @ComponentScan("com.zhan") public class SpringJunitTest { @Autowired private UserService userService; @Test public void test1(){ userService.save(); } }
Nice to see you all!