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>
Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。
使用方式必须在当前类使用@Component,xml文件内配置的是通过pakage扫描方式,
例如:<context:component-scan base-package="pakage_name" />
- 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