spring_aop_annotaion
来源:互联网 发布:淘宝服务热线 人工 编辑:程序博客网 时间:2024/06/08 16:52
demo:
description:注解方式实现aop
Aspect.java
/** *切面 */@Component@org.aspectj.lang.annotation.Aspectpublic 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
@Componentpublic 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() 运行时异常
- spring_aop_annotaion
- c++学习之标准库异常类
- Virtualbox中的XP虚拟机和主机Win7之间的共享文件夹
- oracle自动共享内存管理(ASMM) .
- svn报Could not read status line: connection was closed by server(不能读状态行: 连接被服务器关闭)
- HTTP POST GET 本质区别详解
- spring_aop_annotaion
- NSArray和 Vector<struct>互转
- 標準C++的類型轉換:static_cast、dynamic_cast
- windows 环境变量
- SQL基础5
- 设计模式总结—创建型模式
- mysql 互为主备的简单搭建
- 快捷方式删除不成功的解决办法
- QT MD5 SHA1加密使用