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

    本文参考自: https://blog.csdn.net/ryelqy/article/details/77453713

    @Value能让我们在java代码中使用property文件的属性,使用@Value有两种形式:

    1、@Value("#{configProperties['t1.msgname']}")

    2、@Value("${t1.msgname}")

    这两种形式,最大的区别在于配置:

    1、

    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
        <property name="locations">  
            <list>  
                <value>classpath:/config/t1.properties</value>  
            </list>  
        </property>  
    </bean>

    备注:configProperties是此bean的id,可自定义,并不是说一定得是configProperties

    PropertiesFactoryBean 加载Properties文件的工厂类

    2、@Value("${t1.msgname}")的配置既可以在1的基础上扩展,也能完全自己配置

    扩展方式:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
        <property name="properties" ref="configProperties"/>  //1中bean的id
    </bean>  

    备注:核心是PreferencePlaceholderConfigurer 可以理解为最优先的选择

    自己配置:

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

    ------------------------------------------------------

    前提:

    1、当前类使用@Component或由@Component扩展开来的注解,

    2、xml文件内配置了是通过pakage扫描包

    <context:component-scan base-package="pakage_name"/>

    -------------------------------------------------------

    spring boot中使用

    spring boot中,我们只需要在application.properties中添加属性即可使用,spring boot已配置好上述一切

  • 相关阅读:
    LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    RTSP可用网络流
    Linux访问Github缓慢
    Ubu18.0-NVIDIA显卡驱动重装
    FFMPEG第一次学习
    QT-守护程序
    QT-局域网探测工具(简易版)--Ping
    QT-notepad++仿写
    Ubuntu 解压文件
    Ubuntu -换源
  • 原文地址:https://www.cnblogs.com/yanze/p/10641196.html
Copyright © 2011-2022 走看看