Based on Java configuration options, you can write most of the spring without configuration XML, but with the help of several Java-based annotations explained. Starting with Spring3.0, we support the use of Java code instead of XML to configure spring, and Java-based configuration provides many of the benefits that spring relies on spring's Javaconfig project. By using @configuration, @Bean, @Import, @DependsOn to implement the Java configuration spring.
1) @Configuration & @Bean notes:
The intermediate artifacts of the new java-configuration in spring are class-based @configuration annotations and method-based @bean annotations.
@Bean annotations are used to indicate the instantiation of a method, and the configuration and initialization of an object is managed through the spring IOC container. For those familiar with <beans/> tags that use XML to configure spring, @Bean annotations and <bean/> tags are the same. The classes in spring's @component annotations can use @bean to annotate any method (just can), but typically use @configuration
@Configuration Annotated class indicates that the class is primarily defined as the source of a bean . In addition, the classes defined by the @Configuration allows the use of @bean defined methods in the same class to define dependent beans
annotation classes and @configuration indicate that this class can use the spring IOC container to define a source for a bean. In the @bean note, a method that tells spring that the annotation is @bean will return the Bean object that should be registered as a spring application context.
@Configurationpublic class Moviefinder {@Beanpublic injectionservice injectionservice () {return new Injectionserviceimpl ();}}
The above code will be equivalent to the following XML configuration:
<bean id= "Moviefinder" class= "Com.mypackage.MovieFinder" ></bean>
Example:
Define the store interface and implement class Storeimpl
Package Com.beanannotation;public interface Store {}
Package Com.beanannotation;public class Storeimpl implements Store {public void init () {System.out.println ("This is init.") );} public void Destory () { System.out.println ("This is Destory.");}}
Define Storeconfig:
Package Com.beanannotation;import Org.springframework.context.annotation.bean;import org.springframework.context.annotation.Configuration; @Configurationpublic class Storeconfig {@Bean (name= "Store", Initmethod= "Init", destroymethod= "destory")//If name is not specified, the default is method name public Store GetStore () {return new Storeimpl ();}}
XML configuration:
<?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/ http Www.springframework.org/schema/beans/spring-beans-4.1.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <context:component-scan Base-package= "Com.beanannotation" > </context:component-scan> </beans>
Unit tests:
Package Com.beanannotation;import Org.junit.test;import Org.springframework.context.support.classpathxmlapplicationcontext;public class UnitTest {@Testpublic void Test () { Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Classpath:spring-beanannotation.xml") ; Store service= (store) Context.getbean ("store"); System.out.println (Service.getclass (). GetName ()); Context.close (); Close the Spring container to trigger the execution of the Bean destruction method}}
Results:
2015-7-7 16:13:25 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]36b8bef7:startup Date [Tue Jul 16:13:25 CST 2015]; Root of context hierarchy2015-7-7 16:13:25 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [Spring-beanannotation.xml]this is init.2015 -7-7 16:13:25 org.springframework.context.support.ClassPathXmlApplicationContext doclose info: Closing org[email Protected]36b8bef7:startup Date [Tue Jul 16:13:25 CST 2015]; Root of Context hierarchycom.beanannotation.StoreImplthis is destory.
Spring Learning---@Configuration & @Bean annotations for Java class-based configuration Beans