spring学习(七)—AOP通过配置文件方式实现
2017-02-10 17:29
886 查看
转自传智博客学习视频
主要包括:通过xml文件进行前置增强,后置增强,环绕增强的具体样例
代码价构:
![](http://img.blog.csdn.net/20170210172901425?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHZ5dWFuMzAyNzY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1.配置文件
2.被增强类
3.增强类
4.测试类
主要包括:通过xml文件进行前置增强,后置增强,环绕增强的具体样例
代码价构:
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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <bean id="book" class="aop.Book"></bean> <bean id="myBook" class="aop.MyBook"></bean> <aop:config> <!-- 1.配置切入点 --> <aop:pointcut expression="execution(* aop.Book.*(..))" id="pointcut1"/> <!-- 2.配置切面,将增强用到方法上面 --> <!-- 指定增强类 --> <aop:aspect ref="myBook"> <!-- 配置增强类型 ,method:增加的类中使用哪个方法进行前置增强,pointcut-ref:增强用到哪个切入点上--> <aop:before method="before1" pointcut-ref="pointcut1"/> <!-- 后置增强 --> <aop:after method="after1" pointcut-ref="pointcut1"/> <aop:around method="around1" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config> </beans>
2.被增强类
package aop; public class Book { public void add(){ System.out.println("add........."); } }
3.增强类
package aop; import org.aspectj.lang.ProceedingJoinPoint; public class MyBook { public void before1() { System.out.println("前置增强"); } public void after1() { System.out.println("后置增强"); } public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 环绕通知:方法之前 System.out.println("环绕通知:方法之前"); // 执行被强的方法 proceedingJoinPoint.proceed(); // 环绕通知:方法之后 System.out.println("环绕通知:方法之后"); } }
4.测试类
package aop; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test public void testBook(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Book book = (Book) context.getBean("book"); book.add(); } }
相关文章推荐
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
- 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)
- spring学习(二)—spring 的bean的实例化方式(通过配置文件)
- Spring整理12 -- 面对切面(AOP)2 -- 配置文件的方式实现AOP
- Spring---->采用静态配置文件方式实现AOP
- spring学习笔记(22)----基于配置文件的方式来配置AOP
- spring中通过配置文件方式实现定时任务
- 用Spring配置文件或者注解方式实现AOP
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)
- Spring-AOP 通过配置文件实现 引介增强
- 用Spring配置文件或者注解方式实现AOP
- Spring整理12 -- 面对切面(AOP)2 -- 配置文件的方式实现AOP
- Spring学习笔记(四)Spring配置文件方式实现AOP
- Spring-AOP 通过配置文件实现 异常抛出增强
- Spring-AOP 通过配置文件实现 后置增强
- spring-aop 配置文件方式实现
- Spring学习历程---使用配置文件实现AOP
- Spring-AOP 通过配置文件实现 环绕增强
- Spring---->采用静态配置文件方式实现AOP
- [原]spring学习笔记9.3-Spring对Aop的支持-配置文件的方式