开发过程中,有些配置信息是根据环境的不同而有不同的值。这个时候,我们需要获取配置文件中的值或者spring容器中的值,可以通过@value注解获取相关的值。

@Value(“#{}”)

通过@value获取springcontext容器中的值的信息。

@Value(“#{}”) 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

如果我们想通过@value获取spring容器中的值(包括bean和bean的属性值),我们可以通过@value("#{bean名称}")或者@value("#{bean名称.属性名}",该属性要有setter方法)

@Value(“#{}”) SpEL表达式(https://blog.csdn.net/ya_1249463314/article/details/68484422)

@Value(“#{}”) 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

@RestController
@RequestMapping("/login")
@Component
public class LoginController { @Value("#{1}")
private int number; //获取数字 1 @Value("#{'Spring Expression Language'}") //获取字符串常量
private String str; @Value("#{dataSource.url}") //获取bean的属性
private String jdbcUrl; @Autowired
private DataSourceTransactionManager transactionManager; @RequestMapping("login")
public String login(String name,String password) throws FileNotFoundException{
System.out.println(number);
System.out.println(str);
System.out.println(jdbcUrl);
return "login";
}
}

当bean通过@Value(#{“”}) 获取其他bean的属性,或者调用其他bean的方法时,只要该bean (Beab_A)能够访问到被调用的bean(Beab_B),即要么Beab_A 和Beab_B在同一个容器中,或者Beab_B所在容器是Beab_A所在容器的父容器。

(拿我上面贴出来的代码为例在springMvc项目中,dataSource这个bean一般是在springContext.xml文件中申明的,而loginController这个bean一般是在springMvc.xml文件中申明的,虽然这两个bean loginController和dataSource不在一个容器,但是loginController所在容器继承了dataSource所在的容器,所以在loginController这个bean中通过@Value(“#{dataSource.url}”)能够获取到dataSource的url属性)。

@Value(“${}”)

通过@value获取properties文件中的值的信息。

用 @Value(“${xxxx}”)注解从配置文件读取值的用法

如果我们想通过@value获取xx.properties配置文件中的某个key-value对的值,可以通过@value("${key}")获取其中的value值的信息

https://blog.csdn.net/zengdeqing2012/article/details/50736119

https://blog.csdn.net/jiangyu1013/article/details/56285984

1.用法

从配置properties文件中读取init.password 的值。

@Value("${init.password}")
private String initPwd;

2 . 在spring的配置文件中加载配置文件dbconfig.properties :

<!-- 加载配置文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="fileEncoding" value="UTF-8"/>
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>

或这样加载

<context:property-placeholder location="classpath:dbconfig.properties" />

或这样加载

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>dbconfig.properties</value>
</property>
</bean>

3 . dbconfig.properties 文件:

#MD5
password.algorithmName=md5
password.hashIterations=2
#initpwd
init.password=admin

通过@Value(“${}”) 可以获取对应属性文件中定义的属性值。

假如我有一个sys.properties文件 里面规定了一组值: web.view.prefix =/WEB-INF/views/

在springMvc.xml文件中引入下面的代码可以在该容器内通过@Value(“web.view.prefix")获取这个字符串。

<!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:sys.properties" />

然后再controller文件中通过下面代码即可获取/WEB-INF/views/这个字符串

@Value("${web.view.prefix}")
private String prefix;

需要指出的是,如果只在springMvc.xml引入下面代码,只能在springMvc.xml文件中扫描或者注册的bean中才能通过@Value("web.view.prefix")获取这个字符串。其他未在springMvc.xml扫描和定义的bean必须在相应的xml文件中引入下面代码才能使用@Value(“${}”)表达式

随机推荐

  1. 某企业桌面虚拟化项目-Citrix虚拟桌面解决方案

    xxx桌面虚拟化项目Citrix解决方案 xxx桌面虚拟化项目 Citrix解决方案 1 项目背景 秉承"尊重个性.创造价值.贡献于社会"的企业理念和开拓创新的精神,xxx所制造. ...

  2. @RequestAttribute与@MatrixVariable

    @RequestAttribute 它只能使用在方法入参上,从request请求参数中获取对应的属性值. //路径跳转 @GetMapping("/goto") public St ...

  3. 讲分布式唯一id,这篇文章很实在

    分布式唯一ID介绍 分布式系统全局唯一的 id 是所有系统都会遇到的场景,往往会被用在搜索,存储方面,用于作为唯一的标识或者排序,比如全局唯一的订单号,优惠券的券码等,如果出现两个相同的订单号,对于用 ...

  4. Part 28 AngularJS default route

    At the moment the problem is that, if you try to navigate to a route that is not configured, you wil ...

  5. 华为开发者大会主题演讲:3D建模服务让内容高效生产

    内容来源:华为开发者大会2021 HMS Core 6 Graphics技术论坛,主题演讲<3D建模服务使能3D内容高效生产>. 演讲嘉宾:华为消费者云服务 AI算法专家 3D建模服务(3 ...

  6. ajax的get方法获取豆瓣电影前10页的数据

    # _*_ coding : utf-8 _*_ # @Time : 2021/11/2 11:45 # @Author : 秋泊酱 # 1页数据 电影条数20 # https://movie.dou ...

  7. Python 爬取 猫眼

    1. import requests import re import pymongo MONGO_URL='localhost'#建立连接 MONGO_DB='Maoyan'#创建数据库 clien ...

  8. 线性规划之单纯形算法矩阵描述与python实现

    声明 本文为本人原创,转载请注明出处.本文仅发表在博客园,作者LightningStar. 问题描述 所有的线性规划问题都可以归约到标准型的问题,规约过程比较简单且已经超出本文范围,不再描述,可以参考 ...

  9. C#中指针的使用(转)

    在C#中,有时候希望通过指针来操作内存,这样可以提高效率.我们可以用unsafe关键字修饰含有指针操作的程序段,如下所示: class Program {   static int Main(stri ...

  10. 【原创】【自制系列】自制stack类型(泛型)

    前言 自制类型的第三篇,stack类型.stack是指栈,其实我个人认为stack是最好写的类型,没有之一.关于queue类型需要涉及到循环队列避免浪费内存,但是stack的插入删除都是对于栈顶而言, ...