Importance: IoC is the inversion of control container of the Spring framework.
The first section of the core technology overview of the official spring document is to explain the Ioc container
IoC essence:
The container obtains instructions on which objects to instantiate, configure, and assemble by reading configuration metadata .
Let's understand the specific operation of the IoC container
1. First write a UserDao interface
public interface UserDao {
public void getUser();
}
2. Write Dao's implementation class again
public class UserDaoImpl implements UserDao {
public void getUser() {
System.out.println("获取默认数据!");
}
}
3. Then write a UserService interface
public interface UserService {
public void getUser();
}
4. Finally write the Service implementation class
public class UserServiceImpl implements UserService {
private UserDaoImpl userDao = new UserDaoImpl();
public void getUser() {
userDao.getUser();
}
}
5. Test:
public class MyTest {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
userService.getUser();
}
}
Suppose we now have the following Dao implementation classes. If you want to use either
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的数据!");
}
}
问题
: If we want to use any implementation class, it will be more cumbersome to modify the code every time, depending on the object used, the object each time is different. How to solve it? Don't let goset injection
Only need to modify the Service interface, the test code is as follows:
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 injects some personal explanations:
Scenario: Add two coordinates to determine what the parameters passed in by the user might be?
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);
}
}