spring中引入property文件的三种方式

目前我了解的共有三种方式来引入property文件,必须要引入文件以后,才能使用${}的方式来获取其中的值

方式一:

在xml配置文件中加入

<bean id="pro" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            <property name="locations">

                    <list>

                            <value>classpath:user.property</value>

                    </list>

            </property>

</bean>

多个资源的时候,在list标签下面继续增加<value>即可

 

方式二:

在xml配置文件中加入

 <context:property-placeholder location="classpath:user.property,classpath:user2.property"/>,多个资源可以用逗号隔开写在一起

 

方式三:

在配置类上进行添加注解

@PropertySource("user.property")

或者

@PropertySources({@PropertySource("user.property"),@PropertySource("user1.property")})

 

然后结合@value取值即可

如下

//@PropertySource("user.property")

@PropertySources({@PropertySource("user.property"),@PropertySource("user1.property")})

@Configuration

public class PropertiesCOnfig {

@Bean

public TestConfig getTestConfig(@Value("${user.name}") String name,@Value("${user.age}") String age) {

TestConfig t=new TestConfig();

t.setName(name);

t.setAge(age);

return t;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值