spring的面向切面实现的两种方式
2016-01-26 17:47
501 查看
面向切面:主要应用在日志记录方面。实现业务与日志记录分离开发。
spring面向切面有两种实现方式:1、注解 2、xml配置。
1、注解实现如下:
(1)配置如下:
注意:红色部分的配置
(2)切入实现的代码如下:
2、xml配置实现方式
spring面向切面有两种实现方式:1、注解 2、xml配置。
1、注解实现如下:
(1)配置如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <context:annotation-config/> <aop:aspectj-autoproxy/> <bean id="Psv" class="com.test.spring.AOP.Personservice"/> <bean id="testAop" class="com.test.spring.AOP.testAop"/> </beans>
注意:红色部分的配置
(2)切入实现的代码如下:
@Aspect public class testAop { //指明所要代理的类或方法 @Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))") public void pointCut(){} @After("pointCut()") public void after(){ System.out.println("after"); } @Before("pointCut()") public void before(){ //JoinPoint joinPoint System.out.println("before"); } @Around("pointCut()") public Object around(ProceedingJoinPoint joinpoint){ //joinpoint.getArgs()能够得到传入到方法的参数 Object valu = null try { System.out.println("aaaaaaaa"); valu = joinpoint.proceed(); //返回所代理的方法(有返回值的方法)返回值 System.out.println(valu); } catch (Throwable e) { e.printStackTrace(); } return valu; } @AfterReturning("pointCut()") public void afteReturning(){ System.out.println("afteReturning"); } @AfterThrowing("pointCut()") public void afterThrowing(){ System.out.println("afterThrowing"); } }
2、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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <context:annotation-config/> <bean id="Psv" class="com.test.spring.AOP.Personservice"/> <bean id="testAop" class="com.test.spring.AOP.testAop"/> <aop:config> <aop:aspect id="Pservice" ref="testAop"> <!-- 配置指定切入的对象 --> <aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" /> <!-- 只匹配add方法作为切入点 <aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" /> --> <!-- 前置通知 --> <aop:before method="before" pointcut-ref="point_cut" /> <!-- 后置通知 returning指定返回参数 --> <aop:after-returning method="after" pointcut-ref="point_cut" returning="result" /> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="point_cut"/> <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/> </aop:aspect> </aop:config> </beans>
相关文章推荐
- Java提高篇(三五)-----Java集合细节(一):请为集合指定初始容量
- eclipse 下找不到或无法加载主类的解决办法
- Java——枚举类详解
- myEclipse 控制台右边的按钮作用
- System.setErr()
- 基本java打洞通信实现p2p
- Java提高篇(三四)-----fail-fast机制
- spring-关系数据库操作对象化
- Java类加载过程分析
- Spring Boot 部署与服务配置
- 2015-2016-2 《Java程序设计》 学生博客及Git@OSC 链接
- java并发前身CAS原理深度分析
- Java提高篇(三三)-----Map总结
- 基于小端规则的几个java方法
- Java的日期格式化常用方法
- 利用java的native2ascii转换编码
- Java提高篇(三二)-----List总结
- java常用正则表达式
- 在editplus、ue和eclipse中删除空行
- spring-JDBC模板操作