`
woaiyingyu123
  • 浏览: 69775 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

Spring AOP配置补充

阅读更多
感觉之前写的那Spring配置中的AOP太繁琐了。现在小小的整理一下。
(1)在xml中的配置
	<aop:config proxy-target-class="true">
		<aop:aspect id="logsAspect" ref="recordLog">
			<aop:pointcut expression="execution(* com.lrl.action.Login.userLogin(String)) and args(name)"
				id="recordPoint" />//带参数了!
			<aop:around method="recordLogs" pointcut-ref="recordPoint" />
		</aop:aspect>
	</aop:config>

(2)还可以在类中配置
//直接在方法上
	@Around("execution(* com.lrl.action.Register.userRegister(String)) && args(name)")
	public void recordLogs1(ProceedingJoinPoint jp, String name)
			throws Throwable {
		System.out.println("1234");
		jp.proceed();
		System.out.println(name);
		
	}

//或者可以选择在外面定义切点
	@Pointcut("execution(* com.lrl.action.Register.userRegister(String))")
	public void reg(){};//切点名,任意的
	@Around("reg() && args(name)")//里面包含了切点名和参数列表
	public void recordLogs(ProceedingJoinPoint jp, String name)
			throws Throwable {
               System.out.println(name);
		jp.proceed();	
	}

当然,一个通知可以有多个切点
@Pointcut("execution(* com.lrl.action.Register.userRegister(String))")
	public void reg(){};//切点1
@Pointcut("execution(* com.lrl.action.Register.userLogin(String))")
	public void login(){};//切点2
@Pointcut(" reg() || login()")//可以这样合并
        public void  combine(){};//合并切点

@Around("reg() || login()")//可以这样用!
//@Around("combine()")也可以这样用,效果是一样的!
public void recordLogs(ProceedingJoinPoint jp, String name)
		throws Throwable {
      System.out.println(name);
     jp.proceed();	
	}


//这里还说明几点:
(1)ProceedingJoinPoint是针对于环绕通知的。
(2)JoinPoint是前置、后置等通知的
(3)xml中参数列表用and args(xxx),类中可以用 && args(xxx)或and args(xxx).
(4)返回值等的应用可以参考Spring配置那篇文章。(虽然阅读困难,但是将就一下把。呵呵)
(5)有说的不对的地方,请留言。没做过很大量的实验测试。见谅~以后会补充完善的
(6)注意:最好不要某个类的所有方法都进行AOP切入,否则有时候切入的是getter等方法的时候,其他页面需要数据,会导致为空!具体原因可能是内部机制,现在还不是很了解。以后了解补充!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics