目录
操作说明
创建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 classJdbcProperties {privateString driverClassNamr;privateString url;privateString username;privateString password;publicString getDriverClassNamr() {returndriverClassNamr;
}public voidsetDriverClassNamr(String driverClassNamr) {this.driverClassNamr =driverClassNamr;
}publicString getUrl() {returnurl;
}public voidsetUrl(String url) {this.url =url;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(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 classJdbcConfig {
@BeanpublicDataSource dataSource(JdbcProperties jdbcProperties){
DruidDataSource druidDataSource=newDruidDataSource();
druidDataSource.setDriverClassName(jdbcProperties.getDriverClassNamr());
druidDataSource.setUrl(jdbcProperties.getUrl());
druidDataSource.setUsername(jdbcProperties.getUsername());
druidDataSource.setPassword(jdbcProperties.getPassword());returndruidDataSource;
}
}
调用
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;
@RestControllerpublic classHelloController {
@AutowiredprivateDataSource dataSource;
@GetMapping("hello")publicString 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 classJdbcConfig {/***
* 自动根据前缀调取相应的方法
* @return*/@Bean
@ConfigurationProperties(prefix= "jdbc")publicDataSource dataSource(){return newDruidDataSource();
}
}
操作出现的问题
错误:ConfigurationProperties注入爆红
解决方法
1、在pom.xml中添加以下依赖
org.springframework.boot
spring-boot-configuration-processor
true
2、ConfigurationProperties注入方式上面添加@Component
@Component
@ConfigurationProperties(prefix="jdbc")