项目结构
案例代码
配置类 SpringConfiguration
- @Configuration
- @Import(JdbcCofnig.class)
- @PropertySource(value = "classpath:/jdbc.yml",factory = YamlPropertySourceFactory.class)
- public class SpringConfiguration {
- }
- JdbcConfig
- package config;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import javax.sql.DataSource;
- /**
- * JDBC 配置类
- * @Author Helius
- * @Create 2019-11-01-19:47
- */
- public class JdbcCofnig {
- @Value("${jdbc.driver}")
- private String driver;
- @Value("${jdbc.url}")
- private String url;
- @Value("${jdbc.username}")
- private String username;
- @Value("${jdbc.password}")
- private String password;
- @Bean("datasource")
- public DataSource createDataSource() {
- System.out.println("驱动类是:" + driver);
- //1. 创建 Spring 内置数据源
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- //2. 给数据源填充属性
- dataSource.setDriverClassName(driver);
- dataSource.setUrl(url);
- dataSource.setUsername(username);
- dataSource.setPassword(password);
- return dataSource;
- }
- }
- # Yet Another Markup Language 另一种标记语言
- # YAML YAML
- # 键和值之间用冒号和空格分隔
- jdbc:
- driver: com.MySQL.jdbc.Driver
- url: jdbc:MySQL://localhost:3306/spring_ioc
- username: root
- password: admin
解释:
对于 properties 文件的解析,
spring4.3 以后, 我们无须在配置
<!-- 数据库配置文件位置 --> <context:property-placeholder location="classpath:jdbc.properties" />
或者
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>jdbc.properties</value>
- </property>
- <property name="fileEncoding">
- <value>UTF-8</value>
- </property>
- </bean>
默认使用的是
默认使用 PropertySourceFactory 接口的唯一实现类 DefaultPropertySourceFactory 来解析 properties 文件, 参见 @PropertySource 注解
该类也支持 xml, 但并不支持 YAML 格式.
YAML 格式是 springboot 所推荐的配置文件格式.
那如果使用 YAML 格式就需要我们自定义解析器.
首先需要入第三方 YAML 解析器
- <!-- 导入 yaml 文件解析器坐标 -->
- <dependency>
- <groupId>org.YAML</groupId>
- <artifactId>snakeyaml</artifactId>
- <version>1.23</version>
- </dependency>
然后自定义类型解析器
@PropertySource(value = "classpath:/jdbc.yml",factory = YamlPropertySourceFactory.class)
通过配置类的 @PropertySource 注解的 factory 属性
解析类 YamlPropertySourceFactory
- /**
- * 自定义解析 YAML 文件的工厂类
- * 引入 YAML 文件解析器
- * @Author Helius
- * @Create 2019-11-01-21:51
- */
- public class YamlPropertySourceFactory implements PropertySourceFactory {
- @Override
- public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
- //1. 创建 YAML 文件解析工厂
- YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
- //2. 设置要解析的资源内容
- factoryBean.setResources(resource.getResource());
- //3. 把资源解析成解析成 properties 文件
- Properties properties = factoryBean.getObject();
- // 返回 PropertySource 对象
- return (name != null ? new PropertiesPropertySource(name, properties) : new PropertiesPropertySource(resource.getResource().getFilename(), properties));
- }
- }
测试类
- /**
- * 测试类
- * @Author Helius
- * @Create 2019-11-01-19:54
- */
- public class SpringPropertySourceTest {
- public static void main(String[] args) throws SQLException {
- ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
- DataSource datasource = ac.getBean("datasource", DataSource.class);
- Connection connection = datasource.getConnection();
- connection.close();
- }
- }
控制台输出:
驱动类是: com.MySQL.jdbc.Driver
来源: http://www.bubuko.com/infodetail-3268776.html