Bean dependency injection

Getting started with Bean dependency injection

①Create UserService, UserDao's save() method is being called inside UserService

public class UserServiceImpl implements UserService {
    
    
	@Override
	public void save() {
    
    
         ApplicationContext applicationContext = new 
                 ClassPathXmlApplicationContext("applicationContext.xml");       	           UserDao userDao = (UserDao) applicationContext.getBean("userDao");	
          userDao.save();
 	}
 }

②Give the creation right of UserServiceImpl to Spring

   <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>

③Obtain UserService from the Spring container for operation

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
userService.save();

1. Bean's concept of dependency injection

Dependency Injection: It is a concrete implementation of the core IOC of the Spring framework.

When writing a program, through the inversion of control, the creation of the object is handed over to Spring, but it is impossible to have no dependencies in the code.

IOC decoupling only reduces their dependencies, but does not eliminate them. For example: the business layer will still call the methods of the persistence layer.

After using Spring, the dependency between the business layer and the persistence layer is maintained by Spring.

Simply put, it is to wait for the framework to pass the persistence layer objects to the business layer instead of getting it ourselves

It is like that a number of functional classes are directly coupled together for corresponding data transmission and function invocation, and the emergence of Spring is to act as an intermediary between classes, using the concept of a container to turn all classes into Beans. And unified management, the data transfer inside is called dependency injection.

2. Bean's dependency injection method

① Construction method

// 创建有参构造

public class UserServiceImpl implements UserService {
    
    
@Override
public void save() {
    
    
ApplicationContext applicationContext = new 
                 ClassPathXmlApplicationContext("applicationContext.xml");       UserDao userDao = (UserDao) applicationContext.getBean("userDao");    
          userDao.save();
    }
 }

 // 配置Spring容器调用有参构造时进行注入

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">      		   	<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>

②set method

//在UserServiceImpl中添加setUserDao方法

public class UserServiceImpl implements UserService {
    
    
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
    
    
        this.userDao = userDao;  
        } 
    @Override    
    public void save() {
    
          
   		 userDao.save();
	}
}

  //配置Spring容器调用set方法进行注入

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
	<property name="userDao" ref="userDao"/>
</bean>

set method: P namespace injection

  P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:

  首先,需要引入P命名空间:

xmlns:p="http://www.springframework.org/schema/p"

Second, the injection method needs to be modified

<bean id="userService" class="com.itheima.service.impl.UserServiceImpl" p:userDao-
 ref="userDao"/>

3. Bean's dependency injection data type

The above operations are all injected reference beans. Object references can be injected. Common data types, collections, etc. can all be injected in the container.

Three data types of injected data

Common data type

Reference data type

Collection data type

The reference data type is not repeated here. The previous operations are all injecting the reference of the UserDao object. The following will take the set method injection as an example to demonstrate the injection of common data types and collection data types.

Bean's dependency injection data type

(1) Injection of common data types

 public class UserDaoImpl implements UserDao {
    
    
    private String company;
        private int age;
        public void setCompany(String company) {
    
    
            this.company = company;
        }
        public void setAge(int age) {
    
    
            this.age = age;
        }
        public void save() {
    
    
            System.out.println(company+"==="+age);
            System.out.println("UserDao save method running....");   
        }
    }
    

    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="company" value="传智播客"></property>
        <property name="age" value="15"></property>
    </bean>

(2) Injection of collection data type (List)

   public class UserDaoImpl implements UserDao {
    
    
    	private List<String> strList;
    	public void setStrList(List<String> strList) {
    
    
    		this.strList = strList;
    	}
    	public void save() {
    
    
            System.out.println(strList);
            System.out.println("UserDao save method running....");
    	}
    }

    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="strList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
    </bean>

(3) Injection of collection data type (List)

  public class UserDaoImpl implements UserDao {
    
    
    	private List<User> userList;
    	public void setUserList(List<User> userList) {
    
    
    	this.userList = userList;  
     }
    public void save() {
    
    
    	System.out.println(userList);
    	System.out.println("UserDao save method running....");
    	}
    }

    <bean id="u1" class="com.itheima.domain.User"/>
    <bean id="u2" class="com.itheima.domain.User"/>
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="userList">
            <list>
                <bean class="com.itheima.domain.User"/>
                <bean class="com.itheima.domain.User"/>
                <ref bean="u1"/>
                <ref bean="u2"/>       
            </list>
        </property>
    </bean>

(4) ( Map<String,User> )Injection of collection data types

 public class UserDaoImpl implements UserDao {
    
    
        private Map<String,User> userMap;
        public void setUserMap(Map<String, User> userMap) {
    
    
        this.userMap = userMap;
        }    
    public void save() {
    
          
    	System.out.println(userMap);
    	System.out.println("UserDao save method running....");
    	}
    }

    <bean id="u1" class="com.itheima.domain.User"/>
    <bean id="u2" class="com.itheima.domain.User"/>
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
        <property name="userMap">
            <map>            
                <entry key="user1" value-ref="u1"/>
                <entry key="user2" value-ref="u2"/>
            </map>
        </property>
    </bean>

(5) Injection of collection data types (Properties)

public class UserDaoImpl implements UserDao {
    
    
    private Properties properties;
    public void setProperties(Properties properties) {
    
    
        this.properties = properties;
    }
	public void save() {
    
    
		System.out.println(properties);
		System.out.println("UserDao save method running....");
	}
}

<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
    <property name="properties">
        <props>
            <prop key="p1">aaa</prop>
            <prop key="p2">bbb</prop> 
            <prop key="p3">ccc</prop>
        </props>
    </property>
</bean>

Guess you like

Origin blog.csdn.net/david2000999/article/details/113173791
Recommended