工程目录:
导入jar包:
最基本的jar包
logging
beans
context
core
expression
aopalliance
aspectj.weaver
aop
aspects
定义一个接口,声明四个方法:
AtithmeticCalculator.java
package aop.impl;
import org.omg.PortableInterceptor.IORInterceptor;
public interface AtithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
实现这个接口:
AtithmeticCalculatorImpl.java
package aop.impl;
import org.springframework.stereotype.Component;
@Component
public class AtithmeticCalculatorImpl implements AtithmeticCalculator {
@Override
public int add(int i, int j) {
int result=i+j;
return result;
}
@Override
public int sub(int i, int j) {
int result=i-j;
return result;
}
@Override
public int mul(int i, int j) {
int result=i*j;
return result;
}
@Override
public int div(int i, int j) {
int result=i/j;
return result;
}
}
前置、后置通知:
LoggingAspect.java
package aop.impl;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//把这个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//*代表作用于里面的所有方法
@Before("execution(public int aop.impl.AtithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinpoint){
String methodName=joinpoint.getSignature().getName();
List<Object> args=Arrays.asList(joinpoint.getArgs());
System.out.println("The method "+methodName+" begins with"+args);
}
//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中不能访问目标方法的执行结果
@After("execution(public int aop.impl.AtithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinpoint){
String methodName=joinpoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
}
}
配置文件:
applicationContext.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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 配置自动扫描包 -->
<context:component-scan base-package="aop.impl"></context:component-scan>
<!-- 使aspectJ注解起作用 :自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试函数:
Main.java
package aop.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建springIOC容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取bean
AtithmeticCalculator atithmeticCalculator=ctx.getBean(AtithmeticCalculator.class);
//3.使用bean
int result=atithmeticCalculator.add(1, 5);
System.out.println(result);
result=atithmeticCalculator.sub(1, 5);
System.out.println(result);
result=atithmeticCalculator.mul(1, 5);
System.out.println(result);
result=atithmeticCalculator.div(1, 5);
System.out.println(result);
}
}
1.springAOP总结:
(1).加入jar包
最基本的jar包
logging
beans
context
core
expression
aopalliance
aspectj.weaver
aop
aspects
(2).在配置文件中加入aop命名空间
(3).基于注解的方式
配置文件如下:
a. 使aspectJ注解起作用 :自动为匹配的类生成代理对象
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
b.把横切关注点的代码抽象到切面的类中
c.切面首先是一个IOC中的bean,即加入@component注解
d.切面还需要加入@Aspect注解
e.在类中声明各种通知(方法):
声明一个方法,在方法前加入@Before注解
@Before("execution(public int aop.impl.AtithmeticCalculator.*(int, int))")
改写:
@Before("execution(* aop.impl.AtithmeticCalculator.*(int, int))")
(4).可以在通知方法中声明一个类型为JoinPoint的参数,然后就能访问连接细节,如方法名称和参数
@Before("execution(public int aop.impl.AtithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinpoint){
String methodName=joinpoint.getSignature().getName();
List<Object> args=Arrays.asList(joinpoint.getArgs());
System.out.println("The method "+methodName+" begins with"+args);
}
@After("execution(public int aop.impl.AtithmeticCalculator.*(int, int))")
public void afterMethod(JoinPoint joinpoint){
String methodName=joinpoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
}