Spring框架 aop操作的注解方法 基于aspectj的自动注解aop方法
2018-01-01 00:42
756 查看
首先是在xml配置文件中配置好对象,然后开启aop的注解方法——即<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
xml代码如下:
上面有原来xml配置aop的方法,这时已经不用了,用作参考
被增强的类及方法,代码如下:
用于增强的类及方法,代码如下:
类的上边用@Aspect表示切面
方法前用@Before(value="表达式") 其中表达式与之前配置方法相同
最后测试类,代码如下:
其实就一句
response.getWriter().append(book.fun());
网页上返回值为around()中返回值,console控制台得到前后包围所有增强的输出。
如下图:
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: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/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="com.swift"></context:component-scan> <bean id="book" class="com.swift.Book"></bean> <bean id="adviceBook" class="com.swift.AdviceBook"></bean> <!-- 开启aop注解方法 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 这是xml 配置文件中aop操作的方法留下 4000 对比 <aop:config> <aop:pointcut expression="execution(* com.swift.Book.*())" id="pointcut1"/> <aop:aspect ref="adviceBook"> <aop:before method="before" pointcut-ref="pointcut1"/> <aop:after-returning method="after" pointcut-ref="pointcut1"/> <aop:around method="around" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config> --> </beans>
上面有原来xml配置aop的方法,这时已经不用了,用作参考
被增强的类及方法,代码如下:
package com.swift; public class Book { public String fun() { System.out.println("This is Book's fun().............."); return "This is Book's fun().............."; } }
用于增强的类及方法,代码如下:
package com.swift; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class AdviceBook { @Before(value="execution(* com.swift.Book.*(..))") public String before() { System.out.println("This is AdviceBook's before()..............."); return "This is AdviceBook's before()..............."; } @AfterReturning(value="execution(* com.swift.Book.*(..))") public String after() { System.out.println("This is AdviceBook's after()..............."); return "This is AdviceBook's after()..............."; } @Around(value="execution(* com.swift.Book.*(..))") public String around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("This is AdviceBook's front()..............."); proceedingJoinPoint.proceed(); System.out.println("This is AdviceBook's end()..............."); return "This is AdviceBook's around()..............."; } }
类的上边用@Aspect表示切面
方法前用@Before(value="表达式") 其中表达式与之前配置方法相同
最后测试类,代码如下:
package com.swift; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.swift.Book; @WebServlet("/test") public class ServleTest extends HttpServlet { private static final long serialVersionUID = 1L; public ServleTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml"); Book book=(Book)context.getBean("book"); //这个返回值,反复试验得出最后浏览上只输出This is AdviceBook's around(). response.getWriter().append(book.fun()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
其实就一句
response.getWriter().append(book.fun());
网页上返回值为around()中返回值,console控制台得到前后包围所有增强的输出。
如下图:
相关文章推荐
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(注解方式)
- spring框架的学习(四)——Spring的AOP的概述及AOP的操作(基于aspectj的xml方式)
- day3_Spring_02_基于aspectj的注解aop操作
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
- 【JavaWeb-24】AOP介绍和术语、手动/半自动/自动实现AOP、基于XML和注解的AspectJ使用、JdbcTemplate的使用
- Spring(十二)AspectJ框架开发AOP(基于注解)
- Spring基于注解@AspectJ的AOP
- 基于AspectJ的注解方式进行AOP开发
- Spring Aop(二)——基于Aspectj注解的Aop简单实现
- 10、SSM框架-Spring AOP之基于注解的声明式AspectJ(10)
- Spring_Spring与AOP_AspectJ基于注解的AOP实现
- 让spring3.0可以使用aop注解的方法是导入两个jar包aspectjrt.jar aspectjweaver.jar
- spring-06 之AOP 面向切面编程二 基于注解和基于xml配置文件的aop操作
- AOP之基于AspectJ注解总结与案例
- springAOP编程-aspectJ开启注解自动代理
- Intellij IDEA创建Spring项目之基于Aspectj的AOP操作(XML配置方式)
- 基于aspectj的aop操作
- SSM框架项目搭建系列(七)—Spring AOP之基于注解的声明式AspectJ
- Spring Aop之AspectJ注解配置实现日志管理的方法