spring配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
jdbc.username=root
jdbc.password=031516
package hero.mps.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 JdbcConfiguration {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(this.driverClassName);
source.setUrl(this.url);
source.setUsername(this.username);
source.setPassword(this.password);
return source;
}
}
package hero.mps.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
private DataSource dataSource; //将bean注入
@GetMapping("show")
public String hello(){
return "hello";
}
}
springboot
- 编写配置文件
application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
jdbc.username=root
jdbc.password=031516
package hero.mps.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "jdbc") //根据前缀名查找
public class JdbcProperties {
//下面变量的设置需要和配置文件保持一致
private String driverClassName;
private String url;
private String username;
public String getDriverClassName() {
return driverClassName;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
}
springboot四种属性注入
Autowired注入
package hero.mps.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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")
@EnableConfigurationProperties(JdbcProperties.class)//启用配置文件
public class JdbcConfiguration {
@Autowired
JdbcProperties jdbcProperties;
@Bean
public DataSource dataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(this.jdbcProperties.getDriverClassName());
source.setUrl(this.jdbcProperties.getUrl());
source.setUsername(this.jdbcProperties.getUsername());
source.setPassword(this.jdbcProperties.getPassword());
return source;
}
}
构造方法注入
package hero.mps.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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")
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
JdbcProperties jdbcProperties;
public JdbcConfiguration(JdbcProperties jdbcProperties) {
this.jdbcProperties = jdbcProperties;
}
@Bean
public DataSource dataSource(){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(this.jdbcProperties.getDriverClassName());
source.setUrl(this.jdbcProperties.getUrl());
source.setUsername(this.jdbcProperties.getUsername());
source.setPassword(this.jdbcProperties.getPassword());
return source;
}
}
在@Bean方法上形参注入
package hero.mps.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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")
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean
public DataSource dataSource(JdbcProperties jdbcProperties){
DruidDataSource source = new DruidDataSource();
source.setDriverClassName(jdbcProperties.getDriverClassName());
source.setUrl(jdbcProperties.getUrl());
source.setUsername(jdbcProperties.getUsername());
source.setPassword(jdbcProperties.getPassword());
return source;
}
}
在@Bean方法上启用@ConfigurationProperties配置
package hero.mps.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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")
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfiguration {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource(){
DruidDataSource source = new DruidDataSource();
return source;
}
}