Spring AOP 注解实现

[b]使用aspectj注解实现的AOP需要引入aspectj的lib[/b]
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.0</version>
</dependency>


[b]1. 首先启用 Spring 对 @AspectJ 切面配置的支持[/b]

<?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:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<!-- scan all beans and inject dependence -->
<context:component-scan base-package="com.myproject"/>
<aop:aspectj-autoproxy/>
</beans>



[b]2.定义切面 Bean[/b]

@Component
@Aspect
public class LogInterceptor {

@Pointcut("execution (!void com.myproject.example.service..*(..))")
private void anyUnvoid(){}

@Pointcut("within(com.myproject.example.service..*)")
private void anyService(){}

@Pointcut("anyUnvoid() && anyService()")
private void anyUnvoidService(){}

@Before("anyUnvoidService() && args(username, password)")
public void doBefore(String username, String password){
System.out.println("doBefore: "+username);
}

/**
* 第二种访问目标参数的方法。<br>
* 将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时,
* 该 JoinPoint 参数就代表了织入增强处理的连接点。
*/
@Before("anyUnvoidService()")
public void doBefore2(JoinPoint jp){
Object[] args = jp.getArgs();
System.out.println("doBefore2: " +args[0]);
}

@AfterReturning(pointcut="anyUnvoidService()", returning="result")
public void doAfterRunning(String result){
System.out.println("doAfterReturning:" +result);
}

@AfterReturning(pointcut="execution(* com.myproject.example.service.AccountService.Login(..))", returning="result")
public void doAfterLogin(JoinPoint jp, Account result){
if(result!=null)
System.out.println(result.getUsername() +" login success.");
else
System.out.println(jp.getArgs()[0] +" login fail.");
}

@AfterThrowing(pointcut="anyUnvoidService()", throwing="ex")
public void doAfterThrowing(Throwable ex){
System.out.print("doAfterThrowing: "+ex.getMessage());
}

@After("anyUnvoidService()")
public void doAfter(){
System.out.println("doAfter");
}

/**
* Around 增强处理可替代所有其它增强处理。
* 它可改变执行目标方法的参数值,也可改变目标方法之后的返回值。
*/
@Around("anyUnvoidService()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
Object[] args = pjp.getArgs(); //obtain arguements
System.out.println("enter method: "+args[0]);
Object result = pjp.proceed();
System.out.println("exit method");
return result;
}

}



[b]3. 目标Bean[/b]

@Service
public class AccountService {

@Resource
private AccountDao accountDao;

public Account Login(String username, String password){
System.out.println(username + " want to login.");
return null;
}


}



[b]4 测试[/b]
	@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService =(AccountService)ac.getBean("accountService");
accountService.Login("Peter", "wetwe");
}



[b]测试结果:[/b]
enter method: Peter
doBefore: Peter
doBefore2: Peter
Peter want to login.
exit method
doAfter
doAfterReturning:null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值