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
1 disc.title=Sgt. Peppers Lonely Hearts Club Band 2 disc.artist=The Beatles
2.
1 package com.soundsystem; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.context.annotation.PropertySource; 7 import org.springframework.core.env.Environment; 8 9 @Configuration 10 @PropertySource("classpath:/com/soundsystem/app.properties") 11 public class EnvironmentConfig { 12 13 @Autowired 14 Environment env; 15 16 @Bean 17 public BlankDisc blankDisc() { 18 return new BlankDisc( 19 env.getProperty("disc.title"), 20 env.getProperty("disc.artist")); 21 } 22 23 }
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.测试
1 package com.soundsystem; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.BeanCreationException; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 10 import org.springframework.test.context.ContextConfiguration; 11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 13 public class EnvironmentInjectionTest { 14 15 @RunWith(SpringJUnit4ClassRunner.class) 16 @ContextConfiguration(classes=EnvironmentConfig.class) 17 public static class InjectFromProperties { 18 19 @Autowired 20 private BlankDisc blankDisc; 21 22 @Test 23 public void assertBlankDiscProperties() { 24 assertEquals("The Beatles", blankDisc.getArtist()); 25 assertEquals("Sgt. Peppers Lonely Hearts Club Band", blankDisc.getTitle()); 26 } 27 28 } 29 30 @RunWith(SpringJUnit4ClassRunner.class) 31 @ContextConfiguration(classes=EnvironmentConfigWithDefaults.class) 32 public static class InjectFromPropertiesWithDefaultValues { 33 34 @Autowired 35 private BlankDisc blankDisc; 36 37 @Test 38 public void assertBlankDiscProperties() { 39 assertEquals("U2", blankDisc.getArtist()); 40 assertEquals("Rattle and Hum", blankDisc.getTitle()); 41 } 42 43 } 44 45 public static class InjectFromPropertiesWithRequiredProperties { 46 47 @Test(expected=BeanCreationException.class) 48 public void assertBlankDiscProperties() { 49 new AnnotationConfigApplicationContext(EnvironmentConfigWithRequiredProperties.class); 50 } 51 52 } 53 54 @RunWith(SpringJUnit4ClassRunner.class) 55 @ContextConfiguration("classpath:placeholder-config.xml") 56 public static class InjectFromProperties_XMLConfig { 57 58 @Autowired 59 private BlankDisc blankDisc; 60 61 @Test 62 public void assertBlankDiscProperties() { 63 assertEquals("The Beatles", blankDisc.getArtist()); 64 assertEquals("Sgt. Peppers Lonely Hearts Club Band", blankDisc.getTitle()); 65 } 66 67 } 68 69 }
三、进一步讨论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");
You can do anything you set your mind to, man!