初识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);
    }
}

猜你喜欢

转载自blog.csdn.net/lirui1212/article/details/106342855