demo:
description:注解方式实现aop
Aspect.java
/**
*切面
*/
@Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {
private Logger logger = Logger.getLogger(Aspect.class);
@Pointcut(value="execution(* com.asarja.aop.annotation.*.*(..))")
public void pointcut(){
}
/**
* 前置通知
*/
@Before(value="execution(* com.asarja.aop.annotation.*.*(..))")
public void doBefore(JoinPoint jp){
logger.debug("before "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
}
/**
* 后置通知
*/
@After(value="execution(* com.asarja.aop.annotation.*.*(..))")
public void doAfter(JoinPoint jp){
logger.debug("finally after "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
}
/**
* 环绕通知
*/
@Around(value="execution(* com.asarja.aop.annotation.*.*(..))")
public void doAround(ProceedingJoinPoint pjp) throws Throwable{
logger.debug("around before "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");
pjp.proceed();
logger.debug("around after "+pjp.getTarget().getClass().getName()+"."+pjp.getSignature().getName()+" execute");
}
/**
* 后置返回通知
*/
@AfterReturning(value="execution(* com.asarja.aop.annotation.*.*(..))")
public void doAfterRetuning(JoinPoint jp){
logger.debug("after return "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
}
/**
* 抛出异常后通知
*/
@AfterThrowing(value="execution(* com.asarja.aop.annotation.*.*(..))",throwing="e")
public void doThrowing(JoinPoint jp, Throwable e){
logger.debug("after throwing "+jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+" execute");
logger.debug(e.getMessage());
}
}
Dao.java
public interface Dao<T> {
void save(T t);
void select(T t);
void delete(T t);
}
DaoImpl.java
@Component
public class DaoImpl implements Dao<Mp3>{
@Override
public void save(Mp3 t) {
if(t.getId().equals(1)){
throw new RuntimeException("save() 运行时异常");
}
System.out.println("execute save()");
}
@Override
public void select(Mp3 t) {
System.out.println("execute select()");
}
@Override
public void delete(Mp3 t) {
System.out.println("execute delete()");
}
}
Mp3.java
public class Mp3 implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String url;
public Mp3() {
}
public Mp3(Integer id, String url) {
this.id = id;
this.url = url;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
applicationContext-aop.xml
<?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"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.asarja.aop.annotation" />
<!-- 打开aop注解 -->
<aop:aspectj-autoproxy />
</beans>
Test.java
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/asarja/aop/annotation/applicationContext-aop.xml");
Dao<Mp3> dao = (Dao<Mp3>)context.getBean("daoImpl");
dao.delete(new Mp3(2,"http://,,,,,"));
dao.select(new Mp3(1,"http://,,,,,"));
dao.save(new Mp3(1,"http://,,,,,"));
}
}
结果:
2013-04-09 16:32:13,535 [main] DEBUG com.asarja.aop.annotation.Aspect - before com.asarja.aop.annotation.DaoImpl.delete execute
2013-04-09 16:32:13,535 [main] DEBUG com.asarja.aop.annotation.Aspect - around before com.asarja.aop.annotation.DaoImpl.delete execute
execute delete()
2013-04-09 16:32:13,536 [main] DEBUG com.asarja.aop.annotation.Aspect - finally after com.asarja.aop.annotation.DaoImpl.delete execute
2013-04-09 16:32:13,536 [main] DEBUG com.asarja.aop.annotation.Aspect - around after com.asarja.aop.annotation.DaoImpl.delete execute
2013-04-09 16:32:13,536 [main] DEBUG com.asarja.aop.annotation.Aspect - after return com.asarja.aop.annotation.DaoImpl.delete execute
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - before com.asarja.aop.annotation.DaoImpl.select execute
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - around before com.asarja.aop.annotation.DaoImpl.select execute
execute select()
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - finally after com.asarja.aop.annotation.DaoImpl.select execute
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - around after com.asarja.aop.annotation.DaoImpl.select execute
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - after return com.asarja.aop.annotation.DaoImpl.select execute
2013-04-09 16:32:13,537 [main] DEBUG com.asarja.aop.annotation.Aspect - before com.asarja.aop.annotation.DaoImpl.save execute
2013-04-09 16:32:13,538 [main] DEBUG com.asarja.aop.annotation.Aspect - around before com.asarja.aop.annotation.DaoImpl.save execute
2013-04-09 16:32:13,538 [main] DEBUG com.asarja.aop.annotation.Aspect - finally after com.asarja.aop.annotation.DaoImpl.save execute
2013-04-09 16:32:13,538 [main] DEBUG com.asarja.aop.annotation.Aspect - after throwing com.asarja.aop.annotation.DaoImpl.save execute
2013-04-09 16:32:13,538 [main] DEBUG com.asarja.aop.annotation.Aspect - save() 运行时异常