The assembly of Spring beans is divided into 3:
1. Implicit discovery and automatic assembly
@Component: Declare a Java class as a bean (component Class), waiting for the spring scan to be discovered.
@ComponentScan: Enable component scanning to instantiate a class with @component as a bean, typically on a configuration class, you can specify a scan base package, which defaults to the same package as the configuration class.
@Configuration: Declaring a Java class is a configuration class, it is equivalent to @component, but more intuitive, generally @bean with it.
<context:component-scan base-package= "...";: Enables component scanning in XML mode.
@Autowired: Automatic injection of attributes in the bean, provided that the class must be declared as a bean, such as adding @component
@Bean: The return object of the method is used as a bean, provided that the class must be a bean, such as adding @configuration
2. Assembly via Java code
This part is the use of @configuration and @bean, by manually creating the object and then configuring it as a bean.
3. Through XML configuration
XML Configuration specification:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" ></Beans>
Declare the required bean,<bean id= "xxx" class= "full class name" &others.../> in <beans></beans>, or <bean ... >some The form of configuration</bean>.
Initialize beans with constructor injection: Use the <constructor-arg> or C namespace (you need to introduce a C namespace, such as the xmlns:c= of the XML header above ...). )。
Using <constructor-arg>:
<BeanID= "XXX"class= "Com.example.xxx"> <Constructor-argref= "Bean-id"/> <Constructor-argvalue= "value"/> <Constructor-arg><NULL/></Constructor-arg></Bean>
Use the C namespace:
<BeanID= "XXX"class= "Com.example.xxx"C:xxx-ref= "Bean-id"c:_xxx= "Constant Value"/><!--can also be named according to the order of parameters -<BeanID= "XXX"class= "Com.example.xxx"C:_0-ref= "Bean-id"c:_1= "Constant Value"/><!--when there is only one parameter, use C:_-ref or c:_= directly -
However, there is a limit to the C namespace, and only <constructor-arg> is used when constructing parameters into a collection:
<BeanID= "XXX"class= "Com.example.xxx"> <Constructor-argref= "Bean-id"/> <Constructor-argvalue= "value"/> <Constructor-arg><NULL/></Constructor-arg> <Constructor-arg> <List> <refBean= "B1"/> <refBean= "B2"/> <refBean= "B3"/> </List> </Constructor-arg> <Constructor-arg><!--Set and Constant - <Set> <value>One</value> <value>Both</value> <value>Three</value> </Set> </Constructor-arg></Bean>
Setting properties
<BeanID= "XXX"class= "Com.example.xxx"> < Propertyname= "Property-name"ref= "Bean-id"/> < Propertyname= "Property-name"value= "Constant-value"/> < Propertyname= "Property-name"> <List> ... </List> </ Property></Bean>
Or use the P namespace (introduced with xmlns:p in the XML header)
<id= "XXX" class= "com.example.xxx" = " Bean-id " =" Constant-value " /><!-- The P namespace is not available in order to determine attributes, nor can the collection class be processed--
Spring Util namespace simplifies collection classes: turning a collection into a bean
<!-- using Util:list to create a list bean - < = " Bean-id">< = "Bean-id" /> <!-- --</util:list >
4. Import and Hybrid configuration
Javaconfig referencing XML configuration: Importing XML using @importresource annotations on a configuration class, such as @importresource ("Classpath:beans.xml")
Javaconfig Mutual References: Use @import annotations on configuration classes, such as @import (Class-name.class)
XML reference Xml:<import resource= "Beans.xml"/>
5. JUnit Unit Test
Maven introduces JUnit and spring-test:
<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-test</Artifactid> <version>5.0.4.RELEASE</version> <Scope>Test</Scope> </Dependency> <Dependency> <groupId>Junit</groupId> <Artifactid>Junit</Artifactid> <version>RELEASE</version> </Dependency>
To write a test class:
ImportOrg.junit.Assert;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportSoundsystem. Cdplayerconfig;ImportSoundsystem.compactdisc;ImportSoundsystem. MediaPlayer;/*** Author: * DATE:2018/3/14 15:21 * Email: * Comment:*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Classes= Cdplayerconfig.class) Public classcdplayertest {@AutowiredPrivateMediaPlayer player; @AutowiredPrivateCompactdisc cd; @Test Public voidCdshouldnotbenull () {assert.assertnotnull (CD); } @Test Public voidplaytest () {player.play (); }}
Continuous update ...
The Assembly of Spring beans