Spring IOC & DI
1引入IOC之前
代码实现:
User模块实体类:User.java
package entity;
public class User {
private Integer id;
private String name;
private Integer gender;
// 省略getter&setter方法
}
User模块视图类:UserVo.java
package vo;
public class UserVo {
private Integer id;
private String name;
private Integer gender;
private String genderName;
// 省略getter&setter方法
public UserVo() {
}
public UserVo(User user) {
this.id = user.getId();
this.name = user.getName();
this.gender = user.getGender();
}
// 省略toString方法
}
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) {
// 手动实例化Dao
userDao = new UserDaoImpl();
// 执行Dao层方法
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) {
// 手动实例化Service
userService = new UserServiceImpl();
// 执行Service层方法并返回
return userService.getVo(id);
}
}
User模块测试类:UserTest.java
public class UserTest {
public static void main(String[] args) {
// 手动实例化Controller
UserController userController = new UserController();
// 执行Controller层方法
UserVo userVo = userController.getVo(1);
System.out.println(userVo);
}
}
测试结果
引入IOC(XML)
导入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
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
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;
}
}