1. Introduction to dependency Injection
The basic principle behind dependency injection is the dependency between objects, which can be achieved through the following methods: constructor parameters, factory method parameters. The constructor or factory method is used to create objects and set properties. Therefore, the main work of the container can be understood as the dependencies injected during bean creation. Compared to the way that bean itself controls its instantiation and directly specifies dependencies in the constructor, the self-control dependency injection method is fundamentally reversed, which is also called control inversion.
There are two main methods of dependency injection: constructor injection and setter injection.
1. constructor injection:
Constructor-based injection is implemented by calling the constructor with parameters. Each parameter represents a dependency.
Instance:
Create an object Bean first
Public class helloworld <br/>{< br/> private string MSG; </P> <p> // requires no parameter construction by default <br/> Public helloworld () {}; </P> <p> Public helloworld (string MSG) {<br/> This. MSG = MSG; <br/>}</P> <p> Public String getmsg () {<br/> return MSG; <br/>}</P> <p> Public void setmsg (string MSG) {<br/> This. MSG = MSG; <br/>}< br/>}
In the modify configuration file applicationcontext. XML, instantiate Bean
<Bean id = "hello" class = "com. spring. demo. helloworld "> <br/> <constructor-Arg Index =" 0 "> <br/> <value> helloworld! </Value> <br/> </constructor-Arg> <br/> </bean>
Finally, test whether the injected bean can be obtained and print the attributes of the object.
Public static void main (string [] ARGs) {<br/> // read the configuration file and obtain beanfactory <br/> applicationcontext context = new classpathxmlapplicationcontext ("applicationcontext. XML "); </P> <p> beanfactory factory = context; </P> <p> helloworld Hello = (helloworld) factory. getbean ("hello"); </P> <p> system. out. println (hello. getmsg (); <br/>}
2. setter Injection
After the bean is instantiated by calling the no-argument constructor or the no-argument static factory method, the setter method of the bean can be called to implement dependency Injection Based on setter.
Create an object
Public class helloworld <br/>{< br/> private string MSG; </P> <p> Public String getmsg () {<br/> return this. MSG; <br/>}</P> <p> Public void setmsg (string MSG) {<br/> This. MSG = MSG; <br/>}< br/>}
Modify the configuration file applicationcontext. xml and instantiate the bean.
<Bean id = "hellobean" class = "com. Spring. Demo. helloworld"> <br/> <property name = "MSG" value = "Hello world! "/> <Br/> </bean>
Test class to get the injected Bean
Public static void main (string [] ARGs) {<br/> // read the configuration file and instantiate the IOC container <br/> applicationcontext context = new classpathxmlapplicationcontext ("applicationcontext. XML "); </P> <p> beanfactory factory = context; </P> <p> helloworld Hello = (helloworld) factory. getbean ("hellobean"); </P> <p> system. out. println (hello. getmsg (); <br/>}< br/>
Summary:
Follow these steps to process bean dependencies.
1. Create and initialize a beanfactory instance based on the bean configuration.
2. dependencies of each bean will appear in the form of attributes, constructor parameters, or static factory method parameters. When these beans are actually created, These dependencies will also be provided to the bean.
3. Each attribute or constructor parameter can be either an actual value or a reference to another bean in the container.
4. Each specified attribute or constructor parameter value must be converted to a specific format or type required by the constructor parameter.
Spring will verify the configuration of each bean in the container when the container is created, including verifying that the attributes referenced by those beans point to a valid bean. Before the bean is actually created, the bean attributes are not set. As the bean is actually created, the dependent bean that serves as the bean and the dependent bean will also be created and allocated.