SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-006-给bean运行时注入值(Environment,Property文件)

一、

直观的给bean注入值如下:

@Bean
public CompactDisc sgtPeppers() {
return new BlankDisc(
"Sgt. Pepper's Lonely Hearts Club Band",
"The Beatles");
} < bean id = "sgtPeppers"
class = "soundsystem.BlankDisc"
c: _title = "Sgt. Pepper's Lonely Hearts Club Band"
c: _artist = "The Beatles" / >

都是以硬编码的形式,spring提供了提供了两种方式以运行进注入(1)Property placeholders  (2)The Spring Expression Language ( S p EL )

二、Property placeholders

1.app.properties

 disc.title=Sgt. Peppers Lonely Hearts Club Band
disc.artist=The Beatles

2.

 package com.soundsystem;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; @Configuration
@PropertySource("classpath:/com/soundsystem/app.properties")
public class EnvironmentConfig { @Autowired
Environment env; @Bean
public BlankDisc blankDisc() {
return new BlankDisc(
env.getProperty("disc.title"),
env.getProperty("disc.artist"));
} }

3.给定默认值

package com.soundsystem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; @Configuration
public class EnvironmentConfigWithDefaults { @Autowired
Environment env; @Bean
public BlankDisc blankDisc() {
return new BlankDisc(
env.getProperty("disc.title", "Rattle and Hum"),
env.getProperty("disc.artist", "U2"));
} }

4.properties必需有值,否则报错

package com.soundsystem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; @Configuration
public class EnvironmentConfigWithRequiredProperties { @Autowired
Environment env; @Bean
public BlankDisc blankDisc() {
return new BlankDisc(
env.getRequiredProperty("disc.title"),
env.getRequiredProperty("disc.artist"));
} }

5.测试

 package com.soundsystem;

 import static org.junit.Assert.*;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class EnvironmentInjectionTest { @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=EnvironmentConfig.class)
public static class InjectFromProperties { @Autowired
private BlankDisc blankDisc; @Test
public void assertBlankDiscProperties() {
assertEquals("The Beatles", blankDisc.getArtist());
assertEquals("Sgt. Peppers Lonely Hearts Club Band", blankDisc.getTitle());
} } @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=EnvironmentConfigWithDefaults.class)
public static class InjectFromPropertiesWithDefaultValues { @Autowired
private BlankDisc blankDisc; @Test
public void assertBlankDiscProperties() {
assertEquals("U2", blankDisc.getArtist());
assertEquals("Rattle and Hum", blankDisc.getTitle());
} } public static class InjectFromPropertiesWithRequiredProperties { @Test(expected=BeanCreationException.class)
public void assertBlankDiscProperties() {
new AnnotationConfigApplicationContext(EnvironmentConfigWithRequiredProperties.class);
} } @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:placeholder-config.xml")
public static class InjectFromProperties_XMLConfig { @Autowired
private BlankDisc blankDisc; @Test
public void assertBlankDiscProperties() {
assertEquals("The Beatles", blankDisc.getArtist());
assertEquals("Sgt. Peppers Lonely Hearts Club Band", blankDisc.getTitle());
} } }

三、进一步讨论SPRING ’ S E NVIRONMENT

getProperty() is overloaded into four variations:
 String getProperty(String key)
 String getProperty(String key, String defaultValue)
 T getProperty(String key, Class<T> type)
 T getProperty(String key, Class<T> type, T defaultValue)

给定默认值

@Bean
public BlankDisc disc() {
return new BlankDisc(
env.getProperty("disc.title", "Rattle and Hum"),
env.getProperty("disc.artist", "U2"));
}

自动转型

int connectionCount =
env.getProperty("db.connection.count", Integer.class, 30);
@Bean
public BlankDisc disc() {
return new BlankDisc(
env.getRequiredProperty("disc.title"),
env.getRequiredProperty("disc.artist"));
}

Here, if either the disc.title property or the disc.artist property is undefined, an
IllegalStateException will be thrown.

检查是否存在

boolean titleExists = env.containsProperty("disc.title");
上一篇:django server之间通过remote user 相互调用


下一篇:SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍