Spring 注解@Value详解
一、spring(基础10) 注解@Value详解[1]
一 配置方式
@value需要参数,这里参数可以是两种形式:
- @Value("#{configProperties['t1.msgname']}")或者@Value("${t1.msgname}");
这两形式,在配置上有什么区别:
1、@Value("#{configProperties['t1.msgname']}")这种形式的配置中有“configProperties”,其实它指定的是配置文件的加载对象:配置如下:
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath:/config/t1.properties</value>
- </list>
- </property>
- </bean>
这样配置就可完成对属性的具体注入了;
2、@Value("${t1.msgname}")这种形式不需要指定具体加载对象,这时候需要一个关键的对象来完成PreferencesPlaceholderConfigurer,
这个对象的配置可以利用上面配置1中的配置,也可以自己直接自定配置文件路径。
如果使用配置1中的配置,可以写成如下情况:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties"/>
- </bean>
如果直接指定配置文件的话,可以写成如下情况:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="location">
- <value>config/t1.properties</value>
- </property>
- </bean>
二 用法
- package com.jumore.finance.pay.request.lianlian;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- /**
- * 连连请求参数基类
- * @ClassName: LianLianBaseRequest
- * @Description:
- * @Author: zdq
- * @Date: 2015年10月28日
- */
- @Component("lianLianBaseRequest")
- public class LianLianBaseRequest {
- /** 商户号 **/
- @Value("#{configProperties['lianlian.oidPartner']}")
- private String oid_partner;
- /** 加密key **/
- @Value("#{configProperties['lianlian.MD5Key']}")
- private String md5_key;
- /** 连连公钥 **/
- @Value("#{configProperties['lianlian.lianlianPubKey']}")
- private String lianlian_pub_key;
- /** 公钥 **/
- @Value("#{configProperties['lianlian.pubKey']}")
- private String pub_key;
- /** 私钥 **/
- @Value("#{configProperties['lianlian.priKey']}")
- private String pri_key;
- /** 参数字符集编码 **/
- @Value("#{configProperties['lianlian.inputCharset']}")
- private String charset_name;
- /** 版本 **/
- @Value("#{configProperties['lianlian.version']}")
- private String version;
- /** 签名类型 **/
- @Value("#{configProperties['lianlian.signType']}")
- private String sign_type;
- /** 业务类型 **/
- @Value("#{configProperties['lianlian.busiPartner']}")
- private String busi_partner;
- /** 订单有效时间(分钟) **/
- @Value("#{configProperties['lianlian.validOrder']}")
- private String valid_order;
- // 认证支付
- @Value("#{configProperties['lianlian.authPay.payUrl']}")
- private String authPay_url;// 认证支付地址
- @Value("#{configProperties['lianlian.authPay.notifyUrl']}")
- private String authPay_notify_url;// 服务器异步通知地址
- @Value("#{configProperties['lianlian.authPay.returnUrl']}")
- private String authPay_url_return;// 支付结束同步页面
- @Value("#{configProperties['lianlian.authPay.backUrl']}")
- private String authPay_back_url;// 认证支付修改银行卡页面
- // 网关充值
- @Value("#{configProperties['lianlian.gateway.payUrl']}")
- private String gateway_url;// 网关支付地址,必须
- @Value("#{configProperties['lianlian.gatewayPay.notifyUrl']}")
- private String gateway_notify_url;// 服务器异步通知地址[个人]
- @Value("#{configProperties['lianlian.gatewayPay.returnUrl']}")
- private String gateway_url_return;// 支付结束同步页面[个人]
- @Value("#{configProperties['lianlian.gatewayPay.qyNotifyUrl']}")
- private String gateway_qyNotify_url;// 服务器异步通知地址[企业]
- @Value("#{configProperties['lianlian.gatewayPay.qyReturnUrl']}")
- private String gateway_url_qyReturn;// 支付结束同步页面[企业]
- @Value("#{configProperties['lianlian.gatewayPayAndBuy.notifyUrl']}")
- private String buy_gateway_notify_url;// 服务器异步通知地址[个人购买充值]
- @Value("#{configProperties['lianlian.gatewayPayAndBuy.returnUrl']}")
- private String buy_gateway_url_return;// 支付结束同步页面[个人购买充值]
- // 提现代发
- @Value("#{configProperties['lianlian.cash.distributeUrl']}")
- private String distribute_url;// 代发地址
- @Value("#{configProperties['lianlian.distribute.notifyUrl']}")
- private String distribute_notify_url;// 服务器异步通知地址
- @Value("#{configProperties['lianlian.apiVersion']}")
- private String distribute_api_version;
- // 支行信息
- @Value("#{configProperties['lianlian.cash.queryBankBranchUrl']}")
- private String distribute_bankBranch_url;// 地址
- // 订单查询
- @Value("#{configProperties['lianlian.base.queryOrderUrl']}")
- private String queryOrder_url;
- public String getGateway_qyNotify_url() {
- return gateway_qyNotify_url;
- }
- public void setGateway_qyNotify_url(String gateway_qyNotify_url) {
- this.gateway_qyNotify_url = gateway_qyNotify_url;
- }
- public String getGateway_url_qyReturn() {
- return gateway_url_qyReturn;
- }
- public void setGateway_url_qyReturn(String gateway_url_qyReturn) {
- this.gateway_url_qyReturn = gateway_url_qyReturn;
- }
- public String getBuy_gateway_notify_url() {
- return buy_gateway_notify_url;
- }
- public void setBuy_gateway_notify_url(String buy_gateway_notify_url) {
- this.buy_gateway_notify_url = buy_gateway_notify_url;
- }
- public String getBuy_gateway_url_return() {
- return buy_gateway_url_return;
- }
- public void setBuy_gateway_url_return(String buy_gateway_url_return) {
- this.buy_gateway_url_return = buy_gateway_url_return;
- }
- public String getOid_partner() {
- return oid_partner;
- }
- public void setOid_partner(String oid_partner) {
- this.oid_partner = oid_partner;
- }
- public String getMd5_key() {
- return md5_key;
- }
- public void setMd5_key(String md5_key) {
- this.md5_key = md5_key;
- }
- public String getLianlian_pub_key() {
- return lianlian_pub_key;
- }
- public void setLianlian_pub_key(String lianlian_pub_key) {
- this.lianlian_pub_key = lianlian_pub_key;
- }
- public String getPub_key() {
- return pub_key;
- }
- public void setPub_key(String pub_key) {
- this.pub_key = pub_key;
- }
- public String getPri_key() {
- return pri_key;
- }
- public void setPri_key(String pri_key) {
- this.pri_key = pri_key;
- }
- public String getCharset_name() {
- return charset_name;
- }
- public void setCharset_name(String charset_name) {
- this.charset_name = charset_name;
- }
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- public String getSign_type() {
- return sign_type;
- }
- public void setSign_type(String sign_type) {
- this.sign_type = sign_type;
- }
- public String getBusi_partner() {
- return busi_partner;
- }
- public void setBusi_partner(String busi_partner) {
- this.busi_partner = busi_partner;
- }
- public String getValid_order() {
- return valid_order;
- }
- public void setValid_order(String valid_order) {
- this.valid_order = valid_order;
- }
- public String getAuthPay_url() {
- return authPay_url;
- }
- public void setAuthPay_url(String authPay_url) {
- this.authPay_url = authPay_url;
- }
- public String getAuthPay_notify_url() {
- return authPay_notify_url;
- }
- public void setAuthPay_notify_url(String authPay_notify_url) {
- this.authPay_notify_url = authPay_notify_url;
- }
- public String getAuthPay_url_return() {
- return authPay_url_return;
- }
- public void setAuthPay_url_return(String authPay_url_return) {
- this.authPay_url_return = authPay_url_return;
- }
- public String getAuthPay_back_url() {
- return authPay_back_url;
- }
- public void setAuthPay_back_url(String authPay_back_url) {
- this.authPay_back_url = authPay_back_url;
- }
- public String getGateway_url() {
- return gateway_url;
- }
- public void setGateway_url(String gateway_url) {
- this.gateway_url = gateway_url;
- }
- public String getGateway_notify_url() {
- return gateway_notify_url;
- }
- public void setGateway_notify_url(String gateway_notify_url) {
- this.gateway_notify_url = gateway_notify_url;
- }
- public String getGateway_url_return() {
- return gateway_url_return;
- }
- public void setGateway_url_return(String gateway_url_return) {
- this.gateway_url_return = gateway_url_return;
- }
- public String getDistribute_url() {
- return distribute_url;
- }
- public void setDistribute_url(String distribute_url) {
- this.distribute_url = distribute_url;
- }
- public String getDistribute_notify_url() {
- return distribute_notify_url;
- }
- public void setDistribute_notify_url(String distribute_notify_url) {
- this.distribute_notify_url = distribute_notify_url;
- }
- public String getDistribute_api_version() {
- return distribute_api_version;
- }
- public void setDistribute_api_version(String distribute_api_version) {
- this.distribute_api_version = distribute_api_version;
- }
- public String getDistribute_bankBranch_url() {
- return distribute_bankBranch_url;
- }
- public void setDistribute_bankBranch_url(String distribute_bankBranch_url) {
- this.distribute_bankBranch_url = distribute_bankBranch_url;
- }
- public String getQueryOrder_url() {
- return queryOrder_url;
- }
- public void setQueryOrder_url(String queryOrder_url) {
- this.queryOrder_url = queryOrder_url;
- }
- }
xml配置:
- <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
- <context:component-scan base-package="com.jumore.finance.pay.request"/>
- <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="locations">
- <list>
- <value>classpath*:conf/lianlian.properties</value>
- </list>
- </property>
- </bean>
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
- <property name="properties" ref="configProperties" />
- </bean>
properties配置:
二、官方文档[2]
Reference:
[1] 小米加大炮, spring(基础10) 注解@Value详解, http://blog.csdn.net/zengdeqing2012/article/details/50736119
[2] Rod Johnson. Juergen Hoeller, Spring Framework Reference Documentation, 4,1,6,RELEASE:119
Spring 注解@Value详解的更多相关文章
- Spring注解_详解
@Autowired 注释 将 @Autowired 注释标注在成员变量上 import org.springframework.beans.factory.annotation.Autowire ...
- Spring注解标签详解@Autowired @Qualifier等 @Slf4j
@Slf4j @Slf4j注解实现日志输出 自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTes ...
- Spring注解标签详解@Autowired @Qualifier等
http://blog.csdn.net/wangsr4java/article/details/42777855 @Component.@Repository.@Service.@Controlle ...
- spring事务配置详解
一.前言 好几天没有在对spring进行学习了,由于这几天在赶项目,没有什么时间闲下来继续学习,导致spring核心架构详解没有继续下去,在接下来的时间里面,会继续对spring的核心架构在继续进行学 ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- spring事务管理(详解和实例)
原文地址: 参考地址:https://blog.csdn.net/yuanlaishini2010/article/details/45792069 写这篇博客之前我首先读了<Spring in ...
- Spring DI使用详解
Spring DI使用详解 一.介绍 DI的定义:依赖注入,为类里面的属性设值.例如,我们之前的setName方法就是在为name属性设值. IOC与DI的关系:IOC进行对象的创建,DI进行值的注入 ...
- 【SSM框架】Spring笔记 --- 事务详解
1.Spring的事务管理: 事务原本是数据库中的概念,在实际项目的开发中,进行事务的处理一般是在业务逻辑层, 即 Service 层.这样做是为了能够使用事务的特性来管理关联操作的业务. 在 Spr ...
随机推荐
- Sass和Less的区别?
这篇文章主要解答以下几个问题,供前端开发者的新手参考. 1.什么是Sass和Less? 2.为什么要使用CSS预处理器? 3.Sass和Less的比较 4.为什么选择使用Sass而不是Less? 什么 ...
- 使用SAP CRM中间件XIF(External Interface)一步步创建服务订单
tcode WE19, choose an existing IDOC in the system: Just change the existing IDOC Service Order ID to ...
- 从CMS到G1:LinkedIn个人主页调优实战
本文转载自公众号:阿飞的博客,阅读大约需要13分钟.阿飞是我认识几年的好友,对技术有很强的专研精神,跟他讨论GC问题的时候一些观点都很深刻. LinkedIn中的个人主页是访问量最多的页面之一,它允许 ...
- springboot-发布jar包
其他参考链接: https://www.cnblogs.com/blog5277/p/5920560.html 环境变量配置: 新建系统变量MAVEN_HOME: 在path中添加: ;%MAVEN_ ...
- PHP的SPL标准库
1,简介 SPL,全称 Standard PHP Library 中文是 标准PHP类库.是php内置的一些拓展类和拓展接口,其内容包含数据结构.迭代器.接口.异常.SPL函数,文件处理等内容.SPL ...
- linux/unix发行清单
unix http://www.slackware.com/ https://www.freebsd.org/ http://www.netbsd.org/ https://www.opensuse. ...
- Jenkins实用发布与回滚PHP项目生产实践
目录 1.概述 2.项目实践 2.1.环境说明 2.2.Jenkins配置 2.2.1.修改Jenkins的运行用户 2.2.2.配置Jenkins用户和Gitlab的ssh-key 2.2.3.Je ...
- Sublime Text3 安装 CTags 插件出现乱码
1.下载ctags.exe 可以直接下载我上传好的资源:http://download.csdn.net/download/zhaoxd200808501/9971251.或者网络上其他地方也可以下载 ...
- python中的exec()函数和eval()函数
exec()函数 exec函数用于执行存储在字符串中的python语句 >>> exec("x=1") >>> x 但有时候,直接这样执行可能会 ...
- LINUX基础学习之基础命令(3)--2019-11-22
1.命令行展开 ~ USERNAME:展开用户的主目录 [root@CentOS-7-43 ~]# ls ~roo 公共 模板 视频 图片 文档 下载 音乐 桌面 {}:可承载一个以逗号分隔的列表 ...