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

spring的aop实现

2014-03-27 22:27 260 查看
Spring默认AspectJ切入点语法

引入:将方法或字段添加到被处理的类中。

目标对象: 包含连接点的对象。也被称作 被通知或被代理对象。 

AOP代理:AOP框架创建的对象,对目标对象的加强。

织入:将增强处理添加到目标对象中,并创建一个被增强的对象的过程。

扫描加的注释和启动@Aspect所需要的jar包:



为了在Spring配置中使用@AspectJ切面,首先必须启用Spring对@AspectJ切面配置的支持,并确保自动代理(autoproxying)的bean是否能被这些切面通知。自动代理是指Spring会判断一个bean是否使用了一个或多个切面通知,并据此自动生成相应的代理以拦截其方法调用,并且确保通知在需要时执行。

通过在Spring的配置中引入下列元素来启用Spring对@AspectJ的支持:

<aop:aspectj-autoproxy/>


配置文件

xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

配置aop
增加验证验证文档
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop /spring-aop-4.0.xsd


UserDao.java
package net.csdn.www.dao;

import org.springframework.stereotype.Component;

@Component
public class UserDao {
public void save(){
System.out.println("用户保存成功!");
}
}


applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
<!-- 语言写入值xmlns:p -->
<!-- xmlns:context扫描加的注释 -->
<context:component-scan base-package="net.csdn.www.dao,net.csdn.www.aop">

<!-- 启动@Aspect支持 -->
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<aop:aspectj-autoproxy/>
</beans>


使用
execution
切入点指示符



execution(public * *(..))  所有的公共方法

execution(* set*(..))  以set开头的任意方法

execution(* com.xyz.service.AccountService.*(..)) com.xyz.service.AccountService类中的所有的方法

execution(* com.xyz.service.*.*(..))  com.xyz.service包中的所有的类的所有的方法

execution(* com.xyz.service..*.*(..)) com.xyz.service包及子包中所有的类的所有的方法

execution(* cn.itcast.spring.sh..*.*(String,?,Integer))  cn.itcast.spring.sh包及子包中所有的类的有三个参数

                                                            第一个参数为String,第二个参数为任意类型,

                                                            第三个参数为Integer类型的方法

定义成一个切面

LogAspect.java

package net.csdn.www.aop;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//定义成一个切面
@Aspect
public class LogAspect {
//定义一个切入点 ,访问修饰符,包名,类名,方法名(参数,异常)
@Before("execution(* net.csdn.www.dao.*.save*(..))")
public void saveLog() {
Logger log = Logger.getLogger(LogAspect.class.getName());
log.log(Level.INFO, "信息被保存");

}
}


测试代码:
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao=(UserDao) context.getBean("userDao");
userDao.save();

运行结果:

信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring

2014-3-27 21:35:00 net.csdn.www.aop.LogAspect saveLog

信息: 信息被保存

用户保存成功!

在UserDao.java中声明一个方法:

  public void delete(){
    int i= 5/0;
    }

@AfterThrowing(throwing="rvt",pointcut="execution(* net.csdn.www.dao.*.delete*(..))")
public void throwLog(Throwable rvt){
System.out.println("获取目标方法抛出的异常"+rvt);
System.out.println("记录日志");
}


调用delete()方法后的运行结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at net.csdn.www.dao.UserDao.delete(UserDao.java:15)
at net.csdn.www.dao.UserDao$$FastClassBySpringCGLIB$$c34aca31.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)获取目标方法抛出的异常java.lang.ArithmeticException: / by zero

记录日志

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at net.csdn.www.dao.UserDao$$EnhancerBySpringCGLIB$$d3fba046.delete(<generated>)
at net.csdn.www.test.Test.main(Test.java:14)

添加UserDao.java带返回值的方法

public String selectUser(String name){
System.out.println("用户信息查询成功");
return "success";
}
//定义一个Around的切入点
@Around("execution(* net.csdn.www.dao.*.select*(..))")
public Object selectLog(ProceedingJoinPoint pj) throws Throwable{
Logger log = Logger.getLogger(LogAspect.class.getName());
log.log(Level.INFO, "信息被查询");
//System.out.println(pj.getArgs()+"--");
Object result= pj.proceed(new String[]{"peitihuande zhi"});
return "peitihuande zhi";
}


运行结果:
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring

2014-3-27 22:28:00 net.csdn.www.aop.LogAspect selectLog

信息: 信息被查询

用户信息查询成功

peitihuande zhi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: