Java AOP 注解配置与xml配置
2017-02-17 20:40
204 查看
<?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); } } public void afterReturnInfo(){ System.out.println("获取信息...后"); } public void afterInfo(){ System.out.println("获取信息...最终"); } public void 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.*(..))") public void methodPointcut() { } // 前置通知 @Before(value=("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))")) public void beginTransactionManager(JoinPoint jp){ System.out.println("...开始事务..."); Object[] objs = jp.getArgs(); for (Object object : objs) { System.out.println(object+"!!!"); } } // 后置通知 @AfterReturning("methodPointcut()") public void commitTrasactionManager(){ System.out.println("===提交事务==="); } // 最终通知(finally) @After("methodPointcut()") public void finallyManager(){ System.out.println("~~~无论是否异常,始终执行~~~"); } // 异常通知 //@AfterThrowing(pointcut = "controllerAspect()", throwing="e") @AfterThrowing(value="methodPointcut()",throwing="ep") public static void 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(); } }
相关文章推荐
- spring aop的使用(注解方式以及基于xml配置方式)
- Hibernate中持久化注解的java文件在applicationContext.xml中的配置
- 【JavaWeb-24】AOP介绍和术语、手动/半自动/自动实现AOP、基于XML和注解的AspectJ使用、JdbcTemplate的使用
- java自定义注解实现对象到xml配置的转换
- SpringAOP切点配置(注解与xml配置)
- Spring aop注解方式和xml配置方式
- 从头认识Spring-2.8 基于java注解的配置(基本没有xml)
- Spring使用AspectJ注解和XML配置实现AOP
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)
- mybatis简单sql使用java注解而不是xml配置
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- spring AOP 之 xml 配置实现(附 Java 代码实例)
- spring aop的使用(注解方式以及基于xml配置方式)
- spring注解时AOP失效,XML配置<bean>正常
- 实例说明Spring实现AOP的2种方式(注解和XML配置)
- mybatis简单sql使用java注解而不是xml配置
- Spring 基于XML配置 基于注解配置 基于JAVA类配置比较以及适用场景
- spring AOP 之 注解 配置实现(附 Java 代码实例)