Spring注解@Value及属性加载配置文件
来源:互联网 发布:北京海量数据怎么样 编辑:程序博客网 时间:2024/06/04 23:18
Spring中使用@Value注解给bean加载属性的配置文件有两种使用方式
第一种:使用@Value("#{configProperties['websit.msgname']}")
spring中配置属性加载文件的配置方式
<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}");
使用这种方式,又可以有两种配置方式
方式一
<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>
方式二
<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提供了一种默认值的设置方式,如果在属性文件中没有配置则可以使用默认值。形式如下
@Value("${avg.age:22}") private int userAge;
如果使用@Value注解后,数据不能正常的被注入则需要在xml的配置文件中加入下列代码
<context:annotation-config/>
阅读全文
0 0
- Spring注解@Value及属性加载配置文件
- spring提供读取配置文件中的属性注解@Value
- Controller使用注解@Value加载配置文件 --SpringMVC
- 利用Spring的@Value注解读取配置文件
- 2.spring boot热启动,autowired自动加载,value注解,属性注入
- spring boot:使用@ConfigurationProperties注解加载配置文件
- 如何spring使用@value注解读取配置文件的值
- Spring之@Value 注解获取properties值(Spring 3的@value简化配置文件的读取)
- Spring通过@Value注解注入属性的几种方式
- @Value spring 注解 获取*.properties 的属性值
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- Spring通过@Value注解注入属性的几种方式
- Spring Boot 01 加载配置文件和获取key/value值
- Spring中利用配置文件和@value注入属性值
- Spring中利用配置文件和@value注入属性值
- Spring @Value注解问题
- 【Spring】学习@Value注解
- API接口管理工具免费开源,目前最好的接口管理工具DOClever
- mybatis的延迟加载与代码生成工具
- c语言实现IP地址输入的检查
- 今天,美团程序员的年终奖金可能没了
- 设计模式--中介者模式
- Spring注解@Value及属性加载配置文件
- ffmpeg 给缩略图添加水印
- 最好的6个Go语言Web框架
- Query-Oriented Multi-Document Summarization via Unsupervised Deep Learning
- checkbox 只有一个复选框 选中保存值为1 没有选中就是0
- CentOs之搭建自己的内网穿透服务器(Ngrok)-yellowcong
- 关于抽象类与静态类的初步了解
- 机器学习数据集
- java中Map及Map.Entry详解