1,xml实现aop代码演示
1.1,导入aop需要的maven
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
1.2,新建 UserService
package com.study.service;
public interface UserService {
void show1();
void show2();
}
1.2,新建 UserServiceImpl
package com.study.service.impl;
import com.study.service.UserService;
public class UserServiceImpl implements UserService {
public void show1() {
System.out.println("UserServiceImpl -- show1");
}
public void show2() {
System.out.println("UserServiceImpl -- show2");
}
}
1.3,新建增强类 MyAdvice
package com.study.advice;
//增强类 内部提供增强方法
public class MyAdvice {
public void beforeAdvice(){
System.out.println("前置增强");
}
public void afterAdvice(){
System.out.println("后置增强");
}
}
1.4,新建 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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 目标类-->
<bean id="userService" class="com.study.service.impl.UserServiceImpl"></bean>
<!-- 配置通知类-->
<bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>
<!-- aop配置-->
<aop:config>
<!-- 配置切点表达式,目的是指定那些方法被增强-->
<aop:pointcut id="myPointcut" expression="execution(void com.study.service.impl.UserServiceImpl.show1())"/>
<!-- 配置织入:配置那些切点和那些通知结合-->
<aop:aspect ref="myAdvice">
<aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
1.5,新建测试类 AspectDemo
package com.study.demo;
import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectDemo {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)context.getBean("userService");
userService.show1();
}
}
2 xml配置aop配置详解
2.1 修改配置文件
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 目标类-->
<bean id="userService" class="com.study.service.impl.UserServiceImpl"></bean>
<!-- 配置通知类-->
<bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>
<!-- aop配置-->
<aop:config>
<!-- 配置切点表达式,目的是指定那些方法被增强-->
<aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
<!-- 配置织入:配置那些切点和那些通知结合-->
<aop:aspect ref="myAdvice">
<aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
3 aop的五种通知类型
3.1 后置通知
3.1.1 修改配置文件
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 目标类-->
<bean id="userService" class="com.study.service.impl.UserServiceImpl"></bean>
<!-- 配置通知类-->
<bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>
<!-- aop配置-->
<aop:config>
<!-- 配置切点表达式,目的是指定那些方法被增强-->
<aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
<!-- 配置织入:配置那些切点和那些通知结合-->
<aop:aspect ref="myAdvice">
<!--前置通知-->
<aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
<!--后置通知-->
<aop:after-returning method="afterAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
3.2 环绕通知
3.2.1 修改MyAdvice
package com.study.advice;
import org.aspectj.lang.ProceedingJoinPoint;
//增强类 内部提供增强方法
public class MyAdvice {
public void beforeAdvice(){
System.out.println("前置增强");
}
public void afterAdvice(){
System.out.println("后置增强");
}
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强");
//环绕目标方法
//执行目标方法
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕后增强");
return proceed;
}
}
3.2.2 修改配置文件
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 目标类-->
<bean id="userService" class="com.study.service.impl.UserServiceImpl"></bean>
<!-- 配置通知类-->
<bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>
<!-- aop配置-->
<aop:config>
<!-- 配置切点表达式,目的是指定那些方法被增强-->
<aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
<!-- 配置织入:配置那些切点和那些通知结合-->
<aop:aspect ref="myAdvice">
<!--前置通知-->
<aop:around method="aroundAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
3.3 异常抛出通知
3.3.1 修改 MyAdvice
package com.study.advice;
import org.aspectj.lang.ProceedingJoinPoint;
//增强类 内部提供增强方法
public class MyAdvice {
public void beforeAdvice(){
System.out.println("前置增强");
}
public void afterAdvice(){
System.out.println("后置增强");
}
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强");
//环绕目标方法
//执行目标方法
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕后增强");
return proceed;
}
public void afterThrowingAdvice(){
System.out.println("异常抛出通知 报异常时执行");
}
}
3.3.2 修改 UserServiceImpl
package com.study.service.impl;
import com.study.service.UserService;
public class UserServiceImpl implements UserService {
public void show1() {
int i = 1/0;
System.out.println("UserServiceImpl -- show1");
}
public void show2() {
System.out.println("UserServiceImpl -- show2");
}
}
3.3.3 修改配置文件
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 目标类-->
<bean id="userService" class="com.study.service.impl.UserServiceImpl"></bean>
<!-- 配置通知类-->
<bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>
<!-- aop配置-->
<aop:config>
<!-- 配置切点表达式,目的是指定那些方法被增强-->
<aop:pointcut id="myPointcut" expression="execution(void com.study.service.impl.UserServiceImpl.show1())"/>
<!-- 配置织入:配置那些切点和那些通知结合-->
<aop:aspect ref="myAdvice">
<aop:around method="aroundAdvice" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowingAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>