如果使用配置文件的话,可以直接使用 value
属性指定值。
<!-- 指定配置文件的位置 -->
<context:property-placeholder location="classpath:person.properties"/>
<bean class="top.wsuo.pojo.Person" id="person">
<property name="name" value="李四"/>
<property name="age" value="18"/>
</bean>
value 中可以是:
SpEL
表达式 #{}
;${}
;如果使用配置文件中的值,需要指定配置文件的位置,使用 context:property-placeholder
标签。
使用 @Value
注解同样可以实现相同的效果:
/*
* 使用 @Value 赋值
* 1.基本数值
* 2.可以使用 SpEL #{}
* 3.可以使用 ${}: 取出配置文件中的值(在运行环境变量)
* */
@Value("张四")
private String name;
@Value("#{20-2}")
private Integer age;
@Value("${person.nickName}")
private String neckName;
但是也需要指定配置文件的位置,在配置类中使用 @PropertySource
注解指定:
@Configuration
// 加载外部配置文件
@PropertySource({"classpath:person.properties"})
public class MainConfigOfPropertyValues {
@Bean
public Person person() {
return new Person();
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有