Spring中使用@Value注解给bean加载属性的配置文件有两种使用方式
第一种:使用@Value("#{configProperties['websit.msgname']}")
spring中配置属性加载文件的配置方式
1 2 3 4 5 6 7 |
< bean id = "configProperties" class = "org.springframework.beans.factory.config.PropertiesFactoryBean" > < property name = "locations" > < list > < value >classpath:/properties/websit.properties</ value > </ list > </ property > </ bean > |
注意
1.这里使用的configProperties必须要和定义的bean名称一致。
2.websit用来指定msgname来源于那个配置文件
3.配置的加载属性bean名称为org.springframework.beans.factory.config.PropertiesFactoryBean
第二种:使用@Value("${websit.msgname}");
使用这种方式,又可以有两种配置方式
方式一
1 2 3 4 5 6 7 8 9 10 11 |
< bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" > < property name = "properties" ref = "configProperties" /> </ bean >
< bean id = "configProperties" class = "org.springframework.beans.factory.config.PropertiesFactoryBean" > < property name = "locations" > < list > < value >classpath:/properties/websit.properties</ value > </ list > </ property > </ bean > |
方式二
1 2 3 4 5 6 7 |
< bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > < property name = "locations" > < list > < value >classpath:properties/websit.properties</ value > </ list > </ property > </ bean > |
当使用@Value注解bean属性时,如果没有在配置文件中配置,这时启动spring就会抛出异常。@Value提供了一种默认值的设置方式,如果在属性文件中没有配置则可以使用默认值。
形式如下
1 2 |
@Value ( "${avg.age:22}" ) private int userAge; |
如果使用@Value注解后,数据不能正常的被注入则需要在xml的配置文件中加入下列代码
1 |
< context:annotation-config /> |
SpringBoot使用注解(@value)读取properties(yml)文件中 配置信息
为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。
1. 两种使用方法
1)@Value("#{configProperties['key']}")
2)@Value("${key}")
2. 配置文件示例
ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home
说明:以上是配置文件中的信息,主要是一些账号密码等信息。
3. 读取yml配置文件的工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.dbright.dataprediction.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource ( "classpath:ftpconfig.yml" ) @ConfigurationProperties (prefix = "ftp" ) public class FtpProperties {
@Value ( "${ftplp}" ) public String ftplp; @Value ( "${ftpPort}" ) public String ftpPort; @Value ( "${ftpUser}" ) public String ftpUser; @Value ( "${ftpPwd}" ) public String ftpPwd; @Value ( "${ftpRemotePath}" ) public String ftpRemotePath;
public String getFtplp() { return ftplp; }
public void setFtplp(String ftplp) { this .ftplp = ftplp; }
public String getFtpPort() { return ftpPort; }
public void setFtpPort(String ftpPort) { this .ftpPort = ftpPort; }
public String getFtpUser() { return ftpUser; }
public void setFtpUser(String ftpUser) { this .ftpUser = ftpUser; }
public String getFtpPwd() { return ftpPwd; }
public void setFtpPwd(String ftpPwd) { this .ftpPwd = ftpPwd; }
public String getFtpRemotePath() { return ftpRemotePath; }
public void setFtpRemotePath(String ftpRemotePath) { this .ftpRemotePath = ftpRemotePath; } } |
说明:以上是使用@value注解来读取yml配置文件的代码示例
1) @component —— 把普通pojo实例化到spring容器中,相当于配置文件中的`<bean id="" class=""/>`
2) @PropertySource("classpath:ftpconfig.yml") —— 设置yml文件的路径,方便扫描到。一般我们配置文件都是放在resources包下。所以我们只需要 classpath+所需要读取的配置文件名称。
3) @ConfigurationProperties(prefix = "ftp") —— 这个不需要解释太多,配置文件里面内容的前缀,我们读取的是ftp下的信息。
4) @Value("${ftplp}") —— 这是读取我们所需的配置信息,美元符号+{字段名}即可制定
5)下面定义字符串来接收所读取到的配置信息。
6)写set和get方法,方便外部类调用。
4. 演示:效果图如下
可以看到,我们成功取到了我们想要的值。
5. 一开始说的第二种和这个差不多
把{}外的 $ 变成 # 号,然后里面指定配置文件的信息+字段而已。大同小异,我就不贴代码上来了。
今天的内容就到这里啦,以上为个人经验,希望能给大家一个参考,也希望大家多多支持!
原文链接:https://blog.csdn.net/huangjianghong6816/article/details/78748119
查看更多关于Spring注解@Value及属性加载配置文件方式的详细内容...