直接注入
Bean直接注入示例:
@Bean("blankDisc1")
@Profile("dev")
@Primary
public BlankDisc getDevBlankDiscPrimary() {
BlankDisc blankDisc = new BlankDisc();
blankDisc.setTitle("Sgt. Pepper's Lonely Hearts Club Band dev Primary");
List<String> tracks = new ArrayList<>();
tracks.add("Sgt. Pepper's Lonely Hearts Club Band track dev Primary");
blankDisc.setTracks(tracks);
return blankDisc;
}
XML直接注入示例:
<bean id="blankDisc"
class="com.zachary.soundsystem.impl.BlankDisc"
p:title="Sgt. Pepper's Lonely Hearts Club Band"
p:tracks-ref="trackList" />
注入外部的值
创建app.properties文件
disc.title=Sgt. Pepper's Lonely Hearts Club Band
disc.artist=The Beatles
重Environment中获取外部注入值
@Configuration
@PropertySource("classpath:app.properties")
public class RuntimeInjectCompactDiscConfig {
@Autowired
Environment env;
@Bean
public BlankDisc setBlankDisc() {
BlankDisc blankDisc = new BlankDisc();
blankDisc.setTitle(env.getProperty("disc.title"));
blankDisc.setArtist(env.getProperty("disc.artist"));
blankDisc.setTracks(new ArrayList<>());
return blankDisc;
}
}
Environment中方法:
- String getProperty(String key):返回一个String类型值
- String getProperty(String key, String defaultValue):返回一个String类型值,没有值时取默认值
- T getProperty(String key, Class type):返回一个指定类型的值
- T getPropety(String key, Class type, T defaultValue):返回一个指定类型的值,没有时取默认值
- String getRequiredProperty(String key):返回一个String类型的值,没有定义时抛IllegalStateExceptio异常
- T getRequiredProperty(String key, Class targetType):返回一个指定类型的值,没有定义时抛IllegalStateExceptio异常
- boolean containsProperty(String key):判断某个属性是否存在
- String[] getActiveProfiles():返回激活的profile名称的数组
- String[] getDefaultProfiles():返回默认profile名称的数组
- boolean acceptsProfiles(Profiles profiles):如果environment支持给定的profile,返回true
小知识:Environment 除了可以注入外,还可以从ConditionContext中获得。
解析占位符 “&{…}”
为了使用占位符,我们需要配置一个PropertyPlacehokderConfigurer bean或者PropertySourcesPlaceholderConfigurer bean。从Spring3.1开始,推荐使用PropertySourcesPlaceholderConfigrer,因为它是基于Spring Environment及其属性源解析占位符。
@Bean在Java中配置PropertySourcesPlaceholderConfigrer
示例的代码:
@Bean
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
XML配置PropertySourcesPlaceholderConfigrer
context命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
示例代码:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
</beans>
Java解析占位符
代码示例
@Bean
@Qualifier("setBlankDisc2")
public BlankDisc setBlankDisc2(@Value("${disc.title}") String title, @Value("${disc.artist}") String aritst) {
BlankDisc blankDisc = new BlankDisc();
blankDisc.setTitle(title + "setBlankDisc2");
blankDisc.setArtist(aritst + "setBlankDisc2");
blankDisc.setTracks(new ArrayList<>());
return blankDisc;
}
XML解析占位符
代码示例
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<util:list id="trackList">
<value>Sgt. Pepper's Lonely Hearts Club Band</value>
<value>Sgt. Pepper's Lonely Hearts Club Band2</value>
<value>Sgt. Pepper's Lonely Hearts Club Band3</value>
<value>Sgt. Pepper's Lonely Hearts Club Band4</value>
</util:list>
<bean id="blankDisc"
class="com.zachary.soundsystem.impl.BlankDisc"
p:title="${disc.title}"
p:artist="${disc.artist}"
p:tracks-ref="trackList" />
</beans>
java配置类
@Configuration
@PropertySource("classpath:app.properties")
@ImportResource("classpath:cd-config.xml")
public class RuntimeInjectCompactDiscConfig {
@Autowired
Environment env;
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=RuntimeInjectCompactDiscConfig.class)
public class RuntimeInjectCompactDiscConfigTest {
@Autowired
BlankDisc blankDisc;
@Test
public void setBlankDiscTest() {
blankDisc.play();
}
}