SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value
一、用placeholder给bean运行时注入值的步骤
Spring取得placeholder的值是用${...}
1.声明placeholder bean
(1)java方式
In order to use placeholder values, you must configure either a PropertyPlaceholder-
Configurer bean or a PropertySourcesPlaceholderConfigurer bean. Starting with
Spring 3.1, PropertySourcesPlaceholderConfigurer is preferred because it resolves
placeholders against the Spring Environment and its set of property sources.
The following @Bean method configures PropertySourcesPlaceholderConfigurer
in Java configuration:
@Bean
public
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
(2)xml方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:property-placeholder
location="com/soundsystem/app.properties" /> <bean class="com.soundsystem.BlankDisc"
c:_0 = "${disc.title}"
c:_1 = "${disc.artist}"/> </beans>
2.注入
1.在java文件中用 @Value
如构造函数
public BlankDisc(
@Value("${disc.title}") String title,
@Value("${disc.artist}") String artist) {
this.title = title;
this.artist = artist;
}
2.xml
<bean id="sgtPeppers"
class="soundsystem.BlankDisc"
c:_title="${disc.title}"
c:_artist="${disc.artist}" />
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-007-给BEAN运行时注入值placeholder、@Value的更多相关文章
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)
一. 直观的给bean注入值如下: @Bean public CompactDisc sgtPeppers() { return new BlankDisc( "Sgt. Pepper's ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍
一. 1.SpEL expressions are framed with #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-005-Bean的作用域@Scope、ProxyMode
一. Spring的bean默认是单例的 But sometimes you may find yourself working with a mutable class that does main ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary
一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
一. Spring honors two separate properties when determining which profiles are active:spring.profiles. ...
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值
1.When injecting properties and constructor arguments on beans that are created via component-scanni ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除BEAN自动装配的歧义@QUALIFIER及自定义注解
一. The @Qualifier annotation is the main way to work with qualifiers. It can beapplied alongside @Au ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
一.用@Conditional根据条件决定是否要注入bean 1. package com.habuma.restfun; public class MagicBean { } 2. package ...
随机推荐
- c# xml 解析取值
//字符串 string result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>< ...
- SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by Failed resolution of: Lcom/duanqu/qupai/recorder/R$array;
紧急解决办法: 将你的oneSDK(qupaiSDK)里面的manifest的pageckage改为:com.duanqu.qupai.recorder
- mysqlimport 导入文件到数据库命令
mysqlimport -h 172.16.145.125 -u ocetl -pocetl test --fields-terminated-by='|' '/home/ocetl/tmp_use ...
- eclipse中启动Genymotion模拟器的错误
错误程序: Output file: C:\Users\wishwzp\.genymotion-eclipse.logLoading Genymotion libraryGenymotion dire ...
- string[] 和 arraylist互转及问题解决
1,String 数组转成 list<String> String[] s={"1","2","3","5" ...
- [python] 高效使用assert
Places to consider putting assertions: checking parameter types, classes, or values checking data st ...
- bzoj1002:[FJOI2007]轮状病毒
思路:一道很裸的生成树计数问题,然而要高精度,而且听说直接行列式求值会被卡精度,所以可以模拟行列式求值的过程得到递推公式:f[i]=3*f[i-1]-f[i-2]+2,证明详见vfk博客: http: ...
- busbox编译出错,arm-linux-未找到命令
1.问题:/opt/FriendlyARM/mini6410/linux/busybox-1.17.2/scripts/gcc-version.sh: 行 11: arm-linux-gcc: 未找到 ...
- 【转】C#.net拖拽实现获得文件路径
C#.net拖拽实现获得文件路径 作者Attilax , EMAIL:1466519819@qq.com 思路: 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等), ...
- inline-block的兼容性问题
inline-block的兼容性问题 Inline-block是元素display属性的一个值.这个名字的由来是因为,display设置这个值的元素,兼具行内元素( inline elements)跟 ...