您的位置:首页 > 编程语言 > Java开发

Spring 3 Java Based Configuration with @Value

2013-11-15 17:07 295 查看
Springsource has released the Javaconfig Framework as a core component of Spring 3.0. There is a trend in the industry to move from XML meta data toward using more annotation driven meta data. I say pick your poison, as one can mess up either.

I do like the readability of using Java code for configuration. Reading the Java classes used for the configuration has a shorter learning curve than reading XML files. Also I can directly unit test my configuration.

The Example

File Highlights

The example code located in on github here. I am using eclipse m2eclipse and spring plugins, and I would recommend importing the project as a maven project.

The Java class used for configuration:

@Configuration
// spring config that loads the properties file
@ImportResource("classpath:/properties-config.xml")
public class AppConfig {

/**
* Using property 'EL' syntax to load values from the
* jetProperties value
*/
private @Value("#{jetProperties['jetBean.name']}") String name;
private @Value("#{jetProperties['jetBean.price']}") Long price;
private @Value("#{jetProperties['jetBean.url']}") URL url;

/**
* Create a jetBean within the Spring Application Context
* @return a bean
*/
public @Bean(name = "jetBean")
JetBean jetBean() {
JetBean bean = new JetBeanImpl();
bean.setName(name);
bean.setPrice(price);
bean.setUrl(url);
return bean;
}

}


The highlights for this class:

@Configuration – Basic annotation for Java based configuration.

@ImportResource – Allows us to import a spring xml file to add more functionality to the configuration that this class is building.

@Value – Creates expression driven dependency injection.

@Bean – Create a bean managed by the spring container.

The properties-config.xml file:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- define the properties file to use -->
<util:properties id="jetProperties"
location="classpath:/jet.properties" />
</beans>


One line that matters. Line number 8. Create a
java.util.Properties
instance with values loaded from the supplied properties file.

Other files such as the POJO’s and properties files are included in the example. They are very vanilla, so I did not include them in the post. The test case has some changes in it.

public class JetBeanTest {

@Test
public void testJetBean() {
// create the spring container using the AppConfig
// @Configuration class
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
JetBean jetBean = ctx.getBean(JetBean.class);
assertThat(jetBean.getName(), equalTo("Gulf Stream G550"));
assertThat(jetBean.getPrice(), equalTo(Long.valueOf(60000000)));
URL gulfstream;
try {
gulfstream =
new URL("http://www.gulfstream.com/products/g550/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
fail("error creating URL");
throw new RuntimeException("error creating URL");
}
assertThat(jetBean.getUrl(), equalTo(gulfstream));
}
}


The main change is the new class
AnnotationConfigApplicationContext
which drives the creation of the Spring application context via Java configuration.

Recap

Use
@Configuration
to annotate your configuration class. Mark your beans with
@Bean
. The following annotations are supported:

@Configuration

@Bean

@DependsOn

@Primary

@Lazy

@Import

@ImportResource

@Value

Using @Value with Spring Expression Language

Now starts the crazy cool stuff. The
@Value
can be used on fields, methods and parameters. Plus, Spring Expression Language (SpEL) defines the ‘value’ through the syntax
#{ < SpEL expression > }
. The syntax can get a bit harry, but is incredibly powerful. In this example I have used SpEL to load values from the
javaProperties java.lang.Properties
Object with syntax like:

// jetProperties java.lang.Properties bean in context.
// jetBean.name value in the property file
@Value("#{jetProperties['jetBean.name']}") String name


Using @Value and SpEL gives you the functionality to do such things as: setting default values, accessing system level properties, logical operators, regex, mathematical operations.

To summarize as Uncle Ben said:


With great power there must come great responsibility.


Link to the example code located in on github here.

Much thanks to Chris Beams and his blog post on Springsource.

转自:http://chrislovecnm.com/2010/03/08/spring-3-java-based-configuration-with-value/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐