参照:https://www.cnblogs.com/kuotian/p/8863257.html
示例代码:http://download.csdn.net/detail/u010476739/9922458
工具:spring framework 4.2.4、maven
使用xml配置实现spring-aop
1.pom依赖
<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.jackletter</groupId>
<artifactId>springaopxml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>this is name</name>
<description>this is desc</description>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Aop -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-aop/4.3.4.RELEASE -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</project>
2.spring配置文件(applicationContext.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="person" class="bean.Person" />
<bean id="dog" class="bean.Dog" />
<bean id="cat" class="bean.Cat" />
<aop:config>
<aop:aspect ref="person">
<aop:pointcut expression="execution(* bean.Animate.show())" id="show"/>
<!-- <aop:before method="showBefore" pointcut="execution(* bean.Animate.show())"/> -->
<aop:before method="showBefore" pointcut-ref="show"/>
<aop:after-returning method="showAfter" pointcut-ref="show"/>
<aop:after-throwing method="showThrow" pointcut-ref="show"/>
<aop:around method="showArround" pointcut-ref="show"/>
</aop:aspect>
</aop:config>
</beans>
3.代码
Animate.java
package bean;
public interface Animate {
public String show() throws Exception;
}
Cat.java
package bean;
public class Cat implements Animate {
public String show() {
return "i'm cat";
}
}
Dog.java
package bean;
public class Dog implements Animate{
public String show() throws Exception {
// throw new Exception("ko");
return "i'm dog";
}
}
Person.java
package bean;
import org.aspectj.lang.ProceedingJoinPoint;
public class Person {
public void showBefore(){
System.out.println("before run...");
}
public void showAfter(){
System.out.println("after run...");
}
public Object showArround(ProceedingJoinPoint point) throws Throwable{
System.out.println("arround start...");
Object obj=point.proceed();
System.out.println("arround end...");
return obj;
}
public void showThrow(){
System.out.println("throw run...");
}
}
App.java
package springaopxml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Animate;
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Animate ani=(Animate) context.getBean("dog");
System.out.print(ani.show());
}
}
4.测试调用
输出: