首先回顾下在xml中我们是如何为spring的bean进行属性赋值呢?

大体是这样的

	<bean id="person" class="com.atguigu.bean.Person"  scope="prototype" >
<property name="age" value="18"></property>
<property name="name" value="zhangsan"></property>
</bean>

这样就能够为person对象的agename属性进行赋值。

那使用@Value注解怎么做呢?又如何取出配置文件中的值呢,就像取出jdbc.properties中的值一样。

public class Person {

	//使用@Value赋值;
//1、基本数值
//2、可以写SpEL; #{}
//3、可以写${};取出配置文件【properties】中的值(在运行环境变量里面的值) @Value("张三")
private String name;
@Value("#{20-2}")
private Integer age; @Value("${person.nickName}")
private String nickName;
// 省略了getter、setter等
}

配置类:

//使用@PropertySource读取外部配置文件中的k/v保存到运行的环境变量中;加载完外部的配置文件以后使用${}取出配置文件的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues { @Bean
public Person person(){
return new Person();
} }

写个测试方法测试一下:

public class IOCTest_PropertyValue {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValues.class);
@Test
public void test01(){
printBeans(applicationContext);
System.out.println("============="); Person person = (Person) applicationContext.getBean("person");
System.out.println(person); ConfigurableEnvironment environment = applicationContext.getEnvironment();
String property = environment.getProperty("person.nickName");
System.out.println(property);
applicationContext.close();
} private void printBeans(AnnotationConfigApplicationContext applicationContext){
String[] definitionNames = applicationContext.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
} }

打印出结果:

mainConfigOfPropertyValues
person
=============
Person [name=张三, age=18, nickName=小李四]
小李四

这两个注解非常简单,@Value注解支持不仅支持简单赋值,还支持spring El表达式赋值,关于springEL表达式,可以参考这篇:springEL表达式

@PropertySource 就是导入外部的配置文件。

注意:在@Value中,使用springEL表达式使用#,引入配置文件中的值使用$,此文也有体现。

今天就这么多。

九、Spring中使用@Value和@PropertySource为属性赋值的更多相关文章

  1. Spring中如何向 Bean注入系统属性或环境变量

    [转自] http://unmi.cc/spring-injection-system-properties-env/ 在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的, ...

  2. Spring笔记04(DI(给属性赋值),自动装配(autowire))

    给不同数据类型注入值: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  3. Spring中的@Transactional(rollbackFor = Exception.class)属性详解

    序言 今天我在写代码的时候,看到了.一个注解@Transactional(rollbackFor = Exception.class),今天就和大家分享一下,这个注解的用法: 异常 如下图所示,我们都 ...

  4. coding++:Spring中的@Transactional(rollbackFor = Exception.class)属性详解

    异常: 如下图所示,我们都知道 Exception 分为 运行时异常 RuntimeException 和 非运行时异常. error 是一定会回滚的. 如果不对运行时异常进行处理,那么出现运行时异常 ...

  5. ExcelUtility 对excel的序列化与反序列化,支持当单元格中数据为空时将属性赋值为指定类型的默认值

    源码https://github.com/leoparddne/EPPlusHelper 安装: Install-Package ExcelUtility -Version 1.1.4 需要为对象添加 ...

  6. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  7. 事务理解及Spring中的事务

    一.隔离级别理解 1.脏读 首先理解,一个事务对数据进行了改变,尽管该事务尚未提交,但此时其他事务中的查询语句(注意一定是处于事务中的语句,不处于事务中的语句查到的是正常的)查到的数据,是该事务修改之 ...

  8. Spring中的Bean配置

    IOC&DI概述 OPC(Inversion of Control):其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源.作为回应,容器适时的返回资源.而应用了IOC ...

  9. Spring事务专题(四)Spring中事务的使用、抽象机制及模拟Spring事务实现

    Spring中事务的使用示例.属性及使用中可能出现的问题 前言 本专题大纲如下: 对于专题大纲我又做了调整哈,主要是希望专题的内容能够更丰富,更加详细,本来是想在源码分析的文章中附带讲一讲事务使用中的 ...

随机推荐

  1. Markdwon入门2

    插入表情 这里是指广义的表情包,包括表情.物体.动物等. :+1: :smile: :s :scream: :kissing_heart: :yum: :cry: :blush: :frog: :co ...

  2. Codeforces1114F Please, another Queries on Array?

    题目链接:http://codeforces.com/problemset/problem/1114/F 题意:序列$a$,有两种操作,1 区间里的数同时乘上$x$ 2 求区间的积的欧拉函数 线段树好 ...

  3. CSP-J2019游记&解题报告

    考前一天晚上失眠.......(其实主要不是因为考试的原因) 很幸运,我们学校就是一个考点,本场作战,应该有一点加持吧. 上午在家复习,看到一篇关于PN532模拟小米手环加密卡的文章,于是,,,,,, ...

  4. Tensorflow细节-P309-高维向量可视化

    import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import os from tensorflow ...

  5. learning svn diff --summarize

    # svn diff --summarizeA armbian-custom-dc/test/4g-power.shA armbian-custom-dc/test/4g-reset.shM armb ...

  6. telegraf 学习三 telegra inputs.net_response + smtp2http+ grafana 进行tcp服务状态监控

    以下演示一个简单的使用telegra inputs.net_response 进行tcp 服务状态的监控,统计集成grafana 的alert 为了方便使用了一个smtp2http 的服务,对于htt ...

  7. apisix 基于openresty 的api 网关

    apisix 是由openresty 团队开发并开源的微服务api gateway,还不错,官方文档也比较全,同时这个也是一个不错的学习openresty 的项目 以下为来自官方的架构图 插件加载 插 ...

  8. install_config

    #! /bin/bash REPO='10.10.238.114:4507' zabbix='10.10.238.110' osmaster=`cat /etc/redhat-release |awk ...

  9. Tomcat配置二级域名的分配与访问

    回顾tomcat Tomcat是Apache软件基金会(Apache Software Foundation)的一个顶级项目,由Apache, Sun和其他一些公司及个人共同开发,是目前比较流行的We ...

  10. fake_useragent 本地运行各种报错解决办法