参考博客:http://www.cnblogs.com/hongwz/p/5764917.html
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>SpringAop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringAop</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
</dependencies>
</project>
项目结构:
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.7.xsd">
<bean id="aopImpl1" class="com.SpringAop.AopImpl1"/>
<bean id="aopImpl2" class="com.SpringAop.AopImpl2"/>
<bean id="timeHandler" class="com.SpringAop.TimeHandler"/>
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<aop:pointcut id="addallmethod" expression="execution(* com.SpringAop.HelloAop.*(..))"/>
<aop:before method="currentTimeAop" pointcut-ref="addallmethod"/>
<aop:after method="currentTimeAop" pointcut-ref="addallmethod"/>
</aop:aspect>
</aop:config>
</beans>
<aop:pointcut id="addallmethod" expression="execution(* com.SpringAop.HelloAop.*(..))"/>
表示实现了该接口的所有实现类方法都会是横切面
实例:一个借口两个实现类一个测试类一个Aop实现类
package com.SpringAop;
public interface HelloAop {
public void print();
public void doprint();
}
package com.SpringAop;
public class AopImpl1 implements HelloAop{
public void print() {
// TODO Auto-generated method stub
System.out.println("1 print................");
}
public void doprint() {
// TODO Auto-generated method stub
System.out.println("1 doprint..............");
}
}
package com.SpringAop;
public class AopImpl2 implements HelloAop{
public void print() {
// TODO Auto-generated method stub
System.out.println("2 print................");
}
public void doprint() {
// TODO Auto-generated method stub
System.out.println("2 doprint..............");
}
}
package com.SpringAop;
public class TimeHandler {
public void currentTimeAop(){
System.out.println(System.currentTimeMillis());
}
}
package com.SpringAop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloAop {
private ApplicationContext ac;
@Test
public void tesHelloAop(){
ac = new ClassPathXmlApplicationContext("springaop.xml");
//AomImpl1 aopImpl=(AopImpl) aca.getBean("aopImpl1"); error
HelloAop aopImpl1 =(HelloAop) ac.getBean("aopImpl1");
aopImpl1.doprint();
System.out.println("..............");
aopImpl1.print();
}
}
结果:
Oct 06, 2017 10:57:59 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6996db8: startup date [Fri Oct 06 10:57:59 CST 2017]; root of context hierarchy
Oct 06, 2017 10:57:59 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [springaop.xml]
1507258680128
1 doprint..............
1507258680128
..............
1507258680128
1 print................
1507258680128
使用AomImpl1 aopImpl=(AopImpl) aca.getBean("aopImpl1");
报错
java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to***
解决方案 :参考博客:
http://blog.csdn.net/kuaisuzhuceh/article/details/44756333
对于Spring AOP 采用两种代理方法,一种是常规JDK,一种是CGLIB,当代理对象实现了至少一个接口时,默认使用JDK动态创建代理对象,当代理对象没有实现任何接口时,就会使用CGLIB方法