Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
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, @Importand, @DependsOnannotations to implement the Java configuration spring.
@Configuration & @Bean Note: 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. You can use the @bean annotation method with the components of Spring's @component annotations, however, these @bean annotations are usually the same as the @configuration bean.
@Configuration Annotated class indicates that the class is primarily defined as the source of a bean. In addition, the classes defined by the @Configurationd allows the use of @bean defined methods in the same class to define dependent beans
The annotation class and @configuration indicate that this class can use the spring IOC container as the source defined for the 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. The simplest possible class of @configuration is as follows:
@Configurationpublic class Companyconfig {@Beanpublic employee employee () {return new employee ();}}
The above code will be equivalent to the following XML configuration:
<bean id= "Companyconfig" class= "Com.mucfc.anno.CompanyConfig"/>
The following annotated method name for @bean is the ID of the working bean, which creates and returns the actual bean. A configuration class can have multiple @bean declared. Once the class definition is configured, you can load and supply them using Annotationconfigapplicationcontext as follows to the spring container:
public static void Main (string[] args) {applicationcontext ctx= new Annotationconfigapplicationcontext ( Companyconfig.class); Employee Employee=ctx.getbean (Employee.class); Employee.setname ("dumb"); Employee.setid (2012); SYSTEM.OUT.PRINTLN (employee);}
You can also write this:
Package Com.mucfc.anno;import Org.springframework.context.applicationcontext;import Org.springframework.context.annotation.annotationconfigapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main (String [] args) {Annotationconfigapplicationcontext ctx = new Annotationconfigapplicationcontext (); Ctx.register ( Companyconfig.class); Ctx.refresh (); Employee employee1 = Ctx.getbean (Employee.class); Employee employee2 = Ctx.getbean (Employee.class), Employee1.setname ("dumb"); Employee1.setid (+); Employee2.setname ( "Madman"); Employee2.setid (34546); System.out.println ("Exployee1" +employee1); System.out.println ("Exployee2" +employee2); System.out.print ("Employee1==employee2?:"); System.out.print (EMPLOYEE1==EMPLOYEE2);}}
Add a @scope to the @bean above.
Package Com.mucfc.anno;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import org.springframework.context.annotation.scope;@ Configurationpublic class Companyconfig {@Bean @scope ("singleton") public employee employee () {return new Employee ();}}
The rest is unchanged. Operation Result:
The singleton type is returned by default, and this is changed to Prototpye
What's the good of doing this?
1. Use plain Java code and do not need XML
2. The benefits of OO can also be enjoyed in the configuration
3. Type safety can also provide good support for refactoring
4. Can still enjoy all the functions provided by the SPRINGIOC container
The most common implementation classes for the
ApplicationContext interface are Classpathxmlapplicationcontext and Filesystemxmlapplicationcontext, as well as Portlet-oriented xmlportletapplicationcontext and web-oriented xmlwebapplicationcontext, are all oriented to of XML. Spring 3.0 adds two additional implementation classes: Annotationconfigapplicationcontext and Annotationconfigwebapplicationcontext. As you can see from the name, they are an IoC container initialization class that is born for annotations and relies directly on annotations as the source of container configuration information. Since Annotationconfigwebapplicationcontext is a web version of Annotationconfigapplicationcontext, its usage is almost no different than the latter, so this article will Annotationconfigapplicationcontext as an example to explain.
Annotationconfigapplicationcontext paired with @Configuration and @Bean annotations, since, The XML configuration is no longer the only way to configure the Spring IoC container. There is a competitive relationship between the two, but in most cases it is a collaborative relationship, and the combination makes the Spring IoC container configuration simpler and more powerful. Previously, we wrote the configuration information in XML, and now using annotations, the vector of configuration information was transferred from the XML file to the Java class. We usually end the class name for the class that holds the configuration information with "Config", such as Appdaoconfig.java, Appserviceconfig.java, and so on. We need to add @Configuration annotations on the class that specifies the configuration information to make it clear that the class is the source of information for the Bean configuration.
Note: Spring has the following requirements for classes that label the Configuration
The configuration class cannot be final; the configuration class cannot be localized, that is, the configuration class cannot be defined inside a method of another class, and the configuration class must have an parameterless constructor. Annotationconfigapplicationcontext identifies the return value of the method that is @Bean in the configuration class as a Spring Bean and registers it in the container, managed by the IoC container. The function of the @Bean is equivalent to the label in the XML configuration.
@Bean has the following four properties:
Name --Specifies the name of one or more beans. This is equivalent to the name attribute in the XML configuration.
Initmethod--When the container initializes the Bean, it invokes the method specified by the property. This is equivalent to the Init-method attribute in the XML configuration.
Destroymethod-this property is similar to the Initmethod function, and the method specified by the property is called before the container destroys the Bean. This is equivalent to the Destroy-method attribute in the XML configuration.
Autowire--Specifies the automatic assembly policy for Bean properties, which is a value of three static properties of the Autowire type. Autowire.by_name,autowire.by_type,autowire.no. Compared to the value of the Autowire attribute in the XML configuration, there is less constructor here, because constructor is meaningless here. @Bean does not directly provide properties for the specified scope, it can be implemented by @Scope .
Since the @configureation annotation class itself has been annotated with @component annotations, any class labeled @configuration is itself equivalent to the @component, That is, they can be injected into other beans like normal beans.
As below:
Package Com.mucfc.anno;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.annotation.Configuration, @Configurationpublic class Company {@Autowired// Automatic assembly configuration defined Companyconfig private companyconfig companyconfig;public companyconfig getcompanyconfig () { return companyconfig;} public void Setcompanyconfig (Companyconfig companyconfig) {this.companyconfig = Companyconfig;}}
Use:
Ctx.register (Companyconfig.class); Ctx.register (Company.class); Ctx.refresh ();
Employee Employee3 = Ctx.getbean (Company.class). Getcompanyconfig (). Employee (); Employee3.setname ("Red Revolution"); Employee3.setid (2342); System.out.println ("Exployee3" +employee3);
Output Result:
EXPLOYEE3 Employee Name: Red Revolution Staff ID: 2342
Start the spring container using Java class-based configuration information:
/*annotationconfigapplicationcontext CTX = new Annotationconfigapplicationcontext (); Ctx.register ( Companyconfig.class); Ctx.register (Company.class); Ctx.refresh (); */applicationcontext
Where the commented code and the code that is not annotated can be considered to be the same, the post-run results are not
@Import notes:
The @ Import annotation allows loading of @bean from another configuration class definition. Consider a configuration class, as follows:
@Configurationpublic class Configa { @Bean public A () { return new A (); }}
You can import the above bean declarations in another bean declaration as follows
@Configuration @import (configa.class) public class CONFIGB { @Bean public B A () { return new A ();} }
Now, with no instantiation required, when specifying both configuration A.class and configuration B.class, only the Config Class B needs to be provided as follows:
public static void Main (string[] args) { ApplicationContext ctx = new Annotationconfigapplicationcontext ( Configb.class); Now both beans A and B would be available ... A = Ctx.getbean (A.class); b b = Ctx.getbean (B.class);}
Life cycle Callbacks:
@Bean annotations support specifying arbitrary initialization and destruction?? Callback methods, like the initialization method of spring XML and the properties of the bean element destruction method:
public class Foo {public void init () { //initialization Logic } public void Cleanup () { //Destructi On Logic }} @Configurationpublic class AppConfig { @Bean (Initmethod = "Init", Destroymethod = "cleanup") Public Foo foo () { return new foo (); }}
Specify the applicable scope of the bean:
The default scope is singleton, but you can use the @scope annotation to override this as follows:
@Configurationpublic class AppConfig { @Bean @Scope ("prototype") Public foo foo () { return new Foo (); c4/>}}
A comparative summary of three configuration bean methods
Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
Java class-based configuration Bean for the "Spring" IOC