This is the 11th day of my participation in the More text Challenge. For more details, see more text Challenge


Related articles

Spring series: Spring series


preface
  • Spring is alightweightInversion of controlAnd (IOC)Aspect oriented(AOP) framework.
  • Official website address: Spring official website
  • Making: making
  • All versions download address: download address
The IOC’s derivation
1.1. Simulate a normal information query business process:

1. Mapper layer: Since there is no database connection, here we write a mapper implementation class to simulate data query

public interface PerMapper {
    void getPerInfo(a);
}
Copy the code
public class StudentMapperImpl implements PerMapper {

    @Override
    public void getPerInfo(a) {
        System.out.println("I am a student"); }}Copy the code

② Service layer: The service layer is used to query information about people

public interface PersonService {
    void getPersonInfo(a);
}
Copy the code
public class PersonServiceImpl implements PersonService {

    private PerMapper studentMapper = new StudentMapperImpl();

    @Override
    public void getPersonInfo(a) { studentMapper.getPerInfo(); }}Copy the code

(3) contorller layer

import service.PersonService;
import service.impl.PersonServiceImpl;

public class IOCTest {
    public static void main(String[] args) {
        PersonService service = newPersonServiceImpl(); service.getStudentInfo(); }}Copy the code

④ The execution result is as follows:

1.2. Multiple kinds of queries

Mapper, add teacher implementation class

public class TeacherMapperImpl implements PerMapper {

    @Override
    public void getPerInfo(a) {
        System.out.println("I am a teacher"); }}Copy the code

② What should we do to query the information of teachers and students at the same time?

public class PersonServiceImpl implements PersonService {

    private PerMapper student = new StudentMapperImpl();
    private PerMapper teacher = new TeacherMapperImpl();

    @Override
    public void getPersonInfo(a) { student.getPerInfo(); teacher.getPerInfo(); }}Copy the code

③ Execution result:④ What if the requirements change again? What if you only need information from the teacher? No doubt: one of the two options is to change the getPersonInfo() of PersonServiceImpl directly

   @Override
    public void getPersonInfo(a) {
// student.getPerInfo();
        teacher.getPerInfo();
    }
Copy the code

The second is to extend the interfaces of the Service layer to provide teachers and students with separate interfaces to query:

public interface PersonService {
    void getPersonInfo(a);
    void getPersonInfo1(a);
}
Copy the code
    public class PersonServiceImpl implements PersonService {

    private PerMapper student = new StudentMapperImpl();
    private PerMapper teacher = new TeacherMapperImpl();

    @Override
    public void getPersonInfo(a) {
        teacher.getPerInfo();
    }

    @Override
    public void getPersonInfo1(a) { student.getPerInfo(); }}Copy the code

⑤ Doesn’t it seem to be a problem? What if there were a hundred kinds of people? It needs to be fixed. It’s gonna drive people crazy. This is certainly not reasonable!

1.3. Optimize the query mode

① Smart children can think of the query object out: add set() method, do not implement the interface, only reserved work.

public class PersonServiceImpl implements PersonService {

    private PerMapper per;

    public void setPer(PerMapper per) {
        this.per = per;
    }

    @Override
    public void getPersonInfo(a) { per.getPerInfo(); }}Copy the code

② Implementation mode of controller layer:

public class IOCTest {
    public static void main(String[] args) {
        PersonServiceImpl service = new PersonServiceImpl();
        / / student
        service.setPer(new StudentMapperImpl());
        service.getPersonInfo();
        / / the teacher
        service.setPer(newTeacherMapperImpl()); service.getPersonInfo(); }}Copy the code

③ Conclusion: This way is to give the initiative to the caller, the program does not need to tube how to create, how to achieve. It is only responsible for providing an interface.

  • We no longer manage the creation of objects, but focus more on the implementation of the business, and the coupling is greatly reduced, which is the prototype of IOC!
1.4 Essence of IOC
  • Inversion of control (INVERSION of Control) is a way to produce or obtain a specific object by describing it (XML or annotations) and using a third party. In Spring, inversion of control is implemented by the IOC container through Dependency Injection (DI).

The road ahead will be long and long, but I will keep searching. If you think I blogger’s writing is good! Writing is not easy, please like, follow, comment to give the blogger a encouragement ~