In addition to component scanning and automatic assembly, there are Java code-based assembly and XML-based assembly.
There are some scenarios where we cannot use automatic assembly, for example, we are going to assemble the components in our third-party library into our application, and the automatic assembly is not valid because automatic assembly can only scan packages in this application.
At this point we're going to choose between code-based assembly and XML-based assembly, before we say that Javaconfig is more powerful and more secure than XML.
I. Java code-based Assembly Bean (Javaconfig)
But Javaconfig is also different from Java.
- Conceptually Javaconfig is different from the other code in the application of the business logic he does not contain any logical code and should not be put into any logic
- Distribution will place Javaconfig in a separate package
1.1 Creating a configuration class
1 Package test; 2 3 4 @Configuration 5 Public class dogcomfig{6 }
@Configuration Note that this class is a configuration class, but we did not write @componentscan annotations, so this class has no effect.
1.2 Declaring a simple bean
Declaring the bean in Javaconfig requires a method that returns an instance of the type we need, and then adds a @bean annotation to the bean [email protected] The annotation tells the container that the method returns an object. The object to be registered as a bean in the context of the spring application. The method body contains the logic that ultimately produces the bean instance. The ID of the default bean is the method name. Of course, you can also name @bean ("") with the method you learned before, and the logic can be infinitely complex, as long as the last one is returned.
Package test; PublicNew setdogs ();}
1.3 Using Javaconfig for Injection
The simplest way to assemble a bean in javaconfig is to refer to the method that created the bean, such as:
@Beanpublci playGame playgames () { returnnew playGame (Setdogs ());}
Here we write a method of playing a game, and the Setdogs method is passed in to show that the dog is playing games, it is straight to note that dog is not obtained by calling the Setdogs () method, because spring will intercept all calls to @bean annotations, And make sure to return directly to the bean created by the method, instead of creating a bean every time. Because spring's beans are singleton, the bean that returns the same method in an application context is the same.
The best way to do this is:
@Beanpublci playGame playgames (dog) { returnnew playGame (dog);}
Two. Assembling beans through XML
2.1 Creating an XML Configuration specification
Before using XML to assemble beans for spring, you need to create a new configuration specification. Like @configuration annotations in Javaconfig
<?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:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!--configuration Write here--></beans>
The most basic XML element of an assembly bean is contained in the Spring-beans pattern, and beans is the root element of all elements in the XML.
2.2 Declaring a simple bean
Another element in the Spring-beans pattern needs to be used here: <bean> This element is equivalent to @bean in Javaconfig. We declare in this format:
<bean id= "dog" class = "test.setdogs" > Best write ID is not written in case the ID is test. Dogs#0. #0是用来计数的, used to distinguish between beans of the same type. The Bean's ID will always be used.
Explanation: With this bean you don't have to create an instance of the bean, and when spring finds the bean, it creates the bean through the Setdogs default constructor.
2.3 Instantiating a bean with a constructor
When you declare di in XML, there are a number of optional configuration schemes and styles. There are two options for constructor injection:
- <constructor-arg> elements
- Using the C-namespace of Spring 3.0
<bean id = "" class = "" >
<constructor-arg ref = "" >
</bean>
Spring encounters this <bean> creates an instance of the,<constructor-arg> element tells spring to pass a worthy bean in the ID ref to the constructor.
Namespaces are simpler, but poorly understood, but highly recommended:
<bean id = "" class = "c:cd-ref=" ">
Explain:
Make up tomorrow.
Spring Learning Note (iii) Assembly BEAN