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

Spring 面向切面变成基本案例(AOP)

2017-05-04 11:01 288 查看
< 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:context="http://www.springframework.org/schema/context"
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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
<!-- 最简单的注入 p 命名空间 -->
<bean id="person" class="spring.beans.Person"
p:personName="张三"
p:personSex="男"
p:personAge="1"
></bean>

<!-- 自动生成所有被 @Aspect 标注修饰的类 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<!-- 设置 Spring 容器扫描哪些包下面的 JavaBean, * 代表所有包 -->
<context:component-scan base-package="spring.beans"></context:component-scan>

</beans>


< 二 > 切面类的定义 ( 前置通知: @Before, 后置通知: @After, 正常返回通知: @AfterReturning, 异常返回通知: @AfterThrowing)

package spring.beans;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect // 声明切面类
@Component // 声明是一个 JavaBean
public class MyAop {

// ( 方法执行前的方法 ) execution 是切点表达式
@Before("execution (public String spring.beans.User.toString())")
public void before() {
System.out.println("前置方法");
}

// ( 方法执行后的方法 ) 注: 不管正常执行或者抛出异常
@After("execution (public String spring.beans.User.toString())")
public void after() {
System.out.println("后置处理");
}

// ( 方法正确执行后的方法 ) 参数(切点表达式, 返回值变量)
@AfterReturning(pointcut="execution (public String spring.beans.User.toString())", returning="result")
public void returning(JoinPoint join, String result){
System.out.println("方法名: " + join.getSignature().getName());
System.out.println("结果为: " + result);
}

// ( 方法抛出异常时执行的方法 ) 参数(切点表达式, 抛出的异常变量)
@AfterThrowing(pointcut="execution (public String spring.beans.User.toString())", throwing="ex")
public void throwing(JoinPoint join, Exception ex){
System.out.println(ex.getMessage());
}
}


< 3 > 切面类的定义 ( 环绕通知: @Around ) 注: 功能强大但是不常用

package spring.beans;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect // 声明切面类
@Component // 声明是一个 JavaBean
public class MyAop_02 {

// 环绕通知
@Around("execution (public String spring.beans.Person.toString())")
public void around(ProceedingJoinPoint join){
try {
System.out.println("方法调用之前");
String result = (String) join.proceed();
System.out.println("正常返回结果" + result);
} catch (Throwable e) {
System.out.println("出现了异常喽");
e.printStackTrace();
} finally {
System.out.println("方法调用之后");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: