Spring Boot学习日志------连接数据库三方法之Springboot属性注入
目录
- 创建application.properties(如果有jdbc.properties文件合并以下,然后删除jdbc.properties)
- 创建配置类文件
- 将配置类文件注入到对象中
创建application.properties文件
#数据库连接配置 jdbc.driverClassNamr=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm jdbc.username=root jdbc.password=root
创建配置类文件
package com.example.ycrk.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * ConfigurationProperties从application配置文件中读取配置 * prefix配置项前缀 * 配置项前缀的类变量名必须与前缀之后的配置项名称保持 松散绑定 */ @Component @ConfigurationProperties(prefix="jdbc") public class JdbcProperties { private String driverClassNamr; private String url; private String username; private String password; public String getDriverClassNamr() { return driverClassNamr; } public void setDriverClassNamr(String driverClassNamr) { this.driverClassNamr = driverClassNamr; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } 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; } }
创建对象
package com.example.ycrk.config; import com.alibaba.druid.pool.DruidDataSource; 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 @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { @Bean public DataSource dataSource(JdbcProperties jdbcProperties){ DruidDataSource druidDataSource=new DruidDataSource(); druidDataSource.setDriverClassName(jdbcProperties.getDriverClassNamr()); druidDataSource.setUrl(jdbcProperties.getUrl()); druidDataSource.setUsername(jdbcProperties.getUsername()); druidDataSource.setPassword(jdbcProperties.getPassword()); return druidDataSource; } }
调用
package com.example.ycrk.controller; 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(){ System.out.println("dataSource="+dataSource.toString()); return "Hello,Spring Boot!"; } }
结果
- 繁琐,如果变量过多不是很方便
将JdbcConfig 代码更换为以下代码
package com.example.ycrk.config; import com.alibaba.druid.pool.DruidDataSource; 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 @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfig { /*** * 自动根据前缀调取相应的方法 * @return */ @Bean @ConfigurationProperties(prefix = "jdbc") public DataSource dataSource(){ return new DruidDataSource(); } }
错误:ConfigurationProperties注入爆红
解决方法
1、在pom.xml中添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2、ConfigurationProperties注入方式上面添加@Component
@Component @ConfigurationProperties(prefix="jdbc")