Spring IOC以及DI

一、引入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框架基础包并添加核心配置文件

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

User模块测试类:UserTest.java
1.读取配置文件,刷新Spring容器
2.Controller由手动实例化改为从Spring容器拿取
3.把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

1.Service由手动实例化改为从Spring容器拿取
2.把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容器拿取。

三、XML改注解(IOC)

1.修改核心配置文件
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()代码会报错,需要搭配注解使用。
常用的4种注解:
@Component:一般用于通用组件类上使用的注解
@Service:一般用于业务逻辑层上使用的注解
@Controller:一般用于流程控制层上使用的注解
@Repository:一般用于数据持久层上使用的注解

添加注解后会找不到bean,需要在注解时设置bean的id。
结果:
在这里插入图片描述

四、引入DI

常用注解:
@Autowired注解:自动按照类型注入
@Qualifier注解:在按照类型注入的基础之上,再按照Bean的id注入。
@Resource注解:指定依赖按照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;
    }
}

结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值