Talking about the annotation methods of AOP in spring and AOP in spring, springaop
AOP (Aspect Oriented Programming): the terminology of AOP is "Aspect-Oriented Programming". What is Aspect-Oriented Programming? I understand that it enhances functionality without modifying the source code. well, we will mention this in the context of the aop annotation method below.
1. Set up an environment for aop annotation (import the following package)
II. Implementation
After the environment is set up, create a project.
1. Create an Interface Class (CustomerDao) and add two methods
2. After the interface class is created, it is necessary to create a new implementation class (CustomerDaoImpl) and implement the methods in the interface
3. After completing the above basic work, we need to use the springIOC idea and hand over the customerDaoImpl class to spring for management.
Create an xml file named applicationContext. xml in the src directory
Bean is a label pair in spring. You can think of it as a small item. id is your name, but generally it is the class name and then the first letter is lower case. The class is added with the full path of the class.
Enable Automatic annotation/Proxy: only when automatic proxy is enabled can the annotations we write next have a real application.
4. The environment and basic classes are all compiled. Next, you can create a Demo class for implementation.
First, we need to take out the CustomerDaoImpl class in the spring container.
The codes '1' and '2' do this.
As the name implies in '1', you have to tell it where to take it, so you need to give a path
In '2', the annotation method is called dependency injection: inject the class that has been written in the xml file into the customerDao field, and the name in @ Resource refers to the id value in the bean before.
The next step is to Test (Code '3' needs to be added), write a run1 (), and add @ Test on the method, during the test, double-click the method name and right-click it and choose to run on junit. This is a unit test, which is very convenient.
With the above preparations, after the run1 method is executed, the two rows of output in the implementation class can be displayed.
Iii. aop face-cutting
1. Create a partition class MyAspectAnno. java (add a bean to the configuration file and submit it to spring for management)
2. Add functions to be enhanced
The code in the value is an expression. * after the public parameter is used, the return value of any type is acceptable. save () indicates where to enhance or where to add a function, after the code is written, the method under the annotation @ after will be executed After the save () execution, that is, after (), which is enhanced.
However, when we write a few more notifications (such as surround notifications and pre-notifications), we need to add the Expression Code each time. Copying and pasting is also very troublesome. Here we can add an entry point to achieve the following:
In the future, we only need to do this. A surround notification is added below:
The value is no longer a long expression and can be easily maintained in the future.
It has been clearly stated that we have successfully implemented the function enhancement, using the aop idea and using the annotation method (the notifications we added are respectively in the start point (save ()) after and around)