什么是IOC & DI
IOC(Inversion of Control 控制反转)是面向对象编程中的一种设计模式
其最常见的方式叫做DI(Dependency Injection 依赖注入)
通过控制反转,将实例化对象的控制权,由手动的new变成了Spring框架通过反射机制实例化
需要使用的时候,依赖通过配置文件以及注解的方式注入到对象中
项目常用后端代码结构 如下图所示:
我们新建Maven项目名为“spring-ioc“,设置好Maven版本、配置文件以及Maven仓库
以查询User数据为例对比IOC的引入前后程序耦合性
引入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);
}
}
测试结果
测试结果如下图所示:
表示Dao层数据已经一层层传到Controller层并展示了出来
缺点分析
1.代码耦合性太强 不利于程序的测试
2. 代码也不利于扩展
解决方式:
Spring的IOC完美的解决了这一点
对象的实例化由Spring框架加载实现,放到Spring容器中管理,避免了我们手动new对象
有需要用到对象实例依赖,直接向Spring容器要,让他注入即可
而一旦涉及到对象的实例修改,那么只需更改Spring加载实例化对象的地方,程序代码无需改动
从而降低耦合,提升扩展性
引入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
- 读取配置文件刷新Spring容器
- Controller由手动实例化改为从Spring容器拿取
- 把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
- Service由手动实例化改为从Spring容器拿取
- 把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;
}
}
测试结果
测试结果如下图所示:
表示已经将所有的依赖由手动实例化改为从Spring容器拿取
缺点分析
因为每一个类的实例化都需要一个bean标签,一个大型工程有很多类,配置文件的内容未免过于臃肿,维护成本高
解决方式
使用注解形式实现SpringIOC
XML改注解(IOC)
核心配置文件修改
context-component-scan标签Spring框架自定义的xml标签,通过base-package属性指明需要被自动扫描实例化的类所在位置
如下代码所示,我们在dao、services、controller下的类是需要扫描自动注入容器的
<?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>
修改好后运行项目发现context.getBean()代码报错
说明不是在base-package下的所有类都会自动注入到容器,而是要搭配注解使用
常用注解介绍
- @Component:一般用于通用组件类上使用的注解
- @Service:一般用于业务逻辑层上使用的注解
- @Controller:一般用于流程控制层上使用的注解
- @Repository:一般用于数据持久层上使用的注解
依次添加注解,添加之后运行再次报错找不到bean
其实我们在添加注解后,Spring会默认给每个bean设置id,值为类名首字母改为小写
这次报错原因就是找不到名为”userService“的bean
解决办法就是在注解时设置bean的id,保证可以找到bean
测试结果
测试结果如下图所示:
表示已经使用注解方式将对象放入Spring容器中
引入DI
上面所有的内容都是将对象放入Spring容器中
那么放入之后的使用呢,目前都是使用ApplicationContext拿取容器中的对象
接下来讲解如何使用注解实现依赖注入
常用注解介绍
@Autowired注解自动按照类型注入
会从容器中寻找符合依赖类型的实例,但是也有缺点:
因为时按照类型匹配,如果找不到匹配的实例也会抛出异常
如果容器中有多个匹配的类型也会抛出异常,需要指定引入的实例id
@Qualifier注解作用是在按照类型注入的基础之上,再按照Bean的id注入。所以如果是使用了@Autowire注解自动注入,但是容器中却有多个匹配的实例,可以搭配此注解,指定需要注入的实例id
@Resource注解作用是指定依赖按照id注入,还是按照类型注入。当只使用注解但是不指定注入方式的时候,默认按照id注入,找不到再按照类型注入。
代码实现
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;
}
}
测试结果
测试结果如下图所示:
表示
- @Autowired注解已将UserService依赖自动注入UserController
- @Qualifier注解已指定UserDao依赖的bean id,并使用@Autowired注解自动注入UserServiceImpl