需求
通过java配置类实现一个数据库连接池。
以前xml中是这样写的:
<!--配置德鲁伊数据库连接池--> <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> </bean>
java配置常用的注解
@Configuration :声明一个类作为配置类 @Bean :声明在方法上,将方法的返回值加入Bean容器 @value :属性注入 @PropertySource :指定外部属性文
环境准备
创建一个springboot工程:https://www.cnblogs.com/uncleyong/p/16197938.html
引入Druid连接池依赖:这里我们不导入druid数据源场景启动器:druid-spring-boot-starter
<!--Druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
创建一个jdbc.properties文件,编写jdbc属性:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://47.10.15.47:3306/gift?useUnicode=true&characterEncoding=utf-8&useSSL=true jdbc.username=root jdbc.password=123456
方式一:@Value注解注入
配置类
package com.qzcsbj.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.JdbcProperties; 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") // 不是默认配置文件application.properties,需要指定 public class JdbcConfig { @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.url}") String url; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
然后我们就可以在任意位置通过 @Autowired 注入DataSource了
控制器
package com.qzcsbj.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.sql.DataSource; import java.sql.SQLException; @RestController public class HelloController { @Autowired DataSource dataSource; @RequestMapping(value={"hello"}, method = RequestMethod.GET) public String hello() throws SQLException { System.out.println("=====执行控制器======"); System.out.println("连接对象:" + dataSource.getConnection()); return "hello springboot"; } }
请求:http://localhost:8080/hello
debug测试
package com.qzcsbj.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.JdbcProperties; 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") // 不是默认配置文件application.properties,需要指定 public class JdbcConfig { @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.url}") String url; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); // dataSource.setMaxActive(20); // dataSource.setMinIdle(5); // dataSource.setMaxIdle(10); // dataSource.setMaxWait(5000); // dataSource.setInitialSize(5); return dataSource; } }
请求:http://localhost:8080/hello
package com.qzcsbj.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.JdbcProperties; 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") // 不是默认配置文件application.properties,需要指定 public class JdbcConfig { @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.url}") String url; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaxActive(20); dataSource.setMinIdle(5); dataSource.setMaxIdle(10); dataSource.setMaxWait(5000); dataSource.setInitialSize(5); return dataSource; } }
请求:http://localhost:8080/hello
可以看到,属性注入成功了。
方式二:SpringBoot的属性注入
在上面的案例中,只能注入基本类型值,在SpringBoot中,提供了一种新的属性注入方式,支持各种java基本数据类型及复杂类型的注入。
我们新建一个类,用来进行属性注入:
package com.qzcsbj.config; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "jdbc") public class JdbcProperties { String url; String driverClassName; String username; String password; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
在类上通过@ConfigurationProperties注解声明当前类为属性读取类
prefix="jdbc" 读取属性文件中,前缀为jdbc的值
在类上定义各个属性,名称必须与属性文件中 jdbc. 后面部分一致
在JdbcConfig中使用这个属性:
package com.qzcsbj.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 org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; @PropertySource("classpath:jdbc.properties") @Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { @Bean // 注入 public DataSource dataSource(JdbcProperties jdbc) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(jdbc.getUrl()); dataSource.setDriverClassName(jdbc.getDriverClassName()); dataSource.setUsername(jdbc.getUsername()); dataSource.setPassword(jdbc.getPassword()); // dataSource.setMaxActive(20); // dataSource.setMinIdle(5); // dataSource.setMaxIdle(10); // dataSource.setMaxWait(5000); // dataSource.setInitialSize(5); return dataSource; } }
debug测试
请求:http://localhost:8080/hello
方式三:SpringBoot更简洁的注入
如果一段属性只有一个Bean需要使用,我们无需将其注入到一个类(JdbcProperties)中,而是直接在需要的地方声明即可:
删除JdbcProperties,修改:JdbcConfig
package com.qzcsbj.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 org.springframework.context.annotation.PropertySource; import javax.sql.DataSource; @PropertySource("classpath:jdbc.properties") @Configuration public class JdbcConfig { @Bean @ConfigurationProperties(prefix = "jdbc") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setMaxActive(20); dataSource.setMinIdle(5); dataSource.setMaxIdle(10); dataSource.setMaxWait(5000); dataSource.setInitialSize(5); return dataSource; } }
我们直接把 @ConfigurationProperties(prefix = "jdbc") 声明在需要使用的 @Bean 的方法上,然后完成注入。
使用的前提是:该类必须有对应属性的set方法,jdbc.properties中前缀后面的内容和DataSource中的对上,SpringBoot就会自动把相关属性通过set方法注入到DataSource中
上面JdbcConfig类中,可以调用对应的set方法,比如:dataSource.setUsername();
点击setUsername会调转到DruidAbstractDataSource类中
DruidAbstractDataSource类的继承关系:
debug测试
请求:http://localhost:8080/hello
原文已更新:https://www.cnblogs.com/uncleyong/p/17103609.html
标签:jdbc,Java,String,配置,springframework,dataSource,org,import From: https://www.cnblogs.com/uncleyong/p/17103609.html