zoukankan      html  css  js  c++  java
  • Spring 注解@Value详解

    一、spring(基础10) 注解@Value详解[1]

    一 配置方式

    @value需要参数,这里参数可以是两种形式:

    [html] view plain copy
    1. @Value("#{configProperties['t1.msgname']}")或者@Value("${t1.msgname}");  

    这两形式,在配置上有什么区别:

    1、@Value("#{configProperties['t1.msgname']}")这种形式的配置中有“configProperties”,其实它指定的是配置文件的加载对象:配置如下:

    [html] view plain copy
    1. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
    2.         <property name="locations">  
    3.             <list>  
    4.                 <value>classpath:/config/t1.properties</value>  
    5.             </list>  
    6.         </property>  
    7.     </bean>  
    这样配置就可完成对属性的具体注入了;

    2、@Value("${t1.msgname}")这种形式不需要指定具体加载对象,这时候需要一个关键的对象来完成PreferencesPlaceholderConfigurer,

    这个对象的配置可以利用上面配置1中的配置,也可以自己直接自定配置文件路径。
        如果使用配置1中的配置,可以写成如下情况:

    [html] view plain copy
    1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
    2.         <property name="properties" ref="configProperties"/>  
    3.     </bean>  
    如果直接指定配置文件的话,可以写成如下情况:

    [html] view plain copy
    1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
    2.         <property name="location">  
    3.         <value>config/t1.properties</value>  
    4.         </property>  
    5.     </bean>  
    二 用法

    Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。
    使用方式必须在当前类使用@Component,xml文件内配置的是通过pakage扫描方式,
    例如:<context:component-scan base-package="pakage_name" />
    [html] view plain copy
    1. package com.jumore.finance.pay.request.lianlian;  
    2.   
    3. import org.springframework.beans.factory.annotation.Value;  
    4. import org.springframework.stereotype.Component;  
    5.   
    6. /**  
    7.  * 连连请求参数基类  
    8.  * @ClassName: LianLianBaseRequest  
    9.  * @Description:  
    10.  * @Author: zdq  
    11.  * @Date: 2015年10月28日  
    12.  */  
    13. @Component("lianLianBaseRequest")  
    14. public class LianLianBaseRequest {  
    15.   
    16.     /** 商户号 **/  
    17.     @Value("#{configProperties['lianlian.oidPartner']}")  
    18.     private String oid_partner;  
    19.   
    20.     /** 加密key **/  
    21.     @Value("#{configProperties['lianlian.MD5Key']}")  
    22.     private String md5_key;  
    23.   
    24.     /** 连连公钥 **/  
    25.     @Value("#{configProperties['lianlian.lianlianPubKey']}")  
    26.     private String lianlian_pub_key;  
    27.   
    28.     /** 公钥 **/  
    29.     @Value("#{configProperties['lianlian.pubKey']}")  
    30.     private String pub_key;  
    31.   
    32.     /** 私钥 **/  
    33.     @Value("#{configProperties['lianlian.priKey']}")  
    34.     private String pri_key;  
    35.   
    36.     /** 参数字符集编码 **/  
    37.     @Value("#{configProperties['lianlian.inputCharset']}")  
    38.     private String charset_name;  
    39.   
    40.     /** 版本 **/  
    41.     @Value("#{configProperties['lianlian.version']}")  
    42.     private String version;  
    43.   
    44.     /** 签名类型 **/  
    45.     @Value("#{configProperties['lianlian.signType']}")  
    46.     private String sign_type;  
    47.   
    48.     /** 业务类型 **/  
    49.     @Value("#{configProperties['lianlian.busiPartner']}")  
    50.     private String busi_partner;  
    51.   
    52.     /** 订单有效时间(分钟) **/  
    53.     @Value("#{configProperties['lianlian.validOrder']}")  
    54.     private String valid_order;  
    55.       
    56.     // 认证支付  
    57.     @Value("#{configProperties['lianlian.authPay.payUrl']}")  
    58.     private String authPay_url;// 认证支付地址  
    59.       
    60.     @Value("#{configProperties['lianlian.authPay.notifyUrl']}")  
    61.     private String authPay_notify_url;// 服务器异步通知地址  
    62.       
    63.     @Value("#{configProperties['lianlian.authPay.returnUrl']}")  
    64.     private String authPay_url_return;// 支付结束同步页面  
    65.       
    66.     @Value("#{configProperties['lianlian.authPay.backUrl']}")  
    67.     private String authPay_back_url;// 认证支付修改银行卡页面  
    68.       
    69.     // 网关充值  
    70.     @Value("#{configProperties['lianlian.gateway.payUrl']}")  
    71.     private String gateway_url;// 网关支付地址,必须  
    72.       
    73.     @Value("#{configProperties['lianlian.gatewayPay.notifyUrl']}")  
    74.     private String gateway_notify_url;// 服务器异步通知地址[个人]  
    75.       
    76.     @Value("#{configProperties['lianlian.gatewayPay.returnUrl']}")  
    77.     private String gateway_url_return;// 支付结束同步页面[个人]  
    78.       
    79.     @Value("#{configProperties['lianlian.gatewayPay.qyNotifyUrl']}")  
    80.     private String gateway_qyNotify_url;// 服务器异步通知地址[企业]  
    81.       
    82.     @Value("#{configProperties['lianlian.gatewayPay.qyReturnUrl']}")  
    83.     private String gateway_url_qyReturn;// 支付结束同步页面[企业]  
    84.       
    85.     @Value("#{configProperties['lianlian.gatewayPayAndBuy.notifyUrl']}")  
    86.     private String buy_gateway_notify_url;// 服务器异步通知地址[个人购买充值]  
    87.       
    88.     @Value("#{configProperties['lianlian.gatewayPayAndBuy.returnUrl']}")  
    89.     private String buy_gateway_url_return;// 支付结束同步页面[个人购买充值]  
    90.       
    91.     // 提现代发  
    92.     @Value("#{configProperties['lianlian.cash.distributeUrl']}")  
    93.     private String distribute_url;// 代发地址  
    94.       
    95.     @Value("#{configProperties['lianlian.distribute.notifyUrl']}")  
    96.     private String distribute_notify_url;// 服务器异步通知地址  
    97.       
    98.     @Value("#{configProperties['lianlian.apiVersion']}")  
    99.     private String distribute_api_version;  
    100.       
    101.     // 支行信息  
    102.     @Value("#{configProperties['lianlian.cash.queryBankBranchUrl']}")  
    103.     private String distribute_bankBranch_url;// 地址  
    104.       
    105.     // 订单查询  
    106.     @Value("#{configProperties['lianlian.base.queryOrderUrl']}")  
    107.     private String queryOrder_url;  
    108.       
    109.     public String getGateway_qyNotify_url() {  
    110.         return gateway_qyNotify_url;  
    111.     }  
    112.   
    113.     public void setGateway_qyNotify_url(String gateway_qyNotify_url) {  
    114.         this.gateway_qyNotify_url = gateway_qyNotify_url;  
    115.     }  
    116.   
    117.     public String getGateway_url_qyReturn() {  
    118.         return gateway_url_qyReturn;  
    119.     }  
    120.   
    121.     public void setGateway_url_qyReturn(String gateway_url_qyReturn) {  
    122.         this.gateway_url_qyReturn = gateway_url_qyReturn;  
    123.     }  
    124.       
    125.     public String getBuy_gateway_notify_url() {  
    126.         return buy_gateway_notify_url;  
    127.     }  
    128.   
    129.     public void setBuy_gateway_notify_url(String buy_gateway_notify_url) {  
    130.         this.buy_gateway_notify_url = buy_gateway_notify_url;  
    131.     }  
    132.   
    133.     public String getBuy_gateway_url_return() {  
    134.         return buy_gateway_url_return;  
    135.     }  
    136.   
    137.     public void setBuy_gateway_url_return(String buy_gateway_url_return) {  
    138.         this.buy_gateway_url_return = buy_gateway_url_return;  
    139.     }  
    140.   
    141.     public String getOid_partner() {  
    142.         return oid_partner;  
    143.     }  
    144.   
    145.     public void setOid_partner(String oid_partner) {  
    146.         this.oid_partner = oid_partner;  
    147.     }  
    148.   
    149.     public String getMd5_key() {  
    150.         return md5_key;  
    151.     }  
    152.   
    153.     public void setMd5_key(String md5_key) {  
    154.         this.md5_key = md5_key;  
    155.     }  
    156.   
    157.     public String getLianlian_pub_key() {  
    158.         return lianlian_pub_key;  
    159.     }  
    160.   
    161.     public void setLianlian_pub_key(String lianlian_pub_key) {  
    162.         this.lianlian_pub_key = lianlian_pub_key;  
    163.     }  
    164.   
    165.     public String getPub_key() {  
    166.         return pub_key;  
    167.     }  
    168.   
    169.     public void setPub_key(String pub_key) {  
    170.         this.pub_key = pub_key;  
    171.     }  
    172.   
    173.     public String getPri_key() {  
    174.         return pri_key;  
    175.     }  
    176.   
    177.     public void setPri_key(String pri_key) {  
    178.         this.pri_key = pri_key;  
    179.     }  
    180.   
    181.     public String getCharset_name() {  
    182.         return charset_name;  
    183.     }  
    184.   
    185.     public void setCharset_name(String charset_name) {  
    186.         this.charset_name = charset_name;  
    187.     }  
    188.   
    189.     public String getVersion() {  
    190.         return version;  
    191.     }  
    192.   
    193.     public void setVersion(String version) {  
    194.         this.version = version;  
    195.     }  
    196.   
    197.     public String getSign_type() {  
    198.         return sign_type;  
    199.     }  
    200.   
    201.     public void setSign_type(String sign_type) {  
    202.         this.sign_type = sign_type;  
    203.     }  
    204.   
    205.     public String getBusi_partner() {  
    206.         return busi_partner;  
    207.     }  
    208.   
    209.     public void setBusi_partner(String busi_partner) {  
    210.         this.busi_partner = busi_partner;  
    211.     }  
    212.   
    213.     public String getValid_order() {  
    214.         return valid_order;  
    215.     }  
    216.   
    217.     public void setValid_order(String valid_order) {  
    218.         this.valid_order = valid_order;  
    219.     }  
    220.   
    221.     public String getAuthPay_url() {  
    222.         return authPay_url;  
    223.     }  
    224.   
    225.     public void setAuthPay_url(String authPay_url) {  
    226.         this.authPay_url = authPay_url;  
    227.     }  
    228.   
    229.     public String getAuthPay_notify_url() {  
    230.         return authPay_notify_url;  
    231.     }  
    232.   
    233.     public void setAuthPay_notify_url(String authPay_notify_url) {  
    234.         this.authPay_notify_url = authPay_notify_url;  
    235.     }  
    236.   
    237.     public String getAuthPay_url_return() {  
    238.         return authPay_url_return;  
    239.     }  
    240.   
    241.     public void setAuthPay_url_return(String authPay_url_return) {  
    242.         this.authPay_url_return = authPay_url_return;  
    243.     }  
    244.   
    245.     public String getAuthPay_back_url() {  
    246.         return authPay_back_url;  
    247.     }  
    248.   
    249.     public void setAuthPay_back_url(String authPay_back_url) {  
    250.         this.authPay_back_url = authPay_back_url;  
    251.     }  
    252.   
    253.     public String getGateway_url() {  
    254.         return gateway_url;  
    255.     }  
    256.   
    257.     public void setGateway_url(String gateway_url) {  
    258.         this.gateway_url = gateway_url;  
    259.     }  
    260.   
    261.     public String getGateway_notify_url() {  
    262.         return gateway_notify_url;  
    263.     }  
    264.   
    265.     public void setGateway_notify_url(String gateway_notify_url) {  
    266.         this.gateway_notify_url = gateway_notify_url;  
    267.     }  
    268.   
    269.     public String getGateway_url_return() {  
    270.         return gateway_url_return;  
    271.     }  
    272.   
    273.     public void setGateway_url_return(String gateway_url_return) {  
    274.         this.gateway_url_return = gateway_url_return;  
    275.     }  
    276.   
    277.     public String getDistribute_url() {  
    278.         return distribute_url;  
    279.     }  
    280.   
    281.     public void setDistribute_url(String distribute_url) {  
    282.         this.distribute_url = distribute_url;  
    283.     }  
    284.   
    285.     public String getDistribute_notify_url() {  
    286.         return distribute_notify_url;  
    287.     }  
    288.   
    289.     public void setDistribute_notify_url(String distribute_notify_url) {  
    290.         this.distribute_notify_url = distribute_notify_url;  
    291.     }  
    292.   
    293.     public String getDistribute_api_version() {  
    294.         return distribute_api_version;  
    295.     }  
    296.   
    297.     public void setDistribute_api_version(String distribute_api_version) {  
    298.         this.distribute_api_version = distribute_api_version;  
    299.     }  
    300.   
    301.     public String getDistribute_bankBranch_url() {  
    302.         return distribute_bankBranch_url;  
    303.     }  
    304.   
    305.     public void setDistribute_bankBranch_url(String distribute_bankBranch_url) {  
    306.         this.distribute_bankBranch_url = distribute_bankBranch_url;  
    307.     }  
    308.   
    309.     public String getQueryOrder_url() {  
    310.         return queryOrder_url;  
    311.     }  
    312.   
    313.     public void setQueryOrder_url(String queryOrder_url) {  
    314.         this.queryOrder_url = queryOrder_url;  
    315.     }  
    316.   
    317. }  

    xml配置:
    [html] view plain copy
    1. <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
    2.     <context:component-scan base-package="com.jumore.finance.pay.request"/>  
    3.       
    4.     <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
    5.         <property name="locations">  
    6.             <list>  
    7.                 <value>classpath*:conf/lianlian.properties</value>  
    8.             </list>  
    9.         </property>  
    10.     </bean>  
    11.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
    12.         <property name="properties" ref="configProperties" />  
    13.     </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

  • 相关阅读:
    .net 关于路径的总结
    asp.net Base64加解密
    asp.net中的<% %>,<%= %>,<%# %><%$ %>的使用
    asp.net Swiper 轮播动画
    ASP.NET中Literal控件的使用方法(用于向网页中动态添加内容)
    asp.net 获取表单中控件的值
    rgb值转换成16进制
    关于background全解
    移动端的性能陷阱
    原生JS实现雪花特效
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104133.html
Copyright © 2011-2022 走看看