在 application.properties 文件中先设置要读取的属性
database.driverName=con.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/chapter3
database.username=root
database.password=123456
application.properties 是Spring Boot 的默认配置文件,它会通过其机制读取到Caontext(上下文)中,这里使用Spring 的表达式
1:定义 ReadProperties POJO 类,
2:使用注解 @Component 把这个类注入到Ioc 容器中,
3:然后在每个属性的上方加上注解 @Value 使用 ${ } 这样的占位符读取配置在属性文件的内容,里面填写的值是我们在配置文件中配置的
3-1:或者也可以在 set 方法上加上注解 @Value 注入
POJO 代码
package cn.blogspring.chapter2.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author: qiuj
* @Description: @ConfigurationProperties
* @Date: 2019-01-03 20:16
*/
@Component
public class ReadProperties {
@Value("${blogspring.name}")
private String name;
@Value("${blogspring.url}")
private String url;
@Value("${blogspring.myblogurl}")
private String myblogurl;
@Value("${database.csdn}")
private String csdn;
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public void setMyblogurl(String myblogurl) {
this.myblogurl = myblogurl;
}
public void setCsdn(String csdn) {
this.csdn = csdn;
}
public String getName() {
return name;
}
public String getUrl() {
return url;
}
public String getMyblogurl() {
return myblogurl;
}
public String getCsdn() {
return csdn;
}
}
4: 写个Controller ,并将 项目 跑起来请求一下。发现全部被注入进了,可见读取属性成功
package cn.blogspring.chapter2.controller;
import cn.blogspring.chapter2.pojo.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: qiuj
* @Description:
* @Date: 2019-01-03 20:21
*/
@RestController
public class ReadController {
@Autowired
private ReadProperties readProperties;
@GetMapping("/query")
public ReadProperties query(){
return readProperties;
}
}
1:如果觉得一个一个属性添加 注解@Value() 太繁琐,也可以使用 @ConfigurationProperties , 这里配置的字符串将于我们的属性名称组成属性的全限定名去配置文件里查找 例如: blogspring + "." +name 之间的 . 逗号 Spring 会自动加上或者是忽略
记得把之前@Vlaue 注释掉,直接使用 @ConfigurationProperties 注解配置前缀名即可
2:在请求一下接口,发现属性也被注入了, 不过 csdn 找不到 , 因为我们在 @ConfigurationProperties 配置的名称没有匹配到
database.csdn=https://blog.csdn.net/publicv
1:还有一个比较常见的问题是:我们一般 不会把所有的配置信息写在一个 properties 文件上。
在这边我们新建配置文件blogspring.properties,把原来在 application 配置的属性注释掉!
blogspring.name=qiujian
blogspring.url=blogspring.cn
blogspring.myblogurl=http://blogspring.cn/users/1
再新建配置文件jdbc.properties
database.csdn=https://blog.csdn.net/publicv
2:在Boot 启动类 Application.java 类上添加注解 @Properties() , value = { "" , "" } 可以配置多个属性文件,
使用 classpath 前缀,意味着去类文件路径下找到属性文件, 在我这边是 resource 目录下
@PropertySource(value = {"classpath:blogspring.properties","classpath:jdbc.properties"})
3:在此运行项目,请求接口
4:在注解 @PropertySource() 配置 igonreResourceNotFound=true,
@PropertySource(value = {"classpath:jdbc.properties","classpath:application.properties"},ignoreResourceNotFound = true)
表示是否忽略配置文件找不到的问题,默认值为false,也就是没有找到配置文件就报错!
在测试启动项目中发现,当你配置上的属性文件 找不到时,不添加 igonreResourceNotFound 确实会报错,添加上 igonreResourceNotFound = true 就不会
nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist
还有一种情况是:添加上 igonreResourceNotFound = true也还是报错,原因在于你的类中,有些引用到了 找不到的配置文件上的属性
报错信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readProperties': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'database.csdn' in value "${database.csdn}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1237) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 19 common frames omitted
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'database.csdn' in value "${database.csdn}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:851) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1185) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 30 common frames omitted
去掉这些依赖的属性 在试一遍
成功运行!