spring2.5.6-aop

 

1 包说明

//spring运行需要导入的包

spring.jar

commons-logging.jar

//依赖注入需要导入的包

  cglib.jar

  asm.jar

  //aop 需要导入的包

aspectjweaver.jar 

cglib.jar 

aop配置使用依赖注入的方式 不使用xml的方式

 

2 编写aop基本类 man

package com.aop.spring;
	
	import org.springframework.stereotype.Component;
	
	@Component("man")
	public class Man {
		
		String name = "";
		
		public Man(){};
		
		public Man(String name)
		{
			this.name = name;
		}
		
		public void add()
		{
			System.out.println("add");
		}
		
		public void add(String name)
		{
			System.out.println("add");
		}
		
		public String getName()
		{
			System.out.println("getName");
			return name;
		}
		
		public void update()
		{
			System.out.println("update");
		}
		
		public void delete()
		{
			System.out.println("delete");
		}
	}
 

 3 编写切面类,即监听类和方法执行的类

 

 

package com.aop.spring;
	
	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;
	
	@Component("manInterceptor")
	@Aspect
	public class ManInterceptor {
		
		/**
		 * 该方法意思就是监听com.aop.spring下所有类的所有方法,以下为具体的说明
		 * 定义切入点
		 * 第一个*表示方法的返回值,这里使用通配符,只有返回值符合条件的才拦截,(!void表示有返回值)
		 * 第一个..表示com.aop.spring包及其子包
		 * 第二个*表示包下所有的java类都被拦截
		 * 第三个*表示类的所有方法都被拦截
		 * (..)表方法的参数可以任意多个,
		 * 如[String ,Integer]表示第一个参数是String第二个参数是Integer的方法才被拦截
		 */
		@Pointcut("execution(* com.aop.spring.*.*(..))")
		private void pointCutMethod()
		{
			
		}
		
		/**
		 * 定义前置通知 并设置了方法必须包含一个字符参数
		 */
		@Before("pointCutMethod() && args(userName)")
		public void doBefore(String userName)
		{
			System.out.println("前置通知"+userName); 
		}
		
		/**
		 * 定义后置通知,并设置了只有string返回值的方法才会拦截
		 */
		@AfterReturning(pointcut="pointCutMethod()",returning="result")
		public void doAfterReturning(String result)
		{
			System.out.println("后置通知"+result);
		}
		
		/**
		 * 定义例外通知
		 */
		@AfterThrowing("pointCutMethod()")
		public void doAfterException()
		{
			System.out.println("异常通知"); 
		}
		
		/**
		 * 定义最终通知
		 */
		@After("pointCutMethod()")
		public void doAfter()
		{
			System.out.println("最终通知"); 
		}
		
		
		/**
		 * 定义环绕通知
		 */
		@Around("pointCutMethod()")
		public Object doAround(ProceedingJoinPoint pjp) throws Throwable
		{
			System.out.println("进入方法");
			Object o = pjp.proceed();
			System.out.println("退出方法");
			return o;
		}
	}

 4 编写测试类AopTest

 

 package com.aop.spring;
	
	import org.springframework.context.ApplicationContext;
	import org.springframework.context.support.ClassPathXmlApplicationContext;
	
	public class AopTest {
	
		/**
		 * @param args
		 */
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
			Man man = (Man)ctx.getBean("man");
			man.add("sky");
		}
	
	}
 

 5 总结:测试类输出如下

进入方法

add

后置通知null

最终通知

退出方法

表示Man的add方法被拦截了,并执行了切面类中各种符合条件的监听方法。

可以通过修改切面类中的各种配置来满足不同的处理需求。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值