@Value的两种方式

时间:2025-02-13 20:47:51
基于@Value进行注入时有两种方式,占位符和spel表达式 
//占位符方式
    @Value("${}")
    private String url;
    //SpEL表达方式,其中代表xml配置文件中的id值configProperties
    @Value("#{configProperties['']}")
    private String userName;
这两种方式需要在xml中配置时也是不一样的
<!--基于占位符方式 配置单个properties -->
    <!--<context:property-placeholder location="conf/"/>-->
    <!--基于占位符方式 配置多个properties -->
    <bean  class="">
        <property name="location" value="conf/"/>
    </bean>

    <!--基于SpEL表达式 配置多个properties id值为configProperties 提供java代码中使用 -->
    <bean  class="">
        <property name="locations">
            <list>
                <value>classpath:/conf/</value>
            </list>
        </property>
    </bean>

    <!--基于SpEL表达式 配置单个properties -->
    <!--<util:properties  location="classpath:conf/"/>-->