第一步:编写目标(target)
public class OrderServiceImpl implements OrderService {
@Override
public void add() {
// TODO Auto-generated method stub
System.out.println("add...");
}
@Override
public void update() {
// TODO Auto-generated method stub
System.out.println("update...");
}
}
第二步增强(advice)
在传统的spring aop开发中它支持增强(advice)有五种:
1.前置通知 目标方法执行前增强 org.springframework.aop.MethodBeforeAdvice
2.后置通知 目标方法执行后增强 org.springframework.aop.AfterReturningAdvice
3.环绕通知 目标方法执行前后进行增强 org.aopalliance.intercept.MethodInterceptor
4.异常抛出通知 目标方法抛出异常后的增强 org.springframework.aop.ThrowsAdvice
5.引介通知 在目标类中添加一些新的方法或属性
org.springframework.aop.IntroductionInterceptor
public class OrderHelp implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor {
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("前置通知...");
}
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("后置通知");
}
@Override
public Object invoke(MethodInvocation method) throws Throwable {
// TODO Auto-generated method stub
System.out.println("环绕前...");
Object value = method.proceed();
System.out.println("环绕后...");
return value;
}
}
第三步在applicationContext.xml文件中配置
<!-- 目标 -->
<bean id="orderService" class="brooker.aop.OrderServiceImpl"></bean>
<!-- 通知 -->
<bean id="orderServiceAdvice" class="brooker.aop.OrderHelp"></bean>
<!-- 定义切点 -->
<bean id="orderServicePointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedNames">
<list>
<value>add</value>
<value>update</value>
</list>
</property>
</bean>
<!-- 切面 -->
<bean id="orderServiceAspect" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<!-- 通知 -->
<property name="advice" ref="orderServiceAdvice"/>
<!-- 切点 -->
<property name="pointcut" ref="orderServicePointCut"/>
</bean>
<!-- 代理 -->
<bean id="orderServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="orderService"></property>
<property name="interceptorNames" value="orderServiceAspect"></property>
<property name="proxyInterfaces" value="brooker.aop.OrderService"></property>
</bean>
第四 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AopTest {
@Autowired
@Qualifier("orderServiceProxy")
private OrderService orderService;
@Test
public void test1() {
orderService.add();
}
}
结果:
环绕前...
前置通知...
add...
后置通知
环绕后...
二、基于aspectJ切点传统开发
<!-- 目标 -->
<bean id="orderService" class="brooker.aop.OrderServiceImpl"></bean>
<!-- 通知 -->
<bean id="orderServiceAdvice" class="brooker.aop.OrderHelp"></bean>
<!-- 使用aop标签完成切面与切点的声明 -->
<aop:config>
<!-- 切点 -->
<aop:pointcut expression="execution(* brooker.aop.OrderService.*(..))" id="orderServicePointCut"/>
<!-- 切面 (通知+切点)-->
<aop:advisor advice-ref="orderServiceAdvice" pointcut-ref="orderServicePointCut"/>
</aop:config>