JAVA framework Spring Dependency Injection

One: Introduction

Scenario: When we layer the program: web layer, business layer, persistence layer, there will be dependencies between each layer. For example: business layer and persistence layer, when the code of the business layer calls the persistence layer, the traditional way is: new persistence layer class.

And then call, this method will lead to high coupling, when modifying one layer of code, the other layer also needs to change the code. Bad for maintenance. This relationship is called "dependency".

How to solve?

solve:

Spring provides us with dependency injection, which means that when an object of a class is injected, the classes it depends on are also injected.

Code:

1) Traditional way:

1  package jd.com.service;
 2  
3  import jd.com.dao.UserDaoImpl;
 4  import org.junit.Test;
 5  
6  public  class UserServiceImpl implements UserService {
 7      @Test
 8      @Override
 9      public  void save() {
 10          System .out .println ( "The business layer calls the persistence layer. " );
 11          // Traditional way 
12          UserDaoImpl userDao= new UserDaoImpl();
 13          userDao.save();
 14      }
 15}

 

This approach results in higher coupling between the various layers.

2) set way dependency injection (class):

Set method: The dependent class needs to be set as a field and provide a set method.

private  UserDaoImpl userDao;

public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}

Depends on:

1  package jd.com.dao;
 2  
3  public  class UserDaoImpl implements UserService{
 4      @Override
 5      public  void save() {
 6 System.out          .println ( "The persistence layer saves data. " );
 7      }
 8 }

 

Class called:

 1 package jd.com.service;
 2 
 3 import jd.com.dao.UserDaoImpl;
 4 import org.junit.Test;
 5 
 6 public class UserServiceImpl implements  UserService {
 7     //set方式
 8 
 9     private  UserDaoImpl userDao;
10 
11     public void setUserDao(UserDaoImpl userDao) {
12         this.userDao = userDao;
13     }
14 
15     public void  save(){
16         System.out.println("The business layer calls the persistence layer. " );
 17          userDao.save();
 18      }
 19 }

 

Test class:

 1 package jd.com.service;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class Testdemo {
 8     @Test
 9     public  void  testdemo(){
10         ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
11         UserService userService= (UserService) ac.getBean("userserv");
12         userService.save();
13 
14     }
15 }

 

Profile configuration:

1      <bean id= " dao "   class = " jd.com.dao.UserDaoImpl " />
 2      <!--If it is the number of classes, use the ref value as the id name of the dependent class-->
 3      <bean id = " userserv "  class = " jd.com.service.UserServiceImpl " >
 4          ​​<property name= " userDao "  ref = " dao " />
 5      </bean>

 

3) set method (set field):

Field injection is also similar to the above but the configuration file is different this way.

Configuration file:

1     <bean  id="userserv" class= "jd.com.service.UserServiceImpl" >
2         <property name="userDao" ref="dao" />
3         <!--name是属性key value是属性value-->
4         <property name="oop"  value="java"/>
5     </bean>

 

Code: need to set the set method

1     public    String oop;
2 
3     public void setOop(String oop) {
4         this.oop = oop;
5     }
1     @Test
2     public void test(){
3         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
4         UserServiceImpl userService= (UserServiceImpl) ac.getBean("userserv");
5         System.out.println(userService.oop);
6     }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521295&siteId=291194637