一、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:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<bean id="aopExampleImpl1" class="com.terisadeng.spring.aop.AopExampleImpl1"></bean>
<bean id="aopExampleImpl2" class="com.terisadeng.spring.aop.AopExampleImpl2"></bean>
<bean id="timeLogger" class="com.terisadeng.spring.aop.TimeLogger"></bean>
<aop:config proxy-target-class="true">
<aop:aspect id="time" ref="timeLogger">
<aop:pointcut id="AllMethod" expression="execution(* com.terisadeng.spring.aop.AopExample.*(..))"></aop:pointcut>
<aop:before method="costTime" pointcut-ref="AllMethod"></aop:before>
<aop:after method="costTime" pointcut-ref="AllMethod"></aop:after>
</aop:aspect>
</aop:config>
</beans>
二、创建一个目标接口及两个实现类
package com.terisadeng.spring.aop;
public interface AopExample
{
void enterAopExample();
void outAopExample();
}
package com.terisadeng.spring.aop;
public class AopExampleImpl1 implements AopExample
{
//该方法作为切入点
@Override
public void enterAopExample()
{
System.out.println("Enter AopExampleImpl1.enterAopExample()");
}
//该方法作为切入点
@Override
public void outAopExample()
{
System.out.println("Enter AopExampleImpl1.outAopExample()");
}
}
package com.terisadeng.spring.aop;
public class AopExampleImpl2 implements AopExample
{
//该方法作为切入点
@Override
public void enterAopExample()
{
System.out.println("Enter AopExampleImpl2.enterAopExample()");
}
//该方法作为切入点
@Override
public void outAopExample()
{
System.out.println("Enter AopExampleImpl2.outAopExample()");
}
}
该类是拦截到方法后进行的处理
package com.terisadeng.spring.aop;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeLogger
{
public void costTime(){
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S");
System.out.println("CurrentTime = " +sdf.format(date));
}
}
三、测试方法:
1、使用Junit进行测试
package com.terisadeng.spring.aop;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:aop.xml")
public class TestAop
{
@Resource
private AopExampleImpl1 aopExampleImpl1;
@Resource
private AopExampleImpl2 aopExampleImpl2;
@Test
public void test(){
aopExampleImpl1.enterAopExample();
System.out.println();
aopExampleImpl1.outAopExample();
System.out.println();
aopExampleImpl2.enterAopExample();
System.out.println();
aopExampleImpl2.outAopExample();
}
}
2、使用慢main函数进行测试
package com.terisadeng.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopDemo
{
public static void main(String[] args)
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("aop.xml");
AopExampleImpl1 aopExampleImpl1=(AopExampleImpl1)ctx.getBean("aopExampleImpl1");
AopExampleImpl2 aopExampleImpl2=(AopExampleImpl2)ctx.getBean("aopExampleImpl2");
aopExampleImpl1.enterAopExample();
System.out.println();
aopExampleImpl1.outAopExample();
System.out.println();
aopExampleImpl2.enterAopExample();
System.out.println();
aopExampleImpl2.outAopExample();
}
}
一开始报错com.sun.proxy.$Proxy2 cannot be cast to com.terisadeng.spring.aop.AopExampleImpl1
表示动态代理生成的类无法转换为自定义的实现类
需要在aop:config标签中增加属性:proxy-target-class="true"