Spring--di (Dependency Injection)/IOC (control inversion)

Source: Internet
Author: User

DI (Dependency injection): Dependency Injection

During operation, dependent objects are dynamically injected into the component by an external container;

Ioc (Inversion of control): Inversion of controls

The application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects is the responsibility of the external container. Thus control is transferred from the application itself to the external container, and the transfer of control is called reversal. For example:

The first code:

public class goodservice{

public void Save () {

Proactively acquire

Gooddao Gooddao = new Gooddao ();

Gooddao.save ();

}}

The method of active acquisition, control in the Goodservice class. Hard-coded way.

Now:

public class goodservice{

Private Gooddao Gooddao;

Set/get method omitted

public void Save () {

Gooddao.save ();

}}

Goodservice obtains a passive acceptance of a Gooddao program.

From active acquisition to passive acceptance, control is not changed. IOC

The IOC controls the reversal and gets the Gooddao object (dependent object) in a way that becomes an external injection.

implementing Ioc/di with the Spring framework

1, import spring framework;

2. Create an instance in the configuration file

<bean class= "Com.no1.dao.GoodsDAO" id= "Goodsdao"/>

Class: Represents the name of the category.

Id/name: Instance name 3, obtaining instances of spring configuration

1 ApplicationContext class.

Newfilesystemxmlapplicationcontext ("Src/applicationcontext.xml");

2 Getbean ("Id/name of Profile")

4. Implement Dependency Injection

<bean class= "Com.no1.biz.GoodsBIZ" name= "goodsbiz" >

<property name= "Goodsdao" ref= "Goodsdao"/> </bean>
three ways to instantiate objects

1. Construction method

<bean class= "Com.no1.dao.GoodsDAO" id= "Goodsdao"/> 2, static method of factory class

public class staticfactory{

public static Gooddao Create () {

return new Gooddao ();

}}

<bean id= "Gooddao" class= "com.no1.factory.StaticFactory" factory-method= "Create"/> 3, instance method of factory class

public class methodfactory{

Public Gooddao Create () {

return new Gooddao ();

}}

<bean id= "Methodfactory" class= "Com.no1.factory.MethodFactory"/>

<bean id= "Goodsdao" factory-bean= "methodfactory" factory-method= "create"/>

two ways of relying on injection

1. Attribute injection: Call the Set method

public class goodservice{

Private Gooddao Gooddao;

Set/get method omitted

public void Save () {

Gooddao.save ();

}}

<bean class= "Com.no1.biz.GoodService" name= "Goodservice" >

<property name= "Gooddao" ref= "Gooddao"/>

</bean> 2, Construction injection: By way of constructing method

<bean class= "Com.no1.biz.GoodsBIZ" name= "goodsbiz" >

<constructor-arg index= "0" ref= "Goodsdao"/>

</bean>

index= "0" represents the first parameter of a constructor method

<constructor-arg name= "parameter name"

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.