Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: Spring DI (dependency injection) implementation and configuration attributes inject injection

Dependency injection (Dependency Injection, DI) and reverse the same meaning as the control, which is the same concept as described from two perspectives.
When a Java instance need another Java instance, the traditional method is to create an instance of the caller by the caller (for example, using a new keyword received callee instance), but after using the Spring framework, is an example of the caller no longer created by the caller, but created by the Spring container, which is known as inversion of control.
Spring container objects when creating an instance of the caller, the caller needs to automatically inject an instance to the caller, so the caller to obtain the callee instance via the Spring container, which is called dependency injection.
Dependency injection is mainly implemented in two ways, namely, a property setter injection and constructor injection. Detailed below.
1 ) injecting property setter
The method of using setter means IoC container instance is dependent injection. By calling the constructor with no arguments or no-argument static factory method instantiated bean, calling setter methods of the bean, the setter can be realized based on DI.
2 ) Constructor injection
It refers to a construction method using the injection IoC container instance is dependent. DI constructor implemented by calling the constructor parameters based, each representing a dependency.
The following property setter injection through case demonstrates how Spring's dependency injection container is achieved. Specific steps are as follows.
1 Create PersonService Interface
Create an interface called PersonService in com.mengma.ioc springDemo01 package item, the interface comprises a the addPerson () method as follows.
package com.mengma.ioc;
public interface PersonService {
    public void addPerson();
}
2 . Create a class interface PersonServiceImpl
Create a class named PersonServiceImpl at com.mengma.ioc package that implements the interface to PersonService, as shown below.
Package com.mengma.ioc;
 public  class PersonServiceImpl the implements PersonService {
     // definition of the interface declarations 
    Private the PersonDao PersonDao;
     // provide set () method for injection dependency 
    public  void setPersonDao (the PersonDao PersonDao) {
         the this .personDao = PersonDao;
    }
    // implemented method PersonService interface 
    @Override
     public  void the addPerson () {
        personDao.add (); // call PersonDao the add () method 
        System.out.println ( "addPerson () is executed ..." );
    }
}
First declared personDao objects, and add setter methods for dependency injection, and then realized addPerson PersonDao interface () method, and call the save () method and the output method in a statement.
3 Add the configuration information in the applicationContext.xml
Add a profile applicationContext.xml <bean> element, for instance of PersonServiceImpl class, instance and injected into personDao PersonService, which is implemented as shown in the following code:
 <the bean ID = "PersonService" class = "COM. mengma.ioc.PersonServiceImpl ">
    <! - The personDao Examples of injection personService example ->
    <property name="personDao" ref="personDao"/>
</bean>
4 . Writing Test Methods
Creating a named test2 () method in class FirstTest, edited as follows:
@Test
public  void test2 () {
     // definition of the configuration file Spring Path 
    String XMLPath = "the applicationContext.xml" ;
     // initialize Spring container, load the configuration files 
    the ApplicationContext applicationContext = new new the ClassPathXmlApplicationContext (
            xmlPath);
    // Get PersonService example through the container 
    PersonService PersonService = (PersonService) applicationContext
            .getBean ( "personService" );
     // call personService of addPerson () method 
    personService.addPerson ();
}
5 . Run the project and view the results
Use JUnit test test2 () method, after the successful operation, as shown in FIG console output.

As can be seen, the container after use Spring obtain userService instance, the instance is called addPerson () method, in which method in turn invokes the add PersonDao implementation class () method, and outputs the result. This is the Spring container property setter injection model, which is the actual development of the more common way.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12128886.html