AOP常用注解
注解 名称 @Before 前置通知 @After 后置通知 @AfterThrowing 异常通知 @AfterReturning 最终通知 @Around 环绕通知
注解开发实现步骤
1. 导入Maven项目依赖
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.0.2.RELEASE</ version>
</ dependency>
< dependency>
< groupId> commons-logging</ groupId>
< artifactId> commons-logging</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.12</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.10</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.6</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 5.0.2.RELEASE</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> aopalliance</ groupId>
< artifactId> aopalliance</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 5.0.2.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.8.3</ version>
</ dependency>
</ dependencies>
2. 准备一个实体类(先定义接口再实现)
package com. qcby. service ;
public interface UserService {
public void save ( ) ;
}
package com. qcby. service. impl ;
import com. qcby. service. UserService ;
import org. springframework. stereotype. Service ;
@Service
public class UserServiceImpl implements UserService {
public void save ( ) {
System . out. println ( "业务层:Hello World!" ) ;
}
}
3. 定义切面类
package com. qcby. util ;
import org. aspectj. lang. ProceedingJoinPoint ;
import org. aspectj. lang. annotation. * ;
import org. springframework. stereotype. Component ;
@Aspect
@Component
public class MyAspect {
@Before ( "execution(* com.qcby.*.*.*ServiceImpl.save*(..))" )
public void log1 ( ) {
System . out. println ( "我是前置增强方法!" ) ;
}
@After ( "execution(* com.qcby.*.*.*ServiceImpl.save*(..))" )
public void log2 ( ) {
System . out. println ( "我是后置增强方法!" ) ;
}
@AfterThrowing ( "execution(* com.qcby.*.*.*ServiceImpl.save*(..))" )
public void log3 ( ) {
System . out. println ( "我是异常增强方法!" ) ;
}
@AfterReturning ( "execution(* com.qcby.*.*.*ServiceImpl.save*(..))" )
public void log4 ( ) {
System . out. println ( "我是最终增强方法!" ) ;
}
@Around ( "execution(* com.qcby.*.*.*ServiceImpl.save*(..))" )
public void aroundLog ( ProceedingJoinPoint point) {
try {
log1 ( ) ;
point. proceed ( ) ;
log4 ( ) ;
} catch ( Throwable e) {
log3 ( ) ;
e. printStackTrace ( ) ;
} finally {
log2 ( ) ;
}
}
}
4. 准备配置文件
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" >
< context: component-scan base-package = " com.qcby" />
< aop: aspectj-autoproxy/>
</ beans>
5. 编写测试类
import com. qcby. service. UserService ;
import com. qcby. util. SpringConfig ;
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 ;
@RunWith ( SpringJUnit4ClassRunner . class )
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void run ( ) {
userService. save ( ) ;
}
}
注意事项
使用注解式开发还需要在spring
的配置文件中开启自动代理:<aop:aspectj-autoproxy/>
AOP纯注解式开发需要在配置类中多加一个注解@EnableAspectJAutoProxy
开启自动代理