首页 / XML / Java AOP 注解配置与xml配置
Java AOP 注解配置与xml配置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java AOP 注解配置与xml配置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5292字,纯文字阅读大概需要8分钟。
内容图文
<? 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:jdbc ="http://www.springframework.org/schema/jdbc" 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.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!-- 配置自动扫描 --> < context:component-scan base-package ="cn.zr.aoptest" /> <!-- 注解方式 配置aop自动代理 --> < aop:aspectj-autoproxy /> <!-- xml方式 配置aop --> < bean id ="personDaoImpl" class ="cn.zr.aoptest.dao.person.impl.PersonDaoImpl" ></ bean > < bean id ="personManager" class ="cn.zr.aop.utils.PersonManager" ></ bean > < aop:config > < aop:pointcut expression ="execution(* cn.zr.aoptest.dao.person.impl.*.*(..))" id ="pointcut" /> < aop:aspect id ="xmlaop" ref ="personManager" > < aop:before method ="beforeInfo" pointcut-ref ="pointcut" /> < aop:after-returning method ="afterReturnInfo" pointcut-ref ="pointcut" /> < aop:after method ="afterInfo" pointcut-ref ="pointcut" /> < aop:after-throwing method ="exceptionInfo" throwing ="throwable" pointcut-ref ="pointcut" /> < aop:around method ="aroundInfo" pointcut-ref ="pointcut" /> </ aop:aspect > </ aop:config > </ beans >
package cn.zr.aop.utils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class PersonManager { public void beforeInfo(JoinPoint joinPoint){ System.out.println("获取信息...前"); //获取参数 Object[] objects = joinPoint.getArgs(); for (Object object : objects) { System.out.println(object); } } publicvoid afterReturnInfo(){ System.out.println("获取信息...后"); } publicvoid afterInfo(){ System.out.println("获取信息...最终"); } publicvoid exceptionInfo(Throwable throwable){ System.out.println("出现异常:"+throwable); } public Object aroundInfo(ProceedingJoinPoint pjp){ Object obj = null; System.out.println("=== 环绕前 ==="); try { obj = pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("=== 环绕后 ==="); return obj; } }
package cn.zr.aoptest.dao.person.impl; public class PersonDaoImpl { public void getInfo() { int num = 10/0; System.out.println("获取信息"); } }
package cn.zr.aoptest.dao.person.impl; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Component @Aspect public class TransactionManager { // 设置切点 @Pointcut("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))") publicvoid methodPointcut() { } // 前置通知 @Before(value=("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))")) publicvoid beginTransactionManager(JoinPoint jp){ System.out.println("...开始事务..."); Object[] objs = jp.getArgs(); for (Object object : objs) { System.out.println(object+"!!!"); } } // 后置通知 @AfterReturning("methodPointcut()") publicvoid commitTrasactionManager(){ System.out.println("===提交事务==="); } // 最终通知(finally) @After("methodPointcut()") publicvoid finallyManager(){ System.out.println("~~~无论是否异常,始终执行~~~"); } // 异常通知 //@AfterThrowing(pointcut = "controllerAspect()", throwing="e") @AfterThrowing(value="methodPointcut()",throwing="ep") publicstaticvoid exceptionManager(Throwable ep){ System.out.println("<<<事务回滚>>>,exception:"+ep); } // 环绕通知 @Around("methodPointcut()") public Object aroundTrasactionManager(ProceedingJoinPoint joinPoint) { Object obj = null; System.out.println("---=围绕通知前=---"); try { obj = joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("***=围绕通知后=***"); return obj; } }
package cn.zr.aoptest.Userdao.impl; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl { /** * 添加用户操作 */ public void addUser(String name){ System.out.println("我要进行添加用户操作"+name); int count = 0; int num = 100/count; } }
package cn.zr.aoptest.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.zr.aoptest.Userdao.impl.UserDaoImpl; import cn.zr.aoptest.dao.person.impl.PersonDaoImpl; public class BeanTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDaoImpl impl = (UserDaoImpl) ac.getBean(UserDaoImpl.class); impl.addUser("lf"); PersonDaoImpl personDaoImpl = (PersonDaoImpl) ac.getBean("personDaoImpl"); personDaoImpl.getInfo(); } }
原文:http://www.cnblogs.com/lantu1989/p/6411467.html
内容总结
以上是互联网集市为您收集整理的Java AOP 注解配置与xml配置全部内容,希望文章能够帮你解决Java AOP 注解配置与xml配置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。