基于注解的AOP配置
1.創(chuàng)建工程
1.1.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>com.wantong</groupId>
<artifactId>Spring_AOP_Annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Spring常用依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>
</project>
1.2.dao
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void addUser(){
System.out.println("insert into tb_user......");
}
}
1.3.service
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void addUser() {
userDao.addUser();
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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="com.wantong"></context:component-scan>
</beans>
1.5.測試
/**
* 模擬表現(xiàn)層
*/
public class Client {
public static void main(String[] args) {
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
//使用對象
UserService userService = ac.getBean("userServiceImpl",UserService.class);
userService.addUser();
}
}
2.增強
2.1.applicationContext.xml
<!-- 開啟spring對注解AOP的支持 -->
<aop:aspectj-autoproxy/>
2.2.AOP配置
-
常用注解
- @Aspect:把當前類聲明為切面類
- @Before:前置通知,可以指定切入點表達式
- @AfterReturning:后置【try】通知,可以指定切入點表達式
- @AfterThrowing:異?!綾atch】通知,可以指定切入點表達式
- @After:最終【finally】通知,可以指定切入點表達式
- @Around:環(huán)繞通知,可以指定切入點表達式
-
注解方式實現(xiàn)aop文章來源:http://www.zghlxwxcb.cn/news/detail-815263.html
@Component
@Aspect
public class MyLogAdvice {
//前置通知
@Before("execution(* com.wantong.service.*.*(..))")
public void before(){
System.out.println("前置通知");
}
//后置通知【try】
@AfterReturning("execution(* com.wantong.service.*.*(..))")
public void afterReturning(){
System.out.println("后置通知");
}
//異常通知【catch】
@AfterThrowing("execution(* com.wantong.service.*.*(..))")
public void afterThrowing(){
System.out.println("異常通知");
}
//最終通知【finally】
@After("execution(* com.wantong.service.*.*(..))")
public void after(){
System.out.println("最終通知");
}
//環(huán)繞通知
@Around("execution(* com.wantong.service.*.*(..))")
public void around(ProceedingJoinPoint joinPoint){
try {
System.out.println("方法執(zhí)行前的環(huán)繞通知");
joinPoint.proceed();
System.out.println("方法執(zhí)行后的環(huán)繞通知");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-815263.html
到了這里,關于Spring——基于注解的AOP配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!