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已配置好上述一切

  • 相关阅读:
    mysql-主主配置
    PHP安装-centos7
    mysql-M-S-S模型 中继器 级联
    安装mysql数据库-centos7
    正则表达式
    DJango安装-windows
    flask安装
    python安装centos7
    Linux——C库
    文件I/O
  • 原文地址:https://www.cnblogs.com/yanze/p/10641196.html
Copyright © 2011-2022 走看看