引入IOC之前
先来看看项目常用后端代码结构 下图所示:
代码实现
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;
// 省略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);
}
}
测试结果
XML改注解(IOC)
核心配置文件修改
在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>
这里要把userService 改成userDao
不然会报错
展示运行结果
引入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;
}
}
测试结果