Spring引入IOC和DI

什么是IOC和DI

IOC(Inversion of Control 控制反转):将实例化对象的控制权,由直接new转到spring容器通过反射机制实例化
DI(依赖注入):在创建对象时,把对象的依赖属性通过设置注入到对象中

后端项目基本结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-75YtbbmD-16172

引入IOC前

代码实现

实体类:User.java

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private Integer gender;
}

视图类:UserVo.java

@Data
@ToString
@NoArgsConstructor
public class UserVo {
    private Integer id;
    private String name;
    private Integer gender;
    private String genderName;

    public UserVo(User user){
        this.id=user.getId();
        this.name=user.getName();
        this.gender=user.getGender();
    }
}

dao层接口:UserDao.java

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

dao层实现类:UserDaoImpl.java

public class UserDaoImpl implements UserDao {
    @Override
    public User getInfo(Integer id) {
        return new User(id,"张三",0);
    }
}

service层接口:UserService.java

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

service层实现类:UserServiceImpl.java

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Override
    public UserVo getInfo(Integer id) {
        userDao = new UserDaoImpl();
        User user = userDao.getInfo(id);

        UserVo userVo = new UserVo(user);
        userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
        return userVo;
    }
}

controlletr层:UserController.java

public class UserController {
    private UserService userService;

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

测试类:Demo.java

public class Demo {
    @Test
    public void demo(){
        UserController userController = new UserController();
        UserVo info = userController.getInfo(1);
        System.out.println(info);
    }
}

测试

在这里插入图片描述

问题及解决

问题:代码的耦合度高,不利于扩展
解决:引入IOC或DI

引入IOC

首先要添加相关依赖并编写配置文件

代码实现

配置文件

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

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

controller层

public class UserController {
    private UserService userService;

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

service层

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Override
    public UserVo getInfo(Integer id, ApplicationContext applicationContext) {
        userDao = (UserDao) applicationContext.getBean("userDao");
        User user = userDao.getInfo(id);

        UserVo userVo = new UserVo(user);
        userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
        return userVo;
    }
}

测试类

public class Demo {
    @Test
    public void demo(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        UserVo info = userController.getInfo(1, applicationContext);
        System.out.println(info);
    }
}

测试

在这里插入图片描述

引入DI

配置文件修改

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

	<context:component-scan base-package="com"/>
</beans>

代码实现

控制层

@Controller
public class UserController {
    @Autowired
    private UserService userService;

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

业务层

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserVo getInfo(Integer id) {
        userDao = new UserDaoImpl();
        User user = userDao.getInfo(id);

        UserVo userVo = new UserVo(user);
        userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
        return userVo;
    }
}

数据持久层

@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public User getInfo(Integer id) {
        return new User(id,"张三",0);
    }
}

测试结果

在这里插入图片描述

常用注解介绍

  • @Controller:将类注入到容器中,一般在控制层上使用
  • @Service:将类注入到容器中,一般在业务层上使用
  • @Repository:将类注入到容器中,一般在数据持久层上使用
  • @Component:将类注入到容器中,一般在配置类上使用
  • @Autowired:根据类型自动注入容器中的实例
  • @Qualifier:通常和@Autowired一起使用,在其基础上再按照Bean的id注入实例
  • @Resource:根据名称自动注入容器中的实例

注意:在类上使用@Controller、 @Service、@Repository、@Component时,可以指定Bean的id值,也可以不指定,如果没有指定Bean的id值,则其id值默认为该类的类名并且首字母小写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值