什么是控制反转(IOC)
Ioc—Inversion of Control,即“控制反转”,它是一种设计思想,并不是什么技术;在 Java 中,IOC 意味着将我们设计好的对象交给容器控制,而不是传统的需要时在内部构造直接控制;
什么是依赖注入(DI)
DI-Dependency Injection,即"依赖注入",就是由容器动态的将某个依赖注入到组件中。通过依赖注入机制,我们只需要简单的配置,无需任何代码就可以指定目标所需要的资源,从而完成自身的业务逻辑;我们无需关心具体的资源来自何处,提升了系统灵活性和可扩展性。
引入IOC之前
实体类
public class User {
private Integer id;
private String name;
private Integer gender;
// 省略getter&setter方法
}
视图类
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方法
}
模块Dao
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;
}
}
Service层
public interface UserService {
UserVo getVo(Integer id);
}
Service层实现类
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;
}
}
Controller层
public class UserController {
private UserService userService;
public UserVo getVo(Integer id) {
// 手动实例化Service
userService = new UserServiceImpl();
// 执行Service层方法并返回
return userService.getVo(id);
}
}
测试类
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)
代码实现
要想使用SpringIOC首先需要导入Spring框架基础包并且添加Spring核心配置文件
<bean id="userDao" class="dao.impl.UserDaoImpl"/>
<bean id="userService" class="services.impl.UserServiceImpl"/>
<bean id="userController" class="controller.UserController"/>
测试类
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);
}
}
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);
}
}
Service层实现类
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属性指明需要被自动扫描实例化的类所在位置
如下代码所示,我们在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
引入DI
上面所有的内容都是将对象放入Spring容器中
那么放入之后的使用呢,目前都是使用ApplicationContext拿取容器中的对象
常用注解介绍
@Autowired注解自动按照类型注入会从容器中寻找符合依赖类型的实例,但是也有缺点:因为时按照类型匹配,如果找不到匹配的实例也会抛出异常
如果容器中有多个匹配的类型也会抛出异常,需要指定引入的实例id
@Qualifier注解作用是在按照类型注入的基础之上,再按照Bean的id注入。所以如果是使用了@Autowire注解自动注入,但是容器中却有多个匹配的实例,可以搭配此注解,指定需要注入的实例id
@Resource注解作用是指定依赖按照id注入,还是按照类型注入。当只使用注解但是不指定注入方式的时候,默认按照id注入,找不到再按照类型注入。
代码实现
Controller层:
@Controller
public class UserController {
// 改为自动注入
@Autowired
private UserService userService;
public UserVo getVo(Integer id, ApplicationContext context) {
// 执行Service层方法,因为之后还需要用到context对象,故下传
return userService.getVo(id, context);
}
}
Dao层实现类
// 改为默认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;
}
}
UService层实现类:
改为自动注入并指定需要注入的实例id
@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;
}
}