jdbc.properties在resources目录下
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!--SpringBoot启动器作为父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<!--引入web工程启动器-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
</dependencies>
</project>
方式一:
JdbcConfig
package com.company.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration//表明这是一个配置类
@PropertySource("classpath:jdbc.properties")//加载属性文件
public class JdbcConfig {
@Value("${jdbc.driverClassName}")//从属性文件中取值
private String driverClassName;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Bean//将返回值注入容器
public DataSource getDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
return dataSource;
}
}
方式二:
修改jdbc.properties文件名为:application.properties
引入Lombok依赖(这一步只是为了简化getter和setter等方法的代码,手动补齐可跳过)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
编写属性类JdbcProperties
package com.company.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "jdbc")//加载属性文件并指定前缀
@Data//编译期生成getter、setter、equals、hashcode、normal constructor
public class JdbcProperties {
//成员变量名称必须和属性文件中定义的key前缀.[名称]一致
private String driverClassName;
private String username;
private String password;
private String url;
}
修改JdbcConfig
package com.company.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)//启用属性配置类
public class JdbcConfig {
@Bean
public DataSource getDataSource(JdbcProperties prop){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(prop.getDriverClassName());
dataSource.setUsername(prop.getUsername());
dataSource.setPassword(prop.getPassword());
dataSource.setUrl(prop.getUrl());
return dataSource;
}
}
方式三:
删除JdbcProperties类,修改JdbcConfig
package com.company.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class JdbcConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")//此时会自动找到application.properties中jdbc为前缀的属性去匹配DruidDataSource中是否有对应的setter方法,自动注入(在DruidDataSource的父类中找到了对应setter方法)public DataSource getDataSource(){
return new DruidDataSource();
}
}
以上三种方式都可以在controller中注入并debug调试查看属性是否注入成功
package com.company.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class HelloController {
@Autowired
private DataSource dataSource;
@GetMapping("/hello")
public String hello(){
return "hello Springboot!";//debug point
}
}