spring学习----aop配置demo
2017-08-11 10:34
369 查看
注意
aop:aspect 切面类是普通类即可 aop:advisor 切面类必须实现 advice接口 如:MethodBeforeAdvice AfterReturningAdvice等
定义通知(实现Advice)
public class MyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("=========我是afterReturn通知方法============="); } }
public class MyBeforeAdvice implements MethodBeforeAdvice{ @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("=========我是前置通知方法============="); } }
普通通知(不实现Advice)
public class MyNoAdvice { public void before() throws Throwable { System.out.println("=============我的普通的before前置方法===================="); } public void after() throws Throwable { System.out.println("=============我的普通的After后置方法===================="); } }
业务类
public interface IBussinessService { void bussiness(); void sayHello(); }
public class BussinessServiceImpl implements IBussinessService{ @Override public void bussiness() { System.out.println("=========我是业务方法=========="); } @Override public void sayHello() { System.out.println("=========我是say Hello=========="); } }
测试类
@Test public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:beans.xml"); IBussinessService bussinessService = applicationContext.getBean("bussinessService",IBussinessService.class); bussinessService.bussiness(); }
配置文件
<!-- ==========================测试AOP============================= --> <bean id="myBeforeAdvice" class="com.chenfei.advice.MyBeforeAdvice"></bean> <bean id="myAfterAdvice" class="com.chenfei.advice.MyAfterAdvice"></bean> <bean id="myNoAdvice" class="com.chenfei.advice.MyNoAdvice"/> <bean id="bussinessService" class="com.chenfei.service.impl.BussinessServiceImpl"></bean> <aop:config> <aop:pointcut id="pointtest1" expression="execution(* com.chenfei.service.impl.*.*(..))" /> <aop:aspect ref="myNoAdvice"> <aop:before method="before" pointcut-ref="pointtest1"/> <aop:after method="after" pointcut-ref="pointtest1"/> </aop:aspect> </aop:config> <aop:config> <aop:pointcut id="pointtest2" expression="execution(* com.chenfei.service.impl.*.*(..))" /> <aop:advisor pointcut-ref="pointtest2" advice-ref="myBeforeAdvice" /> <aop:advisor pointcut-ref="pointtest2" advice-ref="myAfterAdvice" /> </aop:config>
测试结果
相关文章推荐
- JavaWeb学习笔记-spring-17-AOP-基于schema配置切面
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- spring学习总结(九):AOP 基础及基于注解配置的AOP
- 重新学习之spring第二个程序,配置AOP面向切面编程
- Spring学习----------AOP以及Spring配置文件详解
- Spring学习4-面向切面(AOP)之schema配置方式
- Spring Aspectj AOP配置学习
- Spring学习笔记(四) JavaConfig创建Bean与AOP编程相结合的Demo
- 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式
- Spring学习----------AOP以及Spring配置文件详解
- aop切面配置不起作用引发的spring容器和spring mvc容器关系学习
- Spring AOP切面日志Demo 配置文件方式
- Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
- Spring学习记录(十三)---基于xml文件配置AOP
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)
- SSH与SSM学习之Spring16——Spring中AOP之使用配置文件配置
- Spring学习之使用注解配置AOP
- 【JavaEE学习笔记】Spring_03_IoC的其他配置方式,AOP浅析
- Spring-AOP配置(学习笔记)
- SSH与SSM学习之Spring17——Spring中AOP之使用注解配置