初识IoC基础

重要性:IoC是Spring框架的控制反转容器。
spring官方文档核心技术第一节概述就是讲解Ioc容器
在这里插入图片描述

IoC实质:

容器通过读取配置元数据来获取有关要实例化,配置和组装哪些对象的指令
在这里插入图片描述

下面就具体的操作了解IoC容器

1.先写一个UserDao接口

public interface UserDao {
    public void getUser();
}

2.再去写Dao的实现类

public class UserDaoImpl implements UserDao {
    public void getUser() {
        System.out.println("获取默认数据!");
    }
}

3.然后写一个UserService接口

public interface UserService {
    public void getUser();
}

4.最后写Service实现类

public class UserServiceImpl implements UserService {
   private UserDaoImpl userDao = new UserDaoImpl();
    public void getUser() {
        userDao.getUser();
    }
}

5.测试:

public class MyTest {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        userService.getUser();
    }
}

假设我们现在以下几个Dao的实现类。如果要使用任意一个

public class UserDaoMysqlImpl  implements UserDao{
    public void getUser() {
        System.out.println("获取Mysql的数据!");
    }
}

public class UserDaoOracleImpl implements UserDao {
    public void getUser() {
        System.out.println("获取Oracle的数据!");
    }
}

public class UserDaoServerImpl implements UserDao {
    public void getUser() {
        System.out.println("获取Server的数据!");
    }
}

问题:如果我们要使用任意一个实现类会比较繁琐 每次都要修改代码,根据使用的对象,每次的对象也不同。如何解决呢?不放试试set注入
只需要修改Service接口、测试类代码如下:

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    //这里的set方法可以比较java的set方法,只不过传进去的参数是一个对象而已,需要对象那么在测试的时候我们就new 对象
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    public void getUser() {
        userDao.getUser();
    }
}
public class MyTest {
    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        //setUserDao();参数是使用的那个类
        userService.setUserDao(new UserDaoServerImpl());
        userService.getUser();
    }
}

set注入个人的一些解释:
场景:两个坐标进行相加,判断用户传入的参数可能都是什么?

public class Point {
    private int row;
    private int col;

   //有参构造方法
    public Point(int row, int col) {
        this.row = row;
        this.col = col;
    }

    //调用自身的有参构造方法
    public Point() {
        this(0, 0);
    }

    public Point(Point point) {
       this.col=point.getCol();
       this.row=point.getRow();
   }

   public Point(Point point1, Point point2) {
        this.row=(point1.getRow()+point2.getRow());
        this.col=(point1.getCol()+point2.getCol());
   }
   public Point(int row) {
        this.row=row;
   }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public Point add(int row, int col, int row1, int col1) {
        Point point = new Point();
        point.setRow(row+row1);
        point.setCol(col+col1);
        return point;
    }

    public Point add(Point point1, Point point2) {
        Point pt = new Point();
        pt.setRow(point1.getRow()+point2.getRow());
        pt.setCol(point1.getCol()+point2.getCol());
        return pt;
    }

    public Point add(Point point) {
        this.setCol(point.getCol());
        this.setRow(point.getRow());
        return point;
    }
  
    public String toString() {
        return String.valueOf("坐标row="+row+" 坐标col="+col);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值