Spring knowledge point summary

Source: Internet
Author: User

Spring knowledge point summary

Before learning Spring, first understand what Spring is. Explanation on Baidu Encyclopedia: Spring is a lightweight open-source framework of hierarchical JavaSE/EE full-stack, with IoC (Inverse of Control inversion Control) and AOP (Aspect Oriented Programming for Aspect-Oriented Programming as the kernel ). There are two keywords involved here, namely, Ioc and AOP. To understand these two knowledge points, the core of Spring has mastered more than half of them.

 

I. IoC

 

The so-called IoC is the inverse of control inversion, and the object creation permission (instantiation) is handed over to the spring framework creation process. When IoC is applied, other objects that an object depends on will be passed in passively, instead of creating or searching for dependent objects.

For example, when learning Layer 3, we know that the Service layer class must call the Dao method. We must add a reference to Dao in the Service class to call the Dao method, next we will review our reference methods:

 

1. Use new

 

StudentDao stuDao = new StudentDao();

 

2. Use Interfaces

 

StudentDao stuDao = new UserDaoImpl();

 

3. Factory

 

StudentDao stuDao = BeanFactory.getBean();

 

 

We have learned the above methods before. The first two of them are known to have serious dependencies between the Service layer and Dao. to decouple the Service and Dao, we introduced the factory. However, the use of the factory also has the following drawbacks:

 

1. The factory class still needs to know the specific implementation class and its parameters. When the Program Complexity reaches a certain level, the factory class may still be very complicated.

2. It is still necessary to have the source code to update the code of the factory class before re-compilation.

Based on the above reasons, we introduced Spring IoC.

Spring IoC is configured through the configuration file:

Configure the Object Id and the corresponding implementation class in the configuration file, and then read the object through the API:

 

 

 

 

UserDao userDao = BeanFactory.getBean(userDao);

 

 

The advantage of writing this is that you do not need to write the factory class in code, but just a simple XML configuration file. Therefore, when registering the new class to the entire program, it does not need source code, compilation, and so on. If there are no special circumstances, you only need to compile the implementation class.

 

Ii. AOP

 

AOP is short for Aspect Oriented Programming, meaning: a technology that implements unified maintenance of program functions through the pre-compilation method and dynamic agent during runtime. AOP adopts a horizontal extraction mechanism to replace the repetitive code of the traditional vertical inheritance system. Spring AOP is used to complete functions: performance monitoring, transaction management, logging, caching, etc.

 

The related terminology of AOP is as follows:

 

1. Target: the Target object, which is the proxy object. Example: UserService

2. JoinPoint connection point. The method of proxy in the target object (drop ). Example: saveUser ()/updateUser/deleteUser/findUser

3. PointCut, the method actually represented, and the actual connection point. Example: saveUser

The starting point is the subset of the connection points.

4. Advice notification/enhancement method that is executed before or after the start point. For example, before and after

Category: pre-notification, post-notification, and surround notification. Notification is used to enhance the method.

5. Introduction, Introduction, special notifications. Directly enhance the class. Add methods to the class, modify methods, and so on.

6. Weaving, woven into: the process of referencing the notification to the target object. And associate the notification with the entry point.

7. Proxy, Proxy, the Target object Target generates the Proxy object, so the code can be enhanced.

Use a diagram to summarize their relationships:

Figure 1 Relationship Between AOP terms

 

AOP does not help us solve any new problems. It only provides a better way to solve some existing problems with less workload, and makes the system more robust, better maintainability. At the same time, it gives us new choices and new ideas when designing system architectures and modules.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.