`

Aop

 
阅读更多

一些介绍:

1.面向方面编程是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即

   2.应用:

       AOP 主要应用于日志记录,性能统计,安全控制 , 事务处理等方面。

在spring中配置aop

<?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-2.5.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy />  <!– 支持AOP的注解方式-->
</beans>

Annotation 方式配置 AOP

package com.yang.interceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.yang.bean.User;

/**
 * 日志拦截器
 */
@Aspect
@Component
public class TestInterceptor {
	@Pointcut("execution(public  *  com.yang.test.*.test*(..))")  //声明一个名字为log的切入点
	public void log(){
	}
	@Before("log()"+"&&args(user)")   //使用pointcut-->log .前置通知.处理传入的参数跟下面的形参保持一致即可。
	public void before(User user){
		System.out.println("方法执行前:"+user.getUname());
		user.setUname("lisi");
		System.out.println("方法执行前!");
	}
	@After("log()")   //最终后置通知。不管有无异常,最终都会执行!
	public void after(){
		System.out.println("方法执行后!");
	}
	@AfterReturning("log()")  //方法正常结束,后置通知
	public void afterReturning(){
		System.out.println("方法执行后!after returning!");
	}
	@AfterThrowing("log()")    //抛出异常会执行,异常通知
	public void afterThrowing(){
		System.out.println("方法执行后!after throwing!");
	}
	@Around("log()")   //环绕通知在一个方法执行之前和之后执行
	public Object around(ProceedingJoinPoint pjp){
		System.out.println("环绕通知,方法执行前!");
		Object obj = null;
		try {
			obj = pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("环绕通知,方法执行后!");
		return obj;
	}
}
package com.yang.test;
import org.springframework.stereotype.Component;
import com.yang.bean.User;
@Component("userDao")
public class UserDao {
	public void test1(User user){
		System.out.println(user.getUname()); 
		System.out.println("UserDao.test1()");
	}
	public void test2(){
		System.out.println("UserDao.test2()");
	}
	
	public void aaa(){
		System.out.println("UserDao.aaa()");
	} 
}
package com.yang.bean;

public class User {
	private int id;
	private String uname;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
}
package com.yang.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yang.bean.User;
public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
		UserDao us = (UserDao) context.getBean("userDao");
		User u  = new User();
		u.setUname("zhangsan");
		us.test1(u);
		us.test2();
		us.aaa();
	}
}

 个人的理解:

TestInterceptor中的方法中对应的注解最后是根据反射拿到的。

 

@Pointcut("execution(public  *  com.yang.test.*.test*(..))")  //声明一个名字为log的切入点

Pointcut中的注解是使用正则进行解析的,当然中间也有一些替换。

 

xml中配置xop

<?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-2.5.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context-2.5.xsd
">

	<aop:aspectj-autoproxy /> <!-- 支持注解方式 -->
	<context:component-scan  base-package="com.yang"/>
	
	<!-- 采用配置方式使用AOP -->
	<bean id="logInterceptor" class="com.yang.spring.aop.LogInterceptor"></bean>
	
	<aop:config >
		<!--定义切点 名字log  -->
		<aop:pointcut expression="execution(public !java.lang.String com.yang.spring.aop.UserDaoImpl.*(com.bjsxt.spring.aop.User,..))" id="log"/>
		
		<!-- 定义切面   logAspect-->
		<aop:aspect id="logAspect" ref="logInterceptor">
		
		<!-- 定义前置通知  -->
		<!-- arg-names="user,age,name,test" 多参数   arg-names 指定before参数名   -->
		  	<aop:before method="before" arg-names="user" pointcut="execution(!java.lang.String com.yang.spring.aop.UserDaoImpl.add(..)) and args(user)" />
		  <!-- 后置通知 -->
			<aop:after method="after"  pointcut-ref="log" />
			
			<aop:after-throwing method="afterThrowing" pointcut-ref="log"/>
			
			<aop:after-returning method="afterReturning" pointcut-ref="log"/>
			
			<aop:around method="around"  pointcut-ref="log"/>
		</aop:aspect>
		 
		
	</aop:config>
	
</beans>

 arg-names是为了显式地定义before方法中对应的参数的名字。

pointcut="execution(!java.lang.String com.yang.spring.aop.UserDaoImpl.add(..)) and args(user)" />
我的理解就是相当于查add方法,参数个数为1个,类型为user,(必须要有对应的User类,before中的before方法必须是
before(User user)类型的)
分享到:
评论
2 楼 bsszds 2012-10-10  
可以看看java编程思想第二十章了解下注解的基本原理,然后再去看spring的注解,hibernate的注解,或者其他框架里的注解,估计就会明白些了!
 
1 楼 chenzheng8975 2012-10-10  
注解的方式不好理解啊

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...

    Java利用spring aop进行监测方法执行耗时

    使用 Spring AOP 进行方法耗时监测的好处有以下几点: 1. 代码实现简单,易于维护:使用 Spring AOP 可以将耗时监测的逻辑与业务逻辑进行解耦,避免业务逻辑代码的冗余和代码维护难度的提高。 2. 安全性高:使用 ...

    开发工具 aopalliance-1.0

    开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...

    Asp.net Core 3.1基于AspectCore实现AOP实现事务、缓存拦截器功能

    AOP的概念也很好理解,跟中间件差不多,说白了,就是我可以任意地在方法的前面或后面添加代码,这很适合用于缓存、日志等处理。 在net core2.2时,我当时就尝试过用autofac实现aop,但这次我不想用autofac,我用了一...

    spring aop spring aop

    spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop

    springBoot+aop+自定义注解+本地线程实现统一接口日志及接口响应时长

    内容概要:springboot+拦截器+aop+自定义注解+本地线程实现统一接口日志记录,记录下接口所在模块、接口描述、接口请求参数、接口返回参数、接口请求时间以及接口耗时用于接口优化,接口记录参数以及操作人防止使用...

    spring aop 实现源代码--xml and annotation(带lib包)

    在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...

    AOP事务所需要的jar包

    java项目中 AOP事务所需要用的jar包....................................啊..........................................................

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    基于注解实现SpringAop

    基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop

    aop所依赖的所有包

    aop所依赖的所有包+文档+源码,最新版全套aop aspectjweaver aopalliance aspects aspectjrt

    反射实现 AOP 动态代理模式(Spring AOP 的实现原理)

    AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理

    spring aop jar 包

    spring aop jar 包

    aop面向切面需要的jar包

    在使用spring的aop功能时,这两个jar是必须的,否则会报错,如下: Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net....

    Spring_aop源码

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象...

    AOP.in..NET

    AOP in .NET: Practical Aspect-Oriented Programming 296 pages Publisher: Manning Publications; Pap/Psc edition (June 25, 2013) Language: English ISBN-10: 1617291145 ISBN-13: 978-1617291142 ...

    SpringBoot下的SpringAOP-day04-源代码

    SpringBoot下的Spring——DAY04——动态代理总结、AOP、自定义注解进行拦截、动态获取注解参数、通知方法 1.动态代理总结 1.1 JDK动态代理特点 1.2 CGlib动态代理 1.2.1 CGLib特点说明 1.3 动态代理的作用 2 Spring...

Global site tag (gtag.js) - Google Analytics