Spring注解驱动开发实现属性赋值
作者:TomDu 时间:2023-05-07 04:40:22
前言
在实际开发当中,Spring中bean的属性直接赋值用的不是太多,整理这方面的资料,做一个小结,以备后续更深入的学习。
通过配置文件的方式
以配置文件的方式启动spring容器时,可以使用property标签的value给bean的属性赋值,赋值的形式有以下几种:
<--通过context:property-placeholder将properties文件中的值加载的环境变量中(properties中的属性值最终是以环境变量的形式存储的)>
<context:property-placeholder location="classpath:person.properties"/>
<bean id="person" class="com.atneusoft.bean.Person" >
<--①通过基本数值直接赋值-->
<property name="name" value="zhangsan"></property>
<--②通过${}取出配置文件中的值-->
<property name="age" value="${person.age}"></property>
<--③通过Spring的El表达式-->
<--<property name="age" value="10*2"></property>-->
</bean>
classpath下的properties文件内容
person.age=\u5C0F\u674E\u56DB
通过注解的方式
使用properties的value对应的注解给属性赋值
//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {
@Bean
public Person person(){
return new Person();
}
}
public class Person {
//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
//3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值)
@Value("张三")
private String name;
@Value("#{20-2}")
private Integer age;
/* @Value("${person.age}") private Integer age;*/
}
注:
外部配置文件中的k/v保存到运行的环境变量中,可以直接在环境变量中取出对应的值
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("person.age");
来源:https://www.cnblogs.com/tombky/p/12683767.html
标签:Spring,注解,属性,赋值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA插件之彩虹括号Rainbow Brackets使用介绍
2022-03-14 09:09:51
![](https://img.aspxhome.com/file/2023/5/98125_0s.png)
Android ItemDecoration 实现分组索引列表的示例代码
2022-02-10 20:22:12
![](https://img.aspxhome.com/file/2023/1/98661_0s.gif)
浅谈Java数值类型的转换与强制转换
2022-07-01 15:24:00
![](https://img.aspxhome.com/file/2023/9/89949_0s.png)
一起来学习C#的观察者模式
2022-04-02 13:43:15
![](https://img.aspxhome.com/file/2023/5/78115_0s.png)
Java多线程死锁示例
2022-09-17 15:05:25
![](https://img.aspxhome.com/file/2023/4/110614_0s.png)
react native打包apk文件安装好之后进入应用闪退的解决方案
2022-11-04 06:13:09
UE4 Unlua 调用异步蓝图节点AIMoveTo函数示例详解
2022-04-12 05:35:41
详解使用Spring Boot开发Restful程序
2023-01-24 09:20:09
![](https://img.aspxhome.com/file/2023/3/128863_0s.png)
Android实现记住账号密码功能
2021-10-02 01:51:24
![](https://img.aspxhome.com/file/2023/9/137929_0s.jpg)
c#使用Socket发送HTTP/HTTPS请求的实现代码
2023-10-12 07:10:00
![](https://img.aspxhome.com/file/2023/6/80046_0s.jpg)
Android实现简单点赞动画
2021-05-25 11:12:20
![](https://img.aspxhome.com/file/2023/0/113320_0s.gif)
Android画板开发之基本画笔功能
2023-01-09 07:26:21
![](https://img.aspxhome.com/file/2023/7/101867_0s.gif)
java eclipse 中文件的上传和下载示例解析
2021-11-18 06:55:58
![](https://img.aspxhome.com/file/2023/9/112049_0s.png)
详解Java的Hibernat框架中的Map映射与SortedMap映射
2021-08-21 20:31:59
Android中控件GridView实现设置行列分割线的方法示例
2021-05-24 09:08:48
C#知识整理
2021-08-02 18:24:26
Android中EditText光标在4.0中的bug及解决方法
2023-10-01 19:24:17
如何搭建新的WPF项目框架
2023-09-28 08:18:05
Dagger2 Android依赖注入学习笔记
2021-11-27 17:15:54
![](https://img.aspxhome.com/file/2023/7/96747_0s.png)
一篇文章带你轻松了解C# Lock关键字
2023-08-15 20:48:32
![](https://img.aspxhome.com/file/2023/9/108679_0s.png)