大家好,又见面了,我是你们的朋友全栈君。
spring aop通知(advice)分成五类:
- 前置通知[Before advice]@Before:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
- 后置通知、正常返回通知[After returning advice]@AfterReturning:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行;与环绕通知同时使用时,会受环绕通知的影响。
- 异常通知、异常返回通知[After throwing advice]@AfterThrowing:在连接点抛出异常后执行。
- 最终通知、返回通知[After (finally) advice]@After:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
- 环绕通知[Around advice]@Around:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。环绕通知还需要负责决定是继续处理join point(调用ProceedingJoinPoint的proceed方法)还是中断执行。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>dome03</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
</project>
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.xbj"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="myAdvices">
<aop:pointcut id="pc" expression="execution(* com.xbj.service.impl.*.*(..))"/>
<aop:before method="before" pointcut-ref="pc" />
<aop:after-returning method="afterRunturn" pointcut-ref="pc" />
<aop:after method="afterfinally" pointcut-ref="pc" />
<aop:around method="around" pointcut-ref="pc" />
<!--ex 要和 切面的 参数签名一致-->
<aop:after-throwing method="afterThorw" pointcut-ref="pc" throwing="ex" />
</aop:aspect>
</aop:config>
</beans>
UserController.java
package com.xbj.controller;
import com.xbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
/* @Controller @Repository @Component @Service 都是将bean放入Ioc容器中,只是为了代码的可读性而去区别开 * @Autowired 是从Ioc容器拿对象 * */
@Controller
public class UserController {
@Autowired
private UserService userService;
public void addUser(){
userService.addUser();
}
}
UserDao.java
package com.xbj.dao;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
public interface UserDao {
void addUser();
void delUser();
}
UserDaoImpl.java
package com.xbj.dao.impl;
import com.xbj.dao.UserDao;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@Repository
public class UserDaoImpl implements UserDao {
public void addUser() {
System.out.println("addUser");
}
public void delUser() {
System.out.println("delUser");
}
//定义初始化方法
public void myInit(){
System.out.println("init...");
}
//定义销毁方法
public void dest(){
System.out.println("dest...");
}
}
UserService.java
package com.xbj.service;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
public interface UserService {
void addUser();
void delUser();
}
UserServiceImpl.java
package com.xbj.service.impl;
import com.xbj.dao.UserDao;
import com.xbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void addUser() {
userDao.addUser();
}
public void delUser() {
userDao.delUser();
}
}
MyAdvices.java
package com.xbj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
/* * 注意类的包名 * */
@Component
public class MyAdvices {
//前置通知
public void before(JoinPoint joinpoint){
System.out.println("前置通知");
}
//正常放回通知
public void afterRunturn(JoinPoint joinpoint){
System.out.println("afterRunturn");
}
//最终返回通知
public void afterfinally(JoinPoint joinpoint){
System.out.println("afterfinally");
}
//环绕通知
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("around-before");
try {
proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("around-after");
}
//异常通知
public void afterThorw(Throwable ex){
System.out.println("afterThorw"+ex);
}
}
test.java
import com.xbj.controller.UserController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:applicationContext.xml"})
public class test {
@Autowired
private UserController controller;
@Test
public void t1() {
controller.addUser();
}
}
控制台输出:
前置通知
around-before
addUser
around-after
afterfinally
afterRunturn
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106636.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...