在开始之前需要导入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();
}
}
结果