1、正常情况下, 基于注解的声明要优先于基于 XML 的声明.
2、通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的.
3、由于 AspectJ 得到越来越多的 AOP 框架支持, 所以基于注解配置得切面将会有更多重用的机会.
4、操作步骤:
①配置文件中配置bean
②配置文件中配置切面bean
③配置文件中配置AOP
④配置文件中配置切面表达式
⑤配置文件中配置切面
⑥创建spring的IOC容器
⑦从IOC容器中获取bean实例
⑧使用bean
5、文件
①TestMain.java:测试类
②AOPoperations.java:方法接口
③AOPopeartionsImpl.java:实现接口和方法的类
④LoggingAspect.java:切面类
⑤LoggingAspect2.java:切面类
⑥PointCut.java:切点类
⑦xmlNotificationContext.xml:配置文件
6、TestMain.java
package com.demo.sshtest.aop2.xmlNotification;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
//创建spring的IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xmlNotificationContext.xml");
//从IOC容器中获取bean实例(这里获取的是接口类的实例,而不是实现接口的类的实例)
AOPoperations aoPopeartions = (AOPoperations)applicationContext.getBean("aOPoperations");
//使用bean
try {
String result3 = aoPopeartions.addCal("1","2");
} catch (Exception e) {
}
}
}
7、AOPoperations.java
package com.demo.sshtest.aop2.xmlNotification;
public interface AOPoperations {
public String addCal(String str1,String str2) throws Exception;
}
8、AOPopeartionsImpl.java
package com.demo.sshtest.aop2.xmlNotification;
import org.springframework.stereotype.Component;
@Component
public class AOPopeartionsImpl implements AOPoperations{
public String addCal(String str1, String str2) throws Exception{
String result = "";
result = "" + (Double.parseDouble(str1) + Double.parseDouble(str2));
return result;
}
}
9、LoggingAspect.java
package com.demo.sshtest.aop2.xmlNotification;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
//声明前置通知:在目标方法开始之前执行
public void beforeMethod(JoinPoint joinPoint){
String methodname = joinPoint.getSignature().getName();
List<Object> values = Arrays.asList(joinPoint.getArgs());
System.out.println("Begin run method "+methodname+" and the value is:"+values);
}
//声明后置通知:在目标方法执行之后执行,无论是否发生异常
public void afterMethod(JoinPoint joinPoint){
String methodname = joinPoint.getSignature().getName();
List<Object> values = Arrays.asList(joinPoint.getArgs());
System.out.println("End run method "+methodname+" and the value is:"+values);
}
//声明返回通知:在目标方法返回结果之后执行
public void afterReturnMethod(JoinPoint joinPoint,Object result){
String methodname = joinPoint.getSignature().getName();
System.out.println("run method "+methodname+" and Return:"+result);
}
//声明异常返通知:在目标方法抛出异常之后执行
public void afterThrowingMethod(JoinPoint joinPoint,Exception exception) throws Exception{
String methodname = joinPoint.getSignature().getName();
System.out.println(methodname+" throw an exception:"+exception.getMessage());
}
//声明环绕通知:在前置通知执行前后运行,后置通知前执行,在异常通知执行后,则不执行
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
Object result = null;
String methodname = proceedingJoinPoint.getSignature().getName();
System.out.println(methodname+" aroundMethod...before note....");
result = proceedingJoinPoint.proceed();
System.out.println(methodname+" aroundMethod...after note...."+result);
return result;
}
}
10、LoggingAspect2
package com.demo.sshtest.aop2.xmlNotification;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
public class LoggingAspect2 {
//声明前置通知:在目标方法开始之前执行
public void beforeMethod2(JoinPoint joinPoint){
String methodname = joinPoint.getSignature().getName();
List<Object> values = Arrays.asList(joinPoint.getArgs());
System.out.println("LoggingAspect2-->Begin run method "+methodname+" and the value is:"+values);
}
//声明后置通知:在目标方法执行之后执行,无论是否发生异常
public void afterMethod2(JoinPoint joinPoint){
String methodname = joinPoint.getSignature().getName();
List<Object> values = Arrays.asList(joinPoint.getArgs());
System.out.println("LoggingAspect2-->End run method "+methodname+" and the value is:"+values);
}
}
11、PointCut.java
package com.demo.sshtest.aop2.xmlNotification;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
public class PointCut {
@Pointcut("execution(* com.demo.sshtest.aop2.xmlNotification.AOPoperations.*(..))")
public void pointcutexpression(){}
}
12、xmlNotificationContext.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: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-4.3.xsd">
<!-- 配置bean -->
<bean id="aOPoperations" class="com.demo.sshtest.aop2.xmlNotification.AOPopeartionsImpl"></bean>
<!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.demo.sshtest.aop2.xmlNotification.LoggingAspect"></bean>
<bean id="loggingAspect2" class="com.demo.sshtest.aop2.xmlNotification.LoggingAspect2"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut id="pointcut" expression="execution(* com.demo.sshtest.aop2.xmlNotification.AOPoperations.*(..))"/>
<!-- 配置切面 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="exception"/>
<aop:after-returning method="afterReturnMethod" pointcut-ref="pointcut" returning="result"/>
<aop:around method="aroundMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref="loggingAspect2" order="1">
<aop:before method="beforeMethod2" pointcut-ref="pointcut"/>
<aop:after method="afterMethod2" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
13、项目目录
14、demo
https://download.csdn.net/download/qq_22778717/10476407