AOPImpl.java
==================
package salesdepart.service.app;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
import org.springframework.core.annotation.Order;
import java.util.*;
@Aspect
@Order(2)
public class AOPImpl {
//apply to salesdepart.service.app'all classes's methods
@Before("execution(* salesdepart.service.app.*.*(..))")
public void applySecondauth(){
System.out.println("@Order(2) AOP@Before Apply applySecondauth() ===>");
System.out.println("@Order(2) AOP@Before Apply applySecondauth() <===");
}
@AfterReturning(returning="rvt"
, pointcut="execution(* salesdepart.service.app.*.*(..))")
public void log(Object rvt){
System.out.println("AOP@AfterReturning begin ===>");
System.out.println("AOP apply AfterReturningAOP(). return value:" + rvt);
System.out.println("record it to logs file...");
System.out.println("AOP@AfterReturning end <==");
}
@After("execution(* salesdepart.service.app.*.*(..))")
public void release(JoinPoint jp)
{
System.out.println("AOP@After :begin ===>");
//返回被织入增强处理的目标方法
System.out.println("AOP@After: weave name: "
+ jp.getSignature().getName());
//访问执行目标方法的参数
System.out.println("AOP@After:this method args:"
+ Arrays.toString(jp.getArgs()));
//访问被增强处理的目标对象
System.out.println("AOP@After:target to be weavered: "
+ jp.getTarget());
System.out.println("AOP@After:ends <====");
}
@Around("execution(* salesdepart.service.app.*.*(..))")
public Object processTx(ProceedingJoinPoint jp)
throws java.lang.Throwable
{
System.out.println("AOP@Around begin ===>");
//访问执行目标方法的参数
Object[] args = jp.getArgs();
//当执行目标方法的参数存在,
//且第一个参数是字符串参数
if (args != null && args.length > 0
&& args[0].getClass() == String.class)
{
//改变第一个目标方法的第一个参数
args[0] = "around changed your parameter";
}
//执行目标方法,并保存目标方法执行后的返回值
Object rvt = jp.proceed(args);
System.out.println("AOP@Around end <===");
return rvt;
}
@AfterThrowing(throwing="ex"
, pointcut="execution(* salesdepart.service.app.*.*(..))")
public void doRecoveryActions(Throwable ex)
{
System.out.println("AOP@AfterThrowing begin ===>");
System.out.println("AOP@AfterThrowing : exception from target method:" + ex);
System.out.println("AOP@AfterThrowing : take some actions");
System.out.println("AOP@AfterThrowing end <===");
}
}
AOPImpl2.java
==================
package salesdepart.service.app;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
import org.springframework.core.annotation.Order;
import java.util.Arrays;
@Aspect
@Order(1)
public class AOPImpl2 {
//定义Before增强处理执行 as first step
@Before("execution(* salesdepart.service.app.*.*(..))")
public void FirstStepAuth(JoinPoint jp)
{
System.out.println("@Order(1) AOP@Before: apply 1st auth check begin ==>");
System.out.println("@Order(1) AOP@Before: apply 1st auth check end <==");
}
}
SalesDepartEmployee .java
====================
package salesdepart.service.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import java.util.*;
@Component("SalesEmployee")
public class SalesDepartEmployee implements employee {
//@Autowired默认按类型装配,@Resource默认按名称装配
private String name;
//默认按类型装配
private DummyDAO mydao;
public SalesDepartEmployee() {
System.out.println(" SalesDepartEmployee() called");
name=" default";
mydao=null;
}
@Override
public void displayInfo() {
System.out.println("this is Sales depart' employee "+name);
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
}
BeanTest .java
===========
package salesdepart.service.app;
import org.springframework.context.*;
import org.springframework.context.support.*;
import java.util.*;
public class BeanTest {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("employee.xml");
employee p=(employee)ctx.getBean("SalesEmployee");
p.displayInfo();
}
}
=========
输出结果
=============
DummyDAO() called
SalesDepartEmployee() called
AOP@Around begin ===>
@Order(2) AOP@Before Apply applySecondauth() ===>
@Order(2) AOP@Before Apply applySecondauth() <===
this is Sales depart' employee default
AOP@Around end <===
AOP@After :begin ===>AOP@AfterReturning end <==
从上面可以看出:
Spring首先执行依赖注入,调用无参数构造函数,完成对象初始化。
当执行调用方法时候,
首先执行第一个
Before(1) begin
Before(1) end
Invoke() begin
Before(2) begin
Before(2) end
被拦截的方法执行()
invoke() end
after() begin
after() end
AfterReturning() begin
AfterReturning() end