I met the Spring

  1. Spring is a lightweightInversion of Control (IOC)andAspect oriented (AOP)The container (frame) of.
  2. Spring is an open source, free framework, container
  3. Spring is a lightweight framework that is non-intrusive
  4. Inversion of control IOC, faceted AOP
  5. Support for things, support for frameworks
  6. Spring philosophy: Make existing technologies more practical. It is a hodgepodge in itself, integrating existing framework technologies

The IOC based

IOC: Inversion of control.

What is inversion of control?

Control: Who controls the creation of objects? In traditional applications, objects are created by the program itself. With Spring, objects are created by Spring.

Inversion: instead of creating objects, the program passively receives them.

Hearing this explanation is not people still a little confused, ok, the following through an example to illustrate

Preparations:

  1. Create a UserDao interface
package dao;

public interface UserDao {
    public void getUser(a);
}
Copy the code
  1. The UserDao interface implementation class UserDaoImpl
package dao;

public class UserDaoImpl implements UserDao{
    public void getUser(a) {
        System.out.println("UserDao"); }}Copy the code

The UserDao interface implements the class UserDaoMysqlImpl

package dao;

public class UserDaoMysqlImpl implements UserDao {
    public void getUser(a) {
        System.out.println("Mysql"); }}Copy the code

The UserDao interface implementation class UserDaoOrcalImpl

package dao;

public class UserDaoOrcalImpl implements UserDao{
    public void getUser(a) {
        System.out.println("orcal"); }}Copy the code
  1. UserService interface
package service;

public interface UserService {
    public void getUserService(a);
}

Copy the code
  1. UserService interface implementation class UserServiceImpl
package service;

import dao.UserDao;
import dao.UserDaoMysqlImpl;
import dao.UserDaoImpl;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    public void getUserService(a) { userDao.getUser(); }}Copy the code
  1. The test class
import service.UserService;
import service.UserServiceImpl;

public class DemoTest {

    public static void main(String[] args) {
        UserService service = newUserServiceImpl(); service.getUserService(); }}Copy the code
  1. Experimental results:

When the new object in the UserService interface implementation class UserServiceImpl is UserDaoImpl

When the object of new in UserService interface implementation class UserServiceImpl is UserDaoMysqlImpl

When the new object in the UserService interface implementation class UserServiceImpl is UserDaoOrcalImpl

As you can see from this, if we write code like this, every time the user needs to change the requirements, we have to go back to the new object, this code is very bad. That allows us to improve. How? It’s very simple, using the set method.

The above code only needs to modify the UserService interface implementation class UserServiceImpl and the test class

UserService interface implementation class UserServiceImpl

package service;

import dao.UserDao;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void getUserService(a) { userDao.getUser(); }}Copy the code

The test class

import dao.UserDaoMysqlImpl;
import dao.UserDaoOrcalImpl;
import service.UserService;
import service.UserServiceImpl;

public class DemoTest {

    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserDao(newUserDaoOrcalImpl()); userService.getUserService(); }}Copy the code

In this case, we just need to modify the test class userService.setUserDao(new UserDaoOrcalImpl()); New UserDaoOrcalImpl() in UserDaoOrcalImpl(), we’ll just new whichever one the user needs.

====> These two different processing methods reflect inversion of control. The first is programmer control and the second is user control.

Here’s a very detailed example: Inversion of control is good if you go to a restaurant, the first one is, the restaurant buys the food, what you want to eat and when you get to the restaurant, the restaurant does it; The second way is to tell the restaurant what you want, and the restaurant brings it to you.