spring AOP

在开始之前需要导入spring里面没有的二个包  

aopalliance.jar    aspectjweaver-1.5.0.jar   没有这二个包不能使用  


接口 


package com.my.aop;

public interface IPerson {
	
	public void add();
	
	public void delete();
	
	public void  update(int i);
}


目标对象  
package com.my.aop;

public class PersonImpl  implements IPerson{

	@Override
	public void add() {
		
		System.out.println("添加");
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void delete() {
	
		System.out.println("删除");
		
		
	}

	@Override
	public void update(int i) {
		
		System.out.println("修改了");
		
	}

}

aop  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:context="http://www.springframework.org/schema/context"
        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.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
           
<bean id="IPerson" class="com.my.aop.PersonImpl"></bean>
<bean id="logingAdvice" class="com.my.aop.advice.LogingAdvice"></bean>

<!-- 配置一个aop  更多细节请查看API -->

<aop:config>
   <aop:pointcut expression="execution(* com.my.aop.PersonImpl.add(..))" id="pointcut"/>
   <aop:pointcut expression="execution(* com.my.aop.PersonImpl.update(..))" id="pointcut1"/>
   <aop:advisor advice-ref="logingAdvice" pointcut-ref="pointcut"/>
   <aop:advisor advice-ref="logingAdvice" pointcut-ref="pointcut1"></aop:advisor>
</aop:config>



</beans>           


AOP核心  

package com.my.aop.advice;

import java.lang.reflect.Method;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class LogingAdvice  implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor  {

	@Override
	public void before(Method method, Object[] arg1, Object target)
			throws Throwable {
		
		System.out.println( method.getName()+"方式被调用   目标对象"+target.getClass().getName());
		
	}

	@Override
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
		
		System.out.println( method.getName()+"方式被执行后   目标对象"+target.getClass().getName());
	}

	@Override
	public Object invoke(MethodInvocation method) throws Throwable {
		
		System.out.println("环绕前====》》》》");
	
		long start = System.currentTimeMillis();
	
		
		Object obj = method.proceed();
		
		
		
		System.out.println("环绕后====》》》》");
		
		long end = System.currentTimeMillis();
		
		System.out.println("使用时间为:"+(end-start));
		
		return obj;
	}

}

测试  


package com.my.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.aop.IPerson;

public class Test {
	
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		
		IPerson ip =  (IPerson) ac.getBean("IPerson");
		
		ip.add();
		
		
	}

}
结果 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值