常用注解说明 @Value

目的:简化项目中properties配置文件的读取。
解决方案:Spring提供了@Value的方式读取,可以简化项目的配置。

1.@Value
Spring配置文件(方式一)

<context:property-placeholder  ignore-resource-not-found="true" local-override="true" location="classpath*:properties/*.properties" />`

Spring配置文件(方式-,使用绝对路径)

<context:property-placeholder
    location="file:${user.dir}/Notes/conf/datasource.properties,
              file:/Users/Notes/conf/db2.properties"
    ignore-unresolvable="true" />

Spring配置文件(方式二)

<bean id="placeholderConfigurer"        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config/application.properties</value>
            <value>classpath:config/redis.properties</value>
        </list>
    </property>
</bean>

java bean配置(方式一)

@Component("bean")
public class Bean {
    // properties配置文件的key: encrypt.desKey
    // #{null}} 如果配置文件没有key时,默认值是null;
    @Value("${encrypt.desKey:#{null}}")
    private String desKey;
    public String getDesKey() {
        return desKey;
    }
    public void setDesKey(String desKey) {
        this.desKey = desKey;
    }
}

javabean配置(方式二)

@Component("bean")
public class Bean {
    //spring的配置,bean Id='configProperties'
    @Value("#{configProperties['key']}")
    private String desKey;

    public String getDesKey() {
        return desKey;
    }
    public void setDesKey(String desKey) {
        this.desKey = desKey;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值