Spring学习文档_AOP

AOP: Aspect Oriented Programming -- 织入


工作原理为采用代理对象, 继承InvocationHandler, Struts2的Interceptor也是典型的AOP, 原理的sample:
package com.lohamce.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class LogInterceptor implements InvocationHandler{
	
	private Object target;

	public void beforeMethod(Method method){
		System.out.println("["+method.getName()+" begins]");
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		beforeMethod(method);
		method.invoke(target, args);
		return null;
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}
}

	@Test
	public void testProxy() throws Exception{
		UserDao userDao = new UserDaoImpl();
		
		LogInterceptor li = new LogInterceptor();
		li.setTarget(userDao);
		
		UserDao proxy = (UserDao)Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(), li);
		
		proxy.save(new User());
		proxy.remove(new User());
		
	}


在Spring中有2种方式配置AOP
1. Annotation, 使用AspectJ实现, AspectJ是AOP的框架, 实现需要加入aspectjweaver.jar和aspectjrt.jar
织入的语法比较麻烦, 使用的话,需要查文档
package com.lohamce.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component("logInterceptor")
public class LogInterceptor {
	
	private Object target;

	@Before("execution (public * com.lohamce.service.impl.*.*(..))")
	public void beforeMethod(){
		System.out.println("[Method begins]");
	}


	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}
}



2. XML,比annotation更有优势,因为可以插拔,有可能切面类的逻辑是第三方的提供的.
<aop:config>
	<aop:pointcut expression="execution (public * com.lohamce.service.impl.*.*(..))" id="servicePointcut"/>
	<aop:aspect id="logAspect" ref="logInterceptor">
		<aop:before method="beforeMethod" pointcut-ref="servicePointcut" />
	</aop:aspect>
</aop:config>


Spring初始化之后,当执行到com.lohamce.service.impl下面的类方法时,判断是否符合表达式 (servicePointcut), 如果符合, 在执行此方法前调用logIntercetor的beforeMethod方法
<aop: advisor> 声明式的事务管理才处理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值