Spring dependency Injection Method

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.