Spring详细内容-Spring AOP En(4)
1.Spring AOP
Aspect Oriented Programming (AOP)
compliments OOPs in the sense that it alsoprovides modularity
.- But the key unit of modularity is
aspect than class
. - AOP breaks the program
logic into distinct parts (called concerns)
. - It is used to
increase modularity by cross-cutting concerns
. A cross-cutting
concern is a concern that can affect the whole application and should be centralized in one location in code as possible, such as transaction management, authentication, logging, security etc.
1)Why use AOP?
- It provides the pluggable way to dynamically add the additional concern before, after or around the actual logic.
2)Where use AOP?
AOP is mostly used in following cases:
- to provide declarative enterprise services such as declarative transaction management.
- It allows users to implement custom aspects.
3)AOP Concepts and Terminology
Join point
- Join point is any point in your program such as method execution, exception handling, field access etc.
- Spring supports only method execution join point.
Advice
- Before Advice: it executes before a join point.
- After Returning Advice: it executes after a joint point completes normally.
- After Throwing Advice: it executes if method exits by throwing an exception.
- After (finally) Advice: it executes after a join point regardless of join point exit whether normally or exceptional return.
- Around Advice: It executes before and after a join point.
Pointcut
- It is an expression language of AOP that matches join points.
Introduction
- It is an expression language of AOP that matches join points.
Target Object
- It is the object i.e. being advised by one or more aspects.
- It is also known as proxied object in spring because Spring AOP is implemented using runtime proxies.
Aspect
- It is a class that contains advices, joinpoints etc.
Interceptor
- It is an aspect that contains only one advice.
AOP Proxy
- It is used to implement aspect contracts, created by AOP framework.
- It will be a JDK dynamic proxy or CGLIB proxy in spring framework.
Weaving
- It is the process of linking aspect with other application types or objects to create an advised object.
- Weaving can be done at compile time, load time or runtime. Spring AOP performs weaving at runtime.
4)AOP Implementations
- AspectJ
- Spring AOP
- JBoss AOP
5) 3 ways to use spring AOP
- By Spring1.2 Old style (dtd based) (also supported in Spring3)
- By AspectJ annotation-style
- By Spring XML configuration-style(schema based)
2.By Spring1.2 Old style (dtd based) (also supported in Spring3)
There are 4 types of advices supported in spring1.2 old style aop implementation.
Before Advice
it is executed before the actual method call.After Advice
it is executed after the actual method call. If method - returns a value, it is executed after returning value.Around Advice
it is executed before and after the actual method call.throws Advice
it is executed if actual method throws exception.
Understanding the hierarchy of advice interfaces
MethodBeforeAdvice interface
extends the BeforeAdvice interface
.
AfterReturningAdvice interface
extends the AfterAdvice interface
.
ThrowsAdvice interface
extends the AfterAdvice interface
.
MethodInterceptor interface
extends the Interceptor interface
. It is used in around advice
.
1)Before Advice
public class A{
public void m(){
System.out.println("actual business logic");}
}
public class BeforeAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("additional concern before actual logic");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A"></bean>
<bean id="before" class="com.amy.BeforeAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="a"></property>
<property name="interceptorNames">
<list>
<value>before</value>
</list>
</property>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= context.getBean("proxy",A.class);
a.m();
}
}
additional concern before actual logic
actual business logic
Understanding
ProxyFactoryBean class
:
The ProxyFactoryBean class
is provided by Spring Famework. It contains2 properties target and interceptorNames
.- The instance of A class will be considered as
target object
and the instance of advisor class asinterceptor
. You need to pass the advisor object as the list object as in the xml file given above.
before
public class ProxyFactoryBean{
private Object target;
private List interceptorNames;
//getters and setters
}
Printing additional information in
MethodBeforeAdvice
public class A{
public void m(){
System.out.println("actual business logic");}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A"></bean>
<bean id="before" class="com.amy.BeforeAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="a"></property>
<property name="interceptorNames">
<list>
<value>before</value>
</list>
</property>
</bean>
</beans>
public class BeforeAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("additional concern before actual logic");
System.out.println("method info:" +" method.getName() is "+ method.getName() + " method.getModifiers() is " + method.getModifiers());
System.out.println("argument info:");
for (Object arg : args) {
System.out.println("arg: "+ arg);
}
System.out.println("target Object:" + target);
System.out.println("target object class name: " + target.getClass().getName());
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= context.getBean("proxy",A.class);
System.out.println("proxy class name: "+a.getClass().getName());
a.m();
}
}
proxy class name: com.amy.A$$EnhancerBySpringCGLIB$$46ca4f4d
additional concern before actual logic
method info: method.getName() is m method.getModifiers() is 1
argument info:
target Object:com.amy.A@6a396c1e
target object class name: com.amy.A
actual business logic
2)After Advice
public class A{
public void m(){
System.out.println("actual business logic");}
}
public class AfterAdvisor implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("additional concern after returning advice");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A"></bean>
<bean id="after" class="com.amy.AfterAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="a"></property>
<property name="interceptorNames">
<list>
<value>after</value>
</list>
</property>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= context.getBean("proxy",A.class);
a.m();
}
}
actual business logic
additional concern after returning advice
3)Around Advice
public class A{
public void m(){
System.out.println("actual business logic");}
}
public class AroundAdvisor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object object;
System.out.println("additional concern before actual logic");
object=invocation.proceed();
System.out.println("additional concern after actual logic");
return object;
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A"></bean>
<!-- <bean id="before" class="com.amy.BeforeAdvisor"></bean>-->
<!-- <bean id="after" class="com.amy.AfterAdvisor"></bean>-->
<bean id="around" class="com.amy.AroundAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="a"></property>
<property name="interceptorNames">
<list>
<value>around</value>
</list>
</property>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= context.getBean("proxy",A.class);
a.m();
}
}
additional concern before actual logic
actual business logic
additional concern after actual logic
4)Throws Advice
public class Validator {
public void Validate(int age) throws ArithmeticException {
if(age<18){
throw new ArithmeticException("No Valid Age");
}else{
System.out.println("vote confirmed");
}
}
}
public class ThrowsAdvisor implements ThrowsAdvice {
public void afterThrowing(Exception ex){
System.out.println("additional concern if exception occurs");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="v" class="com.amy.Validator"></bean>
<!-- <bean id="before" class="com.amy.BeforeAdvisor"></bean>-->
<!-- <bean id="after" class="com.amy.AfterAdvisor"></bean>-->
<!-- <bean id="around" class="com.amy.AroundAdvisor"></bean>-->
<bean id="throw" class="com.amy.ThrowsAdvisor"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="v"></property>
<property name="interceptorNames">
<list>
<value>throw</value>
</list>
</property>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Validator v= context.getBean("proxy",Validator.class);
try{
v.Validate(12);
}catch (Exception e){
e.printStackTrace();
}
}
}
additional concern if exception occurs
java.lang.ArithmeticException: No Valid Age
at com.amy.Validator.Validate(Validator.java:10)
at com.amy.Validator$$FastClassBySpringCGLIB$$f91662a4.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:113)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
at com.amy.Validator$$EnhancerBySpringCGLIB$$629ab8c0.Validate(<generated>)
at com.amy.Test.main(Test.java:21)
Process finished with exit code 0
3.Spring AOP AspectJ Annotation
- The Spring Framework recommends you to use Spring AspectJ AOP implementation over the Spring 1.2 old style dtd based AOP implementation because it provides you more control and it is easy to use.
There are two ways to use
Spring AOP AspectJ implementation
:- By annotation: We are going to learn it here.
- By xml configuration (schema based): We will learn it in next page.
1)By annotation
Spring AspectJ AOP
implementation provides manyannotations
:@Aspect
declares the class as aspect.@Pointcut
declares the pointcut expression.The annotations used to create advices are given below:
@Before
declares the before advice. It is applied before calling the actual method.@After
declares the after advice. It is applied after calling the actual method and before returning result.@AfterReturning
declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.@Around declares
the around advice. It is applied before and after calling the actual method.@AfterThrowing
declares the throws advice. It is applied if actual method throws exception.
(1)Pointcut
- Pointcut is
an expression language of Spring AOP
. - The
@Pointcut annotation
is used to define the pointcut. We can refer the pointcut expression by name also.
@Pointcut(“execution( Operation.(..))”)
private void doSomething() {}
It will be applied on all the public methods.
@Pointcut("execution(public Operation.*(..))")
It will be applied on all the methods of Operation class.
@Pointcut("execution(* Operation.*(..))")
It will be applied on all the public setter methods of Employee class.
@Pointcut("execution(public Employee.set*(..))")
It will be applied on all the methods of Operation class that returns int value.
@Pointcut("execution(int Operation.*(..))")
(2)@Before
public class Operation {
public void msg(){
System.out.println("msg method invoked");}
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
package com.amy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* @author: Amy
* @create: 2023-02-20 15:05
**/
@Aspect
public class TrackOperation {
@Pointcut("execution(* Operation.*(..))")
public void k(){
}//pointcut name
@Before("k()")//applying pointcut on before advice
public void myadvice(JoinPoint jp){
//it is advice (before advice)
System.out.println("additional concern");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
System.out.println("calling msg...");
o.msg();
System.out.println("calling m...");
o.m();
System.out.println("calling k...");
o.k();
}
}
calling msg...
additional concern
msg method invoked
calling m...
additional concern
m method invoked
calling k...
additional concern
k method invoked
if you change the pointcut expression
before
@Pointcut("execution(* Operation.*(..))")
calling msg...
additional concern
msg method invoked
calling m...
additional concern
m method invoked
calling k...
additional concern
k method invoked
modify
@Pointcut("execution(* Operation.m*(..))")
Now additional concern will be applied for the methods starting with m in Operation class
calling msg...
additional concern
msg method invoked
calling m...
additional concern
m method invoked
calling k...
k method invoked//have no "additional concern"
(3)@After
public class Operation {
public void msg(){
System.out.println("msg method invoked");}
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
* @author: Amy
* @create: 2023-02-20 15:05
**/
@Aspect
public class TrackOperation {
@Pointcut("execution(* Operation.*(..))")
public void k(){
}//pointcut name
@After("k()")//applying pointcut on after advice
public void myadvice(JoinPoint jp){
System.out.println("additional concern");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
System.out.println("calling msg...");
o.msg();
System.out.println("calling m...");
o.m();
System.out.println("calling k...");
o.k();
}
}
calling msg...
msg method invoked
additional concern
calling m...
m method invoked
additional concern
calling k...
k method invoked
additional concern
(4) @AfterReturning
By using after returning advice, we can get the result in the advice.
public class Operation {
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
package com.amy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
/**
* @author: Amy
* @create: 2023-02-20 15:05
**/
@Aspect
public class TrackOperation {
@AfterReturning(
pointcut = "execution(* Operation.*(..))",
returning= "result")
public void myadvice(JoinPoint jp,Object result){
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Result in advice: "+result);
System.out.println("end of after returning advice...");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
System.out.println("calling m...");
o.m();
System.out.println("calling k...");
o.k();
}
}
calling m...
m method invoked
additional concern
Method Signature: int com.amy.Operation.m()
Result in advice: 2
end of after returning advice...
calling k...
k method invoked
additional concern
Method Signature: int com.amy.Operation.k()
Result in advice: 3
end of after returning advice...
(5) @Around
- The AspectJ around advice is applied before and after calling the actual business logic methods.
Here, we are assuming that applicationContext.xml file is same as given in @Before example.
public class Operation {
public void msg(){
System.out.println("msg() is invoked");}
public void display(){
System.out.println("display() is invoked");}
}
package com.amy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
* @author: Amy
* @create: 2023-02-20 15:05
**/
@Aspect
public class TrackOperation {
@Pointcut("execution(* Operation.*(..))")
public void k(){
}//pointcut name
@Around("k()")//applying pointcut on before advice
public Object myadvice(ProceedingJoinPoint jp) throws Throwable{
//it is advice (before advice)
System.out.println("Additional Concern Before calling actual method");
Object obj = jp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
o.msg();
o.display();
}
}
Additional Concern Before calling actual method
msg() is invoked
Additional Concern After calling actual method
Additional Concern Before calling actual method
display() is invoked
Additional Concern After calling actual method
(6)@AfterThrowing
By using after throwing advice, we can print the exception in the TrackOperation class.
public class Operation {
public void validate(int age)throws Exception{
if(age<18){
throw new ArithmeticException("Not valid age");
}
else{
System.out.println("Thanks for vote");
}
}
}
package com.amy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
/**
* @author: Amy
* @create: 2023-02-20 15:05
**/
@Aspect
public class TrackOperation {
@AfterThrowing(
pointcut = "execution(* Operation.*(..))",
throwing= "error")
public void myadvice(JoinPoint jp, Throwable error)//it is advice
{
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Exception is: "+error);
System.out.println("end of after throwing advice...");
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
try{
o.validate(19);
}catch(Exception e){
System.out.println(e);}
System.out.println("calling validate again...");
try{
o.validate(11);
}catch(Exception e){
System.out.println(e);}
}
}
Thanks for vote
calling validate again...
additional concern
Method Signature: void com.amy.Operation.validate(int)
Exception is: java.lang.ArithmeticException: Not valid age
end of after throwing advice...
java.lang.ArithmeticException: Not valid age
2)By xml configuration (schema based)
- Spring enables you to define the
aspects, advices and pointcuts in xml file.
aop:before
It is applied before calling the actual business logic method.aop:after
It is applied after calling the actual business logic method.aop:after-returning
it is applied after calling the actual business logic method. It can be used to intercept the return value in advice.aop:around
It is applied before and after calling the actual business logic method.aop:after-throwing
It is applied if actual business logic method throws exception.
(1) aop:before
public class Operation {
public void msg(){
System.out.println("msg method invoked");}
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
public class TrackOperation {
public void myadvice(JoinPoint jp)//it is advice
{
System.out.println("additional concern");
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="trackOperation">
<!-- @Before -->
<aop:pointcut id="pointCutBefore" expression="execution(* com.amy.Operation.*(..))"/>
<aop:before method="myadvice" pointcut-ref="pointCutBefore" />
</aop:aspect>
</aop:config>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
System.out.println("calling msg...");
o.msg();
System.out.println("calling m...");
o.m();
System.out.println("calling k...");
o.k();
}
}
calling msg...
additional concern
msg method invoked
calling m...
additional concern
m method invoked
calling k...
additional concern
k method invoked
(2)aop:after
public class Operation {
public void msg(){
System.out.println("msg method invoked");}
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
public class TrackOperation {
public void myadvice(JoinPoint jp)//it is advice
{
System.out.println("additional concern");
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="trackOperation">
<!-- @After -->
<aop:pointcut id="pointCutAfter" expression="execution(* com.amy.Operation.*(..))" />
<aop:after method="myadvice" pointcut-ref="pointCutAfter" />
</aop:aspect>
</aop:config>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Operation o= (Operation) context.getBean("op");
System.out.println("calling msg...");
o.msg();
System.out.println("calling m...");
o.m();
System.out.println("calling k...");
o.k();
}
}
calling msg...
msg method invoked
additional concern
calling m...
m method invoked
additional concern
calling k...
k method invoked
additional concern
(3)aop:after-returning
public class Operation {
public int m(){
System.out.println("m method invoked");return 2;}
public int k(){
System.out.println("k method invoked");return 3;}
}
public class TrackOperation {
public void myadvice(JoinPoint jp,Object result)//it is advice (after advice)
{
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Result in advice: "+result);
System.out.println("end of after returning advice...");
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="trackOperation">
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning" expression="execution(* com.amy.Operation.*(..))" />
<aop:after-returning method="myadvice" returning="result" pointcut-ref="pointCutAfterReturning" />
</aop:aspect>
</aop:config>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Operation o = (Operation) context.getBean("op");
System.out.println("calling m...");
System.out.println(o.m());
System.out.println("calling k...");
System.out.println(o.k());
}
}
calling m...
m method invoked
additional concern
Method Signature: int com.amy.Operation.m()
Result in advice: 2
end of after returning advice...
2
calling k...
k method invoked
additional concern
Method Signature: int com.amy.Operation.k()
Result in advice: 3
end of after returning advice...
3
(4)aop:around
public class Operation {
public void msg(){
System.out.println("msg() is invoked");}
public void display(){
System.out.println("display() is invoked");}
}
public class TrackOperation {
public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("Additional Concern Before calling actual method");
Object obj=pjp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="trackOperation">
<!-- @Around -->
<aop:pointcut id="pointCutAround" expression="execution(* com.amy.Operation.*(..))" />
<aop:around method="myadvice" pointcut-ref="pointCutAround" /> </aop:aspect>
</aop:config>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Operation o = (Operation) context.getBean("op");
o.msg();
o.display();
}
}
Additional Concern Before calling actual method
msg() is invoked
Additional Concern After calling actual method
Additional Concern Before calling actual method
display() is invoked
Additional Concern After calling actual method
(5)aop:after-throwing
public class Operation {
public void validate(int age)throws Exception{
if(age<18){
throw new ArithmeticException("Not valid age");
}
else{
System.out.println("Thanks for vote");
}
}
}
public class TrackOperation {
public void myadvice(JoinPoint jp,Throwable error)//it is advice
{
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Exception is: "+error);
System.out.println("end of after throwing advice...");
}
}
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="op" class="com.amy.Operation"></bean>
<bean id="trackOperation" class="com.amy.TrackOperation"></bean>
<aop:config>
<aop:aspect id="myaspect" ref="trackOperation">
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.amy.Operation.*(..))" />
<aop:after-throwing method="myadvice" throwing="error" pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
</aop:config>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Operation o = (Operation) context.getBean("op");
System.out.println("calling validate...");
try{
o.validate(19);
}catch(Exception e){
System.out.println(e);}
System.out.println("calling validate again...");
try{
o.validate(11);
}catch(Exception e){
System.out.println(e);}
}
}
calling validate...
Thanks for vote
calling validate again...
additional concern
Method Signature: void com.amy.Operation.validate(int)
Exception is: java.lang.ArithmeticException: Not valid age
end of after throwing advice...
java.lang.ArithmeticException: Not valid age
还没有评论,来说两句吧...