Spring provides the functionality of AOP, as long as we know how to use it.
Proxy objects do not have to be generated by us. Spring helps us build. But we have to go with it.
Enhancements need to be written by themselves. For example, to write a log. Then the code that writes the log needs to write itself.
Write enhancements, re-configuration. When finished, it will help us to generate proxy objects and help us with AOP-related functions.
There are two types of AOP. One is the implementation that spring provides itself. However, Spring's own implementation is more troublesome.
Another company, AspectJ, also provides the implementation of AOP. Spring finds that ASPECTJ's AOP implementation is relatively straightforward. Spring took it over, as a way of realizing it. In the development, the general will use ASPECTJ way. I. AOP-related terminology
1. Joinpoint (connection point)
The so-called connection points refer to the points that are intercepted. In spring, these points refer to methods because spring only supports connection points of method types. All methods in a class can be referred to as connection points. All of these methods can be enhanced. Spring can only be enhanced for methods.
public class Bookdaoimpl {public
void Save () {
System.out.println ("Save book ...");
public void Update () {
System.out.println ("Modify book ...");}
}
All two of these methods are called connection points.
2. Pointcut (entry point)
The so-called entry point is the definition of which joinpoint we want to intercept.
The code above. If, we only want to enhance the Save () method, then the Save () method is the pointcut. When the program executes to the pointcut, the enhancement comes.
3. Advice (notification/enhancement)
The so-called notice refers to the interception to Joinpoint after the thing to do is to inform. Notification is divided into pre-notification, post-notification, exception notification, final notification, surround notification (slice to complete the function)
4. Introduction (Introduction)
Introduction is a special kind of notification without modifying the class code, introduction can dynamically add some methods or field to the class at run time.
5. Target Object --the target object of the agent
Is the object of the Bookdaoimpl class in our code above. It can also be called an enhanced object.
6. Weaving (Weaving) --refers to the process of applying the enhancement to the target object to create a new proxy object
7. Proxy--a class that is augmented by AOP, produces a result proxy class
8. Aspect ( cut-off)-is a combination of pointcuts and notifications, which we will write and configure ourselves later
Ii. AspectJ The development of AOP by means of XML
Step One : Create a Javaweb project to introduce the specific development of the JAR package
Introduce the basic development package developed by Spring Framework first
Re-introduction of the Spring Framework AOP development Package
The development of spring's traditional AOP package
Spring-aop-4.2.4.release.jar Com.springsource.org.aopalliance-1.0.0.jar AOP Alliance, defines a lot of specification ASPECTJ development packages
Com.springsource.org.aspectj.weaver-1.6.8.release.jar--ASPECTJ's Bag Spring-aspects-4.2.4.release.jar-- Spring consolidates ASPECTJ Packages
Step Two : Create a Spring configuration file that introduces specific AOP schema constraints
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
xsi:schemalocation="
Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop.xsd ">
</beans>
Step three: Creating a package structure, writing specific interfaces and implementing classes
Customerdao--Interface
Customerdaoimpl--Implementation class
Step Four: Configure the target class in spring
<bean id= "Customerdao" class= "Com.ken.demo3.CustomerDaoImpl"/>
Step Five: Defining Slice Classes
/**
* Slice class: Pointcut + notification
*
*
/public class Myaspectxml {
/**
* Notification (Specific enhancement) */public
void log () {
System.out.println ("Logging ...");}
}
Step Six: Defining a slice class in a configuration file
<bean id= "Myaspectxml" class= "com.ken.demo3.MyAspectXml"/>
Step Seven: Complete the configuration of AOP in the configuration file
<!--configuring AOP--
<aop:config>
<!--configuring slice classes: Pointcuts + notifications--
<aop:aspect ref= "Myaspectxml" >
<!--Configure the pre-notification, the enhanced method executes before the Save method executes--
< which method in the!--slice class. The log method of the Myaspectxml slice (there may be multiple facets or multiple methods)-
<!--pointcut expression: Execution (public void Com.ken.demo3.CustomerDaoImpl.save ())--
<aop:before method= "log" pointcut= "Execution (public void Com.ken.demo3.CustomerDaoImpl.save ()) "/>
</aop:aspect>
</aop:config>
Step Eight: Complete the test
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration ("Classpath:applicationContext.xml")
public class Demo3 {
@Resource (name = "Customerdao")
private Customerdao Customerdao;
@Test public
void Run1 () {
customerdao.save ();
Customerdao.update ();
}
}
Three, the expression of the Pointcut
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsd "> <bean id=" Customerdao "class=" Com.ken.demo3.CustomerDaoImpl "/> <!--Facets Class--<bean Id= "Myaspectxml" class= "com.ken.demo3.MyAspectXml"/> <!--configuration AOP--<aop:config> <aop:aspect ref= "M
Yaspectxml "> <!--pointcut expression 1.execution () fixed, cannot write 2.public can omit do not write 3.void, return value can appear * denotes any type, return value type cannot but write 4. The package name can be replaced with *. A * represents only one layer of package. Abbreviated: *.. *, which represents any layer package.
Can't do without writing. 5.*daoimpl: Class name, expressed with Daoimpl end 6. Method: Save* (), usually begins with Save 7. Method Parameters: Represents any parameter-<!--<aop:before method= "log" pointcut= "Execution (public void Com.ken.demo3.CustomerDaoImpl.save ()) "/>-<!--public can omit do not write-<!--<aop:before Me thod= "Log" pointcut= "Execution (void Com.ken.demo3.CustomerDaoImpl.save ())"/>-<!--void, return value can appear * denotes any class Type, return value types cannot be written-<!--<aop:before method= "log" pointcut= "Execution (* Com.ken.demo3.CustomerDaoImpl.save ())"/ > <!--package name can be replaced with *. A * represents only one layer of package. Abbreviated: *.. *, which represents any layer package. Can't do without writing. -<!--<aop:before method= "log" pointcut= "Execution (* *). *. Customerdaoimpl.save ()) "/>-<!--*daoimpl: class name, which means end with Daoimpl-<!--<aop:before method=" log "Pointcut=" Execution (* *). *.*daoimpl.save ()) "/>-<!--method: save* (), usually start with Save-<!--<aop:before method=" Log "Pointcu t= "Execution (* *). *.*daoimpl.save* ()) "/>--<!--parameters of the method:. Represents any parameter--<aop:before method= "log" pointcut= "Execution (* *). *.*daoimpl.save* (..)) " /> </aop:aspect> </aop:config> </beAns>
Iv. types of notifications for AOP
1. Front-facing notification
Executes before the method of the target class executes.
Configuration file Information:
<aop:config>
<aop:aspect ref= "Myaspectxml" >
<aop:before method= "log" pointcut= "Execution (* *.. *.*daoimpl.save* (..)) " />
</aop:aspect>
</aop:config>
Application: You can verify the parameters of the method
2. Final Notice
Executes after the method of the target class executes, and if the program has an exception, the final notification is executed.
Regardless of whether the program executes successfully or fails, the method is notified when it is executed.
Write the specific configuration in the configuration file:
<aop:config>
<aop:aspect ref= "Myaspectxml" >
<aop:after method= "after" pointcut= "Execution ( * *.. *.*daoimpl.save* (..)) " />
</aop:aspect>
</aop:config>
Applications: For example, releasing resources like
3. Rear-facing notification
The notification after the method is executed normally. Only if the method executes successfully will it be notified.
Write the specific configuration in the configuration file:
<aop:config>
<aop:aspect ref= "Myaspectxml" >
<aop:after-returning method= "Afterreturning" pointcut= "Execution (* *). *.*daoimpl.save* (..)) " />
</aop:aspect>
</aop:config>
Application: You can modify the return value of a method
4. Exception Throw notification
Notifies after an exception is thrown
Write the specific configuration in the configuration file:
<aop:config>
<aop:aspect ref= "Myaspectxml" >
<aop:after-throwing method= "Afterthrowing" pointcut= "Execution (* *). *.*daoimpl.save* (..)) " />
</aop:aspect>
</aop:config>
Application: Packaging Exception information
5. Surround Notifications
Execution of the method before and after execution. Configuration transactions are good.
Write the specific configuration in the configuration file:
<aop:config>
<aop:aspect ref= "Myaspectxml" >
<aop:around method= "Around" pointcut= " Execution (* *). *.*daoimpl.save* (..)) " />
</aop:aspect>
</aop:config>
Note: The target's method is not executed by default and requires the Proceedingjoinpoint pair to be used by the target object's method for execution.
Spring_aop_xml code Five, Spring framework of aop--annotation Way
Step One: Create a Javaweb project to introduce the specific development of the JAR package
Introduce the basic development package developed by Spring Framework first
Re-introduction of the Spring Framework AOP development Package
The development of spring's traditional AOP package
Spring-aop-4.2.4.release.jar
Com.springsource.org.aopalliance-1.0.0.jar
ASPECTJ's Development Kit
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Spring-aspects-4.2.4.release.jar
Step Two: create a Spring configuration file that introduces specific AOP schema constraints
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
xsi:schemalocation="/
http/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/
http WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">
</beans >
Step Three:Create a package structure, write specific interfaces, and implement classes
Customerdao--Interface
Customerdaoimpl--Implementation class
Step Four:To configure the target class in spring
<bean id= "Customerdao" class= "Com.ken.demo1.CustomerDaoImpl"/>
Step Five:Defining Slice Classes
Add annotations for facets and notifications
@Aspect--defining annotations for slice classes
Notification Type(The annotation parameter is an expression of the pointcut)
@Before--Pre-notification
@AfterReturing--Post notification
@Around-Surround Notifications
@After--Final Notice
@AfterThrowing--Exception throw notification
The specific code is as follows
@Aspect public
class Myaspectanno {
//@Before (value = "Execution", public void Com.ken.demo1.CustomerDaoImpl.save ()) ")
@Before (value =" Myaspectanno.fn () ") Public
Void log () {
System.out.println ("Logging ...");
}
@After (value = "Execution (public void Com.ken.demo1.CustomerDaoImpl.save ())")
@After (value = "Myaspectanno.fn" ( ) "Public
Void After () {
System.out.println (" final notification ... ");
@Around (value = "Execution (public void Com.ken.demo1.CustomerDaoImpl.save ())")
@Around (value = " Myaspectanno.fn () ") public
void Around (Proceedingjoinpoint joinpoint) {
System.out.println (" Surround notification 1 ... ");
try {
joinpoint.proceed ();
} catch (Throwable e) {
e.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Surround notification 2 ...");
Configure a common pointcut. Use @pointcut to define a common pointcut
@Pointcut (value = "Execution (public * *). *.*daoimpl.save* (..)) ")
public void fn () {
}
}
Step Six: Defining a slice class in a configuration file
<bean id= "Myaspectanno" class= "Com.ken.demo1.MyAspectAnno"/>
Step Seven:Turn on automatic proxy in the configuration file
<aop:aspectj-autoproxy/>
Complete the test
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration ("Classpath:applicationContext.xml")
public class Demo1 {
@Resource (name = "Customerdao")
private Customerdao Customerdao;
@Test public
void Run1 () {
customerdao.save ();
Customerdao.update ();
}
}
Spring AOP Annotation Mode code