(This film is excerpted from the article in Java 3Y, for their own learning record, support the original)
Spring IOC addresses object management and object dependency. Spring hands objects over to the IOC container for management.
Inversion of control: Instead of creating an object ourselves, we hand it to the IOC container. Object creation is handed over to the external container, which is the inversion of control.
Dependency injection: Objects do not need to create or manage their dependencies themselves. Dependencies are automatically injected into objects that need them
The simplest way to think about dependency injection and inversion of control is that our objects are created “by ourselves”, but now we hand over the creation permissions and dependencies between objects to the IOC container.
There are four ways to assign objects to the IOC container:
- annotations
- XML
- JavaConfig
- Groovy DSL-based configuration
In daily development, we often use the @Component annotation identifier to put objects in the “IOC container” and the @AutoWired annotation to inject objects
For example, if I add a user, I need to perform a new operation on the database. Before adding a transaction, you need to start the transaction. After adding a transaction, you need to submit the transaction. Similar modify, delete operations must be opened, closed transactions. There will be such repeated code in the code, how to extract this section of repeated code.
Spring AOP is fundamentally based on dynamic proxies