Spring IOC (Inversion of Control) and DI (Dependency Injection)

definition

IOC (Inversion of Control), that is, inversion of control: the creation control of the object is no longer executed by the program , but is handed over to the Spring container . Simply put, the program does not need to perform new operations, and the objects are automatically created directly by the Spring container.
DI (Dependency Injection), that is, dependency injection: the Spring container provides the resources (objects) needed when the program is running.
Bean object: an object managed and created in the IOC container.

Insert image description here

Mainly represented by @Component, @Service, @Controller, @Repository, @Autowired, @Qualifier, and @Resource annotations.

@Component is a general annotation, and the other three annotations are derivative annotations of this annotation and have specific functions.
The @Controller layer is an annotation of spring-mvc, used to annotate control layer components and check whether request forwarding and redirection are processed.
@Service layer is the business logic layer annotation. This annotation only marks that the class is in the business logic layer.
The @Repository annotation is in the persistence layer, marking the DAO class, and has the function of automatically converting (encapsulating) data access exceptions thrown by database operations into spring persistence layer exceptions.

All annotations marked in the same directory or sub-package as the main program will be recognized by ComponentScan (component scanner) and create corresponding java beans in the Spring container. The name of the bean is the first letter of the class name in lowercase (Admin->admin).

@Autowired embodies the idea of ​​dependency injection. Obtaining objects from the container no longer requires creating objects through new. It can automatically create objects (interface implementation classes) based on qualified beans in the container. Injection is performed by type by default.
@Qualifier is generally used with Autowired and can specify a specific Bean class name.

@Resource is an annotation provided by jdk, while @Autowired and @Qualifer are annotations provided by Spring. @Resource is also injected directly through the class name of the Bean.

Program example

Dao layer: ArraysDao returns the [1,2,3] collection

public interface BasicDao<E> {
    
    
    List<E> getData();
}

@Repository  //标识对象是数据存储对象,并且在容器中创建bean
public class ArraysDao implements BasicDao<Integer> {
    
    
    @Override
    public List<Integer> getData() {
    
    
        ArrayList<Integer>arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        return  arrayList;

    }
}

Service layer: obtain data collection through Dao layer

public interface BasicService {
    
    
    List<Integer> getArray();
}

@Service  //将对象标识为Service对象,并在容器中创建bean
public class ArrayService implements BasicService{
    
    
    @Autowired
    BasicDao arraysDao;
    @Override
    public List<Integer> getArray() {
    
    
        return arraysDao.getData();
    }
}

Controlloer layer:


@RestController // =@ResponseBody +@Controller 标识是控制器对象
public class TestController {
    
    
    @Autowired //依赖注入,会自动注入符合BasicService接口的bean
    BasicService arrayService;

    @RequestMapping("/test")
    public List<Integer> test(){
    
    
        System.out.println(arrayService.getArray());
        return  arrayService.getArray();
    }
}

When the program is running, you can see the java beans generated by the corresponding annotations on this interface.
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44866921/article/details/133417264