您的位置:首页 > 编程语言 > Java开发

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的内部实现原理是动态代理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: