基于代理的AOP开发与基于aspectJ切点传统开发

第一步:编写目标(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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值