First, Spring container
A "container" is an important concept of spring, and its main purpose is to complete the creation of member variables and complete the assembly.
The characteristics of the container "inversion of control" and "dependency injection" are two mutually reinforcing concepts.
Control inversion: When we use an instance of a type to implement a function, we need to first new out an instance of that type and assign a value to one of our declared reference variables, so that we can use that variable to operate. and new and assigned to our own permissions, here is to reverse the control permission to spring.
Dependency Injection: A type is required to complete a function often requires other types of variables to complete, we often in the program through its own new way to accomplish this requirement, and in spring, it will help us to implement this Assembly function, which is dependency injection.
Second, in the system to achieve
Only the IOC container is used in spring, then the declaration of the preceding namespace only needs to use the
<? XML version= "1.0" encoding= "UTF-8" ?> < xmlns= "Http://www.springframework.org/schema/beans" 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 ">
Declare the bean under this tab:
<BeanID= "Name"class= "Com.brolanda.spring.p.name"> < Propertyname= "GivenName"value= "Esther" /> < Propertyname= "MiddleName"value= "LAN" /> < Propertyname= "LastName"value= "card" /></Bean>
We need to declare a variable name of type string in the name class, corresponding to the property label's name attribute, and set the set, get method for each property so that spring can help you inject it.
Public className {PrivateString GivenName; PrivateString MiddleName; PrivateString LastName; PublicString Getgivenname () {returnGivenName; } Public voidsetgivenname (String givenName) { This. GivenName =GivenName; } PublicString Getmiddlename () {returnMiddleName; } Public voidsetmiddlename (String middlename) { This. MiddleName =MiddleName; } PublicString Getlastname () {returnLastName; } Public voidsetlastname (String lastName) { This. LastName =LastName; }}
If the member variable is an instance of another custom type, then we need to declare the bean of the custom type first, and then introduce the corresponding bean in the declaration of the calling class:
<BeanID= "Person"class= "Com.brolanda.spring.p.person"> < Propertyname= "Name"> <refBean= "Name" /> </ Property></Bean>
That is, the person is referencing the name object declared above as its member variable.
The whole is as follows:
Spring container (inversion of control, dependency injection)