使用Aspectj框架进行AOP切面开发。
首先需要导入两个Jar包:
1. aspectjrt.jar
2. aspectjweaver-XXX.jar
Aspectj支持的五种注解:
下面将使用两种方式进行开发。
形式一 : 使用注解形式结合Spring,
切面:AudienceAdvice类
@Aspect
public class AudienceAdvice {
/** * 1> execution() : 指明一个表达式 * 2> * *..UserService.*(..): 表达式说明 * (1) * : 修饰符 --> 例如:public 或public void * (2) *. : 任意包名或子包名 * (3) .UserService : 该切面只对UserService接口生效 * (4) .* : 所有的方法 * (5) (..) :方法可以有任意参数 * * 作用:任意包下的UserServiceImpl的任意方法,触发此AOP切面 * 3> 表达式可以只写一次,当其他注解继续引用此表达式时, * 直接引用写了表达式的方法名即可 * 例如: init() 方法 和 after() 方法的注解形式 * */
@Pointcut("execution(* *..UserService.*(..))")
public void init(){};
/** * 之后 */
@After("init()")
public void after(){
System.out.println("after");
}
/** * 之前 */
@Before("init()")
public void before(){
System.out.println("before");
}
/** * 环绕 */
@Around("init()")
public void around(ProceedingJoinPoint jp){
System.out.println("before");
try {
//执行方法
jp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("after");
}
}
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:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- service -->
<bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
</bean>
<!-- 必须要注入此切面,即使下面引用了自动注入 -->
<bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
<!-- aop自动代理,此标签必须引入 -->
<aop:aspectj-autoproxy />
</beans>
UserServiceImpl类
public class UserServiceImpl implements UserService{
@Override
public void save() {
}
}
Test部分
public class UserServiceTest {
@Test
public void saveTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
UserService userDao =
applicationContext.getBean("userServiceImpl", UserService.class);
userDao.save();
applicationContext.close();
}
}
形式二:使用XML配置Aspectj结合Spring
切面:AudienceAdvice
public class AudienceAdvice {
public void init(){};
/** * 之后 */
public void after(){
System.out.println("after");
}
/** * 之前 */
public void before(){
System.out.println("before");
}
/** * 环绕 */
public void around(ProceedingJoinPoint jp){
System.out.println("before");
try {
jp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("after");
}
}
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:util="http://www.springframework.org/schema/util" 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/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
</bean>
<!-- 注入切面 -->
<bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
<!-- XML配置切面 -->
<aop:config>
<!-- 切面 -->
<aop:aspect ref="advice">
<!-- 切入点 -->
<aop:pointcut expression="execution(* *..UserService.*(..))" id="mypoint"/>
<!-- method : 指定运行切面中(AudienceAdvice类)的哪个方法 pointcut-ref : 指定切入点,什么时候运行通知 -->
<!-- 前置通知 -->
<!-- <aop:before method="before" pointcut-ref="mypoint"/> -->
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="mypoint"/>
</aop:aspect>
</aop:config>
</beans>
UserServiceImpl服务层:
public class UserServiceImpl implements UserService{
public UserServiceImpl() {
System.out.println("初始化");
}
@Override
public void save() {
System.out.println("save");
}
}
测试部分:
public class UserServiceTest {
@Test
public void saveTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
UserService userDao =
applicationContext.getBean("userServiceImpl", UserService.class);
userDao.save();
applicationContext.close();
}
}