Previously exposed to XML-based configuration, spring,spring3.0 began to use pure Java code to configure spring applications with little or no XML. The steps for using spring based on Java configuration are as follows:
1. Create a Java-based configuration.
Configure a very small amount of XML to enable Java configuration:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-3.0.xsd "><Context:component-scanBase-package= "Com.springinaction.springidol" /></Beans>
<context:component-scan> In addition to knowing how to automatically register the beans that are labeled with annotations that use a stereotype (stereotype), it also automatically loads the classes that are annotated with @Configuration annotations. In the above configuration, the Base-package property tells spring to find all classes marked with the @configuration annotation in the Com.springinaction.spring-idol package.
2. Define a configuration class
Java classes that use @configuration annotations in a Java-based configuration are equivalent to the <beans> elements in an XML configuration, as follows:
Package Com.springinaction.springidol; Import org.springframework.context.annotation.Configuration; @Configuration Public class springidolconfig {// Bean declaration methods gohere}
@Configuration annotations tell Spring as an identity: This class will contain the definition of one or more spring beans. These beans are defined using the methods noted in the @bean annotations. Next, use @bean to assemble the beans declared using the spring Java-based configuration.
3. Declaring a simple bean
Use @bean annotations to label a method to define the Duke Bean:
@Bean Public Performer Duke () {returnnew juggler ();}
The above configuration is equivalent to the <bean> element that was previously configured with XML. @Bean tell spring that this method will return an object that should be registered as a bean in the spring application context. The method name is the ID of the bean. All the logic that is implemented in this method is essentially to create a bean.
4. Inject using Spring's Java-based configuration.
Before, using the constructor method injection in the XML configuration file, use the <constructor-arg> element to create an acrobat that throws 15 simultaneously. With Java-based configuration, you only need to direct the numbers directly to the constructor:
@Bean Public Performer Duke15 () {returnnew juggler (*);}
Setter method Injection is also a natural Java code:
@Bean Public New instrumentalist (); Kenny.setsong ("Jingle Bells"); return Kenny;}
The above shows the constructor injection and setter method injection for simple values based on Java configuration. The next step is to explain how the bean assembles another bean reference.
First, use Java to declare a sonnet29 Bean:
@Bean Private Poem sonnet29 () {returnnew Sonnet29 ();}
This is a simple Java-based bean declaration.
Next, create a poeticjuggler bean that assembles the sonnet29 bean for it through the constructor:
@Bean Public Performer Poeticduke () {returnnew Poeticjuggler (sonnet29 ());}
The above is to assemble a reference to the bean by referencing the other bean's method.
In spring's Java configuration, referencing a bean by declaring a method is not the same as calling the method.
By using the @bean annotation Callout sonnet29 () method, we will tell spring that we want the bean defined by the method to be registered in the application context of spring. Therefore, when this method is referenced in another bean's declaration method, spring intercepts the invocation of the method and attempts to find the bean in the application context, rather than having the method create a new instance.
Spring Combat Six uses a Java-configured spring