Spring IOC & DI

 

新建Maven项目,设置Maven版本,配置文件与Maven仓库,在代码引入之前,需要用代码实现以下

  • User模块实体类:User.java

package entity;

public class User {
private Integer id;
private String name;
private Integer gender;
}

  • User模块视图类:UserVo.java

package vo;

public class UserVo {
private Integer id;
private String name;
private Integer gender;
private String genderName;
public UserVo() {
}
public UserVo(User user) {
this.id = user.getId();
this.name = user.getName();
this.gender = user.getGender();
}
}

  • User模块Dao层:UserDao.java

package dao;

public interface UserDao {
User getEntity(Integer id);
}

  • User模块Dao层实现类:UserDaoImpl.java

package dao.impl;

public class UserDaoImpl implements UserDao {
public User getEntity(Integer id) {
User user = new User();
user.setId(1);
user.setName("Anne");
user.setGender(0);
return user;
}
}

  • User模块Service层:UserService.java

package services;

public interface UserService {
UserVo getVo(Integer id);
}

  • User模块Service层实现类:UserServiceImpl.java

package services.impl;

public class UserServiceImpl implements UserService {
private UserDao userDao;

public UserVo getVo(Integer id) {
userDao = new UserDaoImpl();
User user = userDao.getEntity(id);
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}

  • User模块Controller层:UserController.java


package controller;

public class UserController {
private UserService userService;

public UserVo getVo(Integer id) {
userService = new UserServiceImpl();
return userService.getVo(id);
}
}

  • User模块测试类:UserTest.java


public class UserTest {
public static void main(String[] args) {
UserController userController = new UserController();
UserVo userVo = userController.getVo(1);
System.out.println(userVo);
}
}

引入IOC

<bean id="userDao" class="dao.impl.UserDaoImpl"/>
<bean id="userService" class="services.impl.UserServiceImpl"/>
<bean id="userController" class="controller.UserController"/>

  • User模块测试类:UserTest.java

public class UserTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserController userController = (UserController) context.getBean("userController");
UserVo userVo = userController.getVo(1, context);
System.out.println(userVo);
}
}

  • User模块Controller层:UserController.java

package controller;

public class UserController {
private UserService userService;

public UserVo getVo(Integer id, ApplicationContext context) {
userService = (UserService) context.getBean("userService");
return userService.getVo(id, context);
}
}

  • User模块Service层实现类:UserServiceImpl.java

package services.impl;

public class UserServiceImpl implements UserService {
private UserDao userDao;

public UserVo getVo(Integer id, ApplicationContext context) {
userDao = (UserDao) context.getBean("userDao");
User user = userDao.getEntity(id);
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}

 

引入IOC(XML)

代码实现

要想使用SpringIOC首先需要导入Spring框架基础包并且添加Spring核心配置文件

将依赖交给Spring的beanFactory管理

 <bean id="userDao" class="dao.impl.UserDaoImpl"/>
<bean id="userService" class="services.impl.UserServiceImpl"/>
<bean id="userController" class="controller.UserController"/>

User模块测试类:UserTest.java

  1. 读取配置文件刷新Spring容器
  2. Controller由手动实例化改为从Spring容器拿取
  3. 把ApplicationContext传到Controller层继续使用
public class UserTest {
public static void main(String[] args) {
// 读取配置文件刷新Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 从Spring容器拿Controller
UserController userController = (UserController) context.getBean("userController");
// 执行Controller层方法,因为之后还需要用到context对象,故下传
UserVo userVo = userController.getVo(1, context);
System.out.println(userVo);
}
}

User模块Controller层:UserController.java

  1. Service由手动实例化改为从Spring容器拿取
  2. 把ApplicationContext传到Service层继续使用

 

package controller;

public class UserController {
private UserService userService;

public UserVo getVo(Integer id, ApplicationContext context) {
// 从Spring容器拿Service
userService = (UserService) context.getBean("userService");
// 执行Service层方法,因为之后还需要用到context对象,故下传
return userService.getVo(id, context);
}
}

User模块Service层实现类:UserServiceImpl.java

Dao由手动实例化改为从Spring容器拿取

package services.impl;

public class UserServiceImpl implements UserService {
private UserDao userDao;

public UserVo getVo(Integer id, ApplicationContext context) {
// 从Spring容器拿Dao
userDao = (UserDao) context.getBean("userDao");
// 执行Dao层方法
User user = userDao.getEntity(id);
// 省略业务逻辑处理。。。
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}

XML改注解(IOC)


核心配置文件修改


context-component-scan标签Spring框架自定义的xml标签,通过base-package属性指明需要被自动扫描实例化的类所在位置

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">

<!-- bean definitions here -->
<context:component-scan base-package="dao"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="controller"/>

</beans>

引入DI

User模块Controller层:UserController.java
package controller;

@Controller
public class UserController {
// 改为自动注入
@Autowired
private UserService userService;

public UserVo getVo(Integer id, ApplicationContext context) {
// 执行Service层方法,因为之后还需要用到context对象,故下传
return userService.getVo(id, context);
}
}

User模块Dao层实现类:UserDaoImpl.java

去除指定bean id,改为默认bean id(userDaoImpl)

package dao.impl;

// 改为默认bean id“userDaoImpl”
@Repository
public class UserDaoImpl implements UserDao {
public User getEntity(Integer id) {
// 此处应该从数据库查询值 方便起见直接返回一个固定对象
User user = new User();
user.setId(1);
user.setName("Anne");
user.setGender(0);
return user;
}
}

User模块Service层实现类:UserServiceImpl.java

改为自动注入并指定需要注入的实例id


package services.impl;

@Service("userService")
public class UserServiceImpl implements UserService {
// 改为自动注入并指定需要注入的实例id
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;

public UserVo getVo(Integer id) {
// 执行Dao层方法
User user = userDao.getEntity(id);
// 省略业务逻辑处理。。。
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}
 
posted on 2021-03-28 13:13  SpringTrap  阅读(56)  评论(0)    收藏  举报