示例代码:http://pan.baidu.com/s/1hspE8Yg
 
参照:https://www.cnblogs.com/kuotian/p/8863257.html
 
工具: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: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/aop
        				http://www.springframework.org/schema/aop/spring-aop.xsd
        				http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="bean" />
	<aop:aspectj-autoproxy />
</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;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class Person {
	@Pointcut("execution(* bean.Animate.show())")
	public void pointCut(){
		
	}
	
	@Before("pointCut()")
	public void showBefore(){
		System.out.println("before run...");
	}
	
	@AfterReturning("pointCut()")
	public void showAfter(){
		System.out.println("after run...");
	}
	
	@Around("pointCut()")
	public Object showArround(ProceedingJoinPoint point) throws Throwable{
		System.out.println("arround start...");
		Object obj=point.proceed();
		System.out.println("arround end...");
		return obj;
	}
	
	@AfterThrowing("pointCut()")
	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.测试调用
 
 
输出: