在使用注解的时候,首先得在配置文件bean.xml中添加命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
然后在xsi:schemaLocation中添加:
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
bean.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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:component-scan base-package="com.test"></context:component-scan> <bean id="myInterceptor" class="com.test.aop.MyInterceptor"></bean> <bean id="myInterceptor2" class="com.test.aop.MyInterceptor2"></bean> <bean id="myInterceptor3" class="com.test.aop.MyInterceptor3"></bean> <aop:config> <aop:aspect ref="myInterceptor"> <aop:pointcut id="myMethod" expression="execution(public void com.test.dao.impl.UserDAOImpl.save(com.test.model.User))"/> <aop:before method="beforeMethod" pointcut-ref="myMethod"/> </aop:aspect> <aop:aspect ref="myInterceptor2"> <aop:pointcut id="myMethod2" expression="execution(public * com.test.dao..*.*(..))"/> <aop:before method="beforeMethod" pointcut-ref="myMethod2"/> <aop:after-returning method="afterReturningMethod" pointcut-ref="myMethod2"/> <aop:after-throwing method="afterThrowingMethod" pointcut-ref="myMethod2"/> <aop:around method="aroundMethod" pointcut-ref="myMethod2"/> </aop:aspect> <aop:aspect ref="myInterceptor3"> <aop:pointcut id="myMethod3" expression="execution(public * com.test.service..*.add(..))"/> <aop:before method="beforeMethod" pointcut-ref="myMethod3"/> <aop:after-returning method="afterReturningMethod" pointcut-ref="myMethod3"/> <aop:after-throwing method="afterThrowingMethod" pointcut-ref="myMethod3"/> <aop:around method="aroundMethod" pointcut-ref="myMethod3"/> </aop:aspect> </aop:config> </beans>
MyInterceptor.java
package com.test.aop; public class MyInterceptor { public void beforeMethod() { System.out.println("start mothod!"); } }
MyInterceptor2.java
package com.test.aop; import org.aspectj.lang.ProceedingJoinPoint; public class MyInterceptor2 { public void beforeMethod() { System.out.println("MyInterceptor2 before mothod!"); } public void afterReturningMethod() { System.out.println("MyInterceptor2 afterReturning mothod!"); } public void afterThrowingMethod() { System.out.println("MyInterceptor2 afterThrowing mothod!"); } public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("MyInterceptor2 around start mothod!"); pjp.proceed(); System.out.println("MyInterceptor2 around end mothod!"); } }
MyInterceptor3.java
package com.test.aop; import org.aspectj.lang.ProceedingJoinPoint; public class MyInterceptor3 { public void beforeMethod() { System.out.println("MyInterceptor3 before mothod!"); } public void afterReturningMethod() { System.out.println("MyInterceptor3 afterReturning mothod!"); } public void afterThrowingMethod() { System.out.println("MyInterceptor3 afterThrowing mothod!"); } public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("MyInterceptor3 around start mothod!"); pjp.proceed(); System.out.println("MyInterceptor3 around end mothod!"); } }
相关推荐
实现spring的aop的操作,采用AspectJ技术,通过xml的配置来实现,本人亲自测试过,aop相关架包已引入
spring xml 实现aop切面编程 内附注释,希望对入门的新手有帮助
Spring中的AOP代理还是离不开Spring的IOC容器,代理的生成,管理及其依赖关系都是由IOC容器负责,Spring默认使用JDK动态代理,在需要代理类而不是代理接口的时候,Spring会自动切换为使用CGLIB代理,不过现在的项目...
spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式进行AOP编程 spring-4 演示了spring与JDBCTemplate配合使用 spring-5 演示了声明式事务及使用xml配置文件处理事务
下面是一个在spring mvc中关于切面如何使用的例子,可以指直观的理解切面到底有什么作用 1、引用 AspectJ jar 包依赖 pom.xml 文件添加依赖 org.aspectj aspectjrt 1.9.2 2、创建两个新的包 ...
spring-aop实践项目,基于xml方式的面向切面实践小项目
Spring AOP 的实现例子(基于XML配置实现); 具体介绍看这里:https://blog.csdn.net/qq_22078107/article/details/85865543
10.面向切面编程AOP 11.手写AOP框架 12.Spring支持的AOP的实现 13.AOP常用的术语 14.什么是AspectJ框架 15.AspectJ常见通知类型 16.AspectJ 的切入点表达式(掌握) 17.AspectJ的前置通知@Before .... .... ....
采用注解模式,和XML模式对AOP面向切面编程进行测试。
2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving)的...
使用动态代理实现面向切面编程、基于AspectJ注解的方式配置AOP、基于XML文件的方式配置AOP
配置可以通过xml文件来进行,大概有四种方式: 1. 配置ProxyFactoryBean,显式地设置...3. 通过<aop:config>来配置(纯POJO切面) 4. 通过<aop: aspectj-autoproxy>来配置,使用AspectJ的注解来标识通知及切入点
AOP(Aspect-OrientedProgramming),面向切面编程 Spring AOP里面常用名词的概念: Spring容器的启动流程 Spring Bean的生命周期? Spring中bean的作用域 说一下Spring基于xml注入bean的几种方式? Spring如何解决循环...
面向切面编程(AOP --- aspect oriented programming) 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 ...
4 面向切面编程 52 4.1 代理模式 52 代理模式拓展: 52 4.1.1 JDK动态代理 58 JDK动态代理拓展: 59 4.1.2 CGLIB做代理 66 CGLIB动态代理拓展: 68 4.1.3 Spring的动态代理 71 4.2 AOP编程 71 4.2.1概念: 71 Spring...
Spring Framework简介:了解Spring框架的核心理念,包括依赖注入(DI)和面向切面编程(AOP)。 Spring Boot:熟悉Spring Boot,知道如何创建和管理Spring Boot项目,以及如何使用Spring Boot的自动配置功能。 ...
使用动态代理实现面向切面编程、基于AspectJ注解的方式配置AOP、基于XML文件的方式配置AOP
面向切面编程(AOP — aspect oriented programming) 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 ...
2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving...