The calling process to simulate the actual development process XML version -Setter way injection
Source obtain github
[TOC]
1. Project Structure
2.jar to keep up with the same Package
3. Rewrite set method
UserServiceImpl.java
1 2 3 4 5
|
private UserDao userDao;
public void (UserDao userDao) { this.userDao = userDao; }
|
beans.xml
1 2 3 4 5 6 7
|
<bean id="userDaoImpl" class="com.hs.dao.impl.UserDaoImpl"/>
<bean id="userServiceImpl" class="com.hs.service.impl.UserServiceImpl"> <property name="userDao"> <ref bean="userDaoImpl"/> </property> </bean>
|
In the set method in place of complete,private UserDao userDao = new userDaoImpl();
UserController.java
1 2 3 4 5
|
private UserService userService;
public void setUserService(UserService userService) { this.userService = userService; }
|
beans.xml
1 2 3 4 5 6 7 8 9 10< 大专栏 Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入/span> 11
|
<bean id="userServiceImpl" class="com.hs.service.impl.UserServiceImpl"> <property name="userDao"> <ref bean="userDaoImpl"/> </property> </bean>
<bean id="userController" class="com.hs.web.UserController" p:userService-ref="userServiceImpl"> </bean>
|
在set方法里代替完成了private UserService userService = new userServiceImpl();
4.测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
package com.hs.test;
import com.hs.web.UserController; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MethodTest { public void test01() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); UserController controller = applicationContext.getBean("userController", UserController.class); controller.add(); } }
|
Console output:
UserController Layer -> ADD
implementation class UserService layer -> ADD
implementation class UserDao layer -> ADD