Spring--Aop_xml
2017-09-13 20:05
204 查看
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:p="http://www.springframework.org/schema/p" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" xmlns:context="http://www.springframework.org/schema/context"> <context:component-scan base-package="com.dw"/> <!-- <aop:aspectj-autoproxy/> --> <bean id = "car" class = "com.dw.car.impl.Tanke"/> <bean id = "iterception" class="com.dw.model.Iterception"/> <aop:config> <aop:pointcut expression="execution(* com.dw.car.*.*(..))" id="point"/> <aop:aspect id="aspect" ref="iterception"> <aop:before method="before1" pointcut-ref="point"/> <aop:after-returning method="after1" pointcut-ref="point"/> <aop:around method="around1" pointcut="execution(* com.dw.car.*.*(..))"/> </aop:aspect> </aop:config> </beans>
Iterception.java:
package com.dw.model;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//@Aspect
//@Component
public class Iterception {
// @Pointcut("execution(* com.dw.car.*.*(..))")
// public void myMethod(){};
//
// //@Before("execution(public void com.dw.car.impl.Tanke.move())")
// @Before("execution(* com.dw.car.*.*(..))")
// public void before() {
// System.out.println("exec before");
//}
//
// @AfterReturning("execution(* com.dw.car.*.*(..))")
// public void afterReturning() {
// System.out.println("exec after");
// }
public void before1() {
System.out.println("before……");
}
public void after1() {
System.out.println("after……");
}
public void around1(ProceedingJoinPoint pjp) {
System.out.println("around 1 ……");
System.out.println("around 2 ……");
try {
pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("around 3 ……");
}
}
输出:
before……
around 1 ……
around 2 ……
tanke is moving!
around 3 ……
after……
Aspectj是一个面向切面的框架,使用时需要引入相关jar包。aop的内部实现原理是动态代理。
相关文章推荐
- Spring中启用AOP时要使用ApplicationContext而不要使用XmlBeanFactory
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring基于XML配置AOP
- spring aop相关概念和xml与注解实现方式
- Spring 一二事(9) - xml 形式的 AOP
- spring中aop的xml配置方法实例详解
- Spring 4.0 学习日记(9) ---XML配置实现AOP切面
- spring applicationContext.xml 文件中aop:aspectj-autoproxy
- Spring的AOP使用xml配置
- Spring笔记——AOP(XML方式)
- Spring aop注解方式和xml配置方式
- spring使用aop时xml中的配置
- Spring之AOP XML方式
- 17、(知识篇)Spring使用AOP(XML方式)
- Spring Aop实例之xml配置
- Spring AOP详解及通过XML配置实现的实现方法
- xml方式---spring的AOP拦截用户操作
- [5] Spring中的AOP操作(使用xml 配置文件的方式)
- 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
- 配置xml方式实现Spring的aop