一、配置注解读取配置文件
(1)@PropertySource可以指定读取的配置文件,通过@Value注解获取值
实例:
@PropertySource(value = {"classpath:jdbc.properties"})
注意如果是多个配置文件配置,用逗号隔开
@PropertySource(value = {"classpath:jdbc.properties","classpath:jdbc.properties"})
配置注解的顺序不是必须的,可以任意
(2)如果配置文件不存在如何处理,用注解中自带的方法标注。
实例:
@PropertySource(value = {"classpath:jdbc.properties"},ignoreResourceNotFound = true)
二、读取配置文件案例
实战:
1、建立BoneCPDataSource代替XML文档
1 package com.liuyangfirst.conf; 2 3 import com.jolbox.bonecp.BoneCPDataSource; 4 //import com.liuyangfirst.dao.UserDao; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.ComponentScan; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.PropertySource; 10 11 import javax.sql.DataSource; 12 13 /** 14 * Created by liuya 15 * User: liuya 16 * Date: 2018/3/24 17 * Time: 22:06 18 * projectName:20180324versionone 19 */ 20 21 @Configuration 22 @PropertySource(value = {"classpath:jdbc.properties"}, ignoreResourceNotFound = true) 23 public class SpringConf { 24 25 @Value("${jdbc.url}") 26 private String jdbcUrl; 27 28 @Value("${jdbc.driverClassName}") 29 private String jdbcDriverClassName; 30 31 @Value("${jdbc.username}") 32 private String jdbcUsername; 33 34 @Value("${jdbc.password}") 35 private String jdbcPassword; 36 37 @Bean(destroyMethod = "close",name = "dataSource") 38 public DataSource dataSource() { 39 BoneCPDataSource boneCPDataSource = new BoneCPDataSource(); 40 // 数据库驱动 41 boneCPDataSource.setDriverClass(jdbcDriverClassName); 42 // 相应驱动的jdbcUrl 43 boneCPDataSource.setJdbcUrl(jdbcUrl); 44 // 数据库的用户名 45 boneCPDataSource.setUsername(jdbcUsername); 46 // 数据库的密码 47 boneCPDataSource.setPassword(jdbcUsername); 48 // 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 49 boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60); 50 // 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 51 boneCPDataSource.setIdleMaxAgeInMinutes(30); 52 // 每个分区最大的连接数 53 boneCPDataSource.setMaxConnectionsPerPartition(100); 54 // 每个分区最小的连接数 55 boneCPDataSource.setMinConnectionsPerPartition(5); 56 return boneCPDataSource; 57 } 58 59 60 }
2、建立jdbc.properties,写入需要配置的信息
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf8&useUnicode=true&autoReconnect=true&allowMultiQueries=true 3 jdbc.username=root 4 jdbc.password=123456
3、配置pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.liuyangfirst</groupId> 7 <artifactId>20180330versiontwo</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>war</packaging> 10 11 <name>20180330versiontwo</name> 12 <description>Demo project for Spring Boot</description> 13 <url>http://maven.apache.org</url> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 18 <java.version>1.8</java.version> 19 <junit.version>4.12</junit.version> 20 </properties> 21 <parent> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-parent</artifactId> 24 <version>1.4.1.RELEASE</version> 25 </parent> 26 27 <dependencies> 28 <!-- JUnit --> 29 <dependency> 30 <groupId>junit</groupId> 31 <artifactId>junit</artifactId> 32 <version>${junit.version}</version> 33 <scope>test</scope> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-webmvc</artifactId> 38 <version>4.3.7.RELEASE</version> 39 </dependency> 40 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-test</artifactId> 44 <version>4.3.9.RELEASE</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-test</artifactId> 49 <version>4.3.9.RELEASE</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-starter-test</artifactId> 54 <scope>test</scope> 55 </dependency> 56 <dependency> 57 <groupId>junit</groupId> 58 <artifactId>junit</artifactId> 59 <version>4.12</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework.boot</groupId> 63 <artifactId>spring-boot-test</artifactId> 64 <version>RELEASE</version> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework.boot</groupId> 68 <artifactId>spring-boot</artifactId> 69 <version>2.0.0.RELEASE</version> 70 </dependency> 71 <dependency> 72 <groupId>org.springframework.boot</groupId> 73 <artifactId>spring-boot-autoconfigure</artifactId> 74 <version>1.4.1.RELEASE</version> 75 </dependency> 76 77 <dependency> 78 <groupId>javax.servlet.jsp</groupId> 79 <artifactId>jsp-api</artifactId> 80 <version>2.2</version> 81 <scope>provided</scope> 82 </dependency> 83 84 <dependency> 85 <groupId>javax.servlet</groupId> 86 <artifactId>servlet-api</artifactId> 87 <version>2.5</version> 88 <scope>provided</scope> 89 </dependency> 90 91 <dependency> 92 <groupId>commons-logging</groupId> 93 <artifactId>commons-logging</artifactId> 94 <version>1.2</version> 95 </dependency> 96 97 <!-- 连接池 --> 98 <dependency> 99 <groupId>com.jolbox</groupId> 100 <artifactId>bonecp-spring</artifactId> 101 <version>0.8.0.RELEASE</version> 102 </dependency> 103 104 <dependency> 105 <groupId> org.apache.cassandra</groupId> 106 <artifactId>cassandra-all</artifactId> 107 <version>0.8.1</version> 108 109 <exclusions> 110 <exclusion> 111 <groupId>org.slf4j</groupId> 112 <artifactId>slf4j-log4j12</artifactId> 113 </exclusion> 114 <exclusion> 115 <groupId>log4j</groupId> 116 <artifactId>log4j</artifactId> 117 </exclusion> 118 </exclusions> 119 </dependency> 120 121 <dependency> 122 <groupId>org.slf4j</groupId> 123 <artifactId>slf4j-simple</artifactId> 124 <version>1.7.25</version> 125 </dependency> 126 <!-- MySql 5.5 Connector --> 127 <dependency> 128 <groupId>mysql</groupId> 129 <artifactId>mysql-connector-java</artifactId> 130 <version>5.1.13</version> 131 </dependency> 132 133 <dependency> 134 <groupId>com.google.guava</groupId> 135 <artifactId>guava</artifactId> 136 <version>21.0</version> 137 </dependency> 138 139 </dependencies> 140 141 <build> 142 <finalName>${project.artifactId}</finalName> 143 <plugins> 144 <!-- 资源文件拷贝插件 --> 145 <plugin> 146 <groupId>org.apache.maven.plugins</groupId> 147 <artifactId>maven-resources-plugin</artifactId> 148 <configuration> 149 <encoding>UTF-8</encoding> 150 </configuration> 151 </plugin> 152 <!-- java编译插件 --> 153 <plugin> 154 <groupId>org.apache.maven.plugins</groupId> 155 <artifactId>maven-compiler-plugin</artifactId> 156 <configuration> 157 <source>1.7</source> 158 <target>1.7</target> 159 <encoding>UTF-8</encoding> 160 </configuration> 161 </plugin> 162 </plugins> 163 </build> 164 165 </project>
4、测试数据库联通
1 package com.liuyangfirst.test; 2 3 import com.liuyangfirst.conf.SpringConf; 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 /** 12 * Created by liuya 13 * User: liuya 14 * Date: 2018/3/30 15 * Time: 22:56 16 * projectName:20180330versiontwo 17 */ 18 public class TestDateSource { 19 20 public static void main(String[] args) { 21 // 通过Java配置来实例化Spring容器 22 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf.class); 23 24 DataSource dataSource = context.getBean(DataSource.class); 25 26 Connection connection = null; 27 28 try { 29 connection = dataSource.getConnection(); 30 if (connection != null) { 31 System.out.println("Connection successful!"); 32 Statement stmt = connection.createStatement(); 33 ResultSet rs = stmt.executeQuery(" select * from tb_user "); // do something with the connection. 34 while (rs.next()) { 35 System.out.println(rs.getString(1)); // should print out "1"' 36 System.out.println(rs.getString(2)); // should print out "1"' 37 } 38 } 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } finally { 42 try { 43 connection.close(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 } 49 }
5、测试结果