Spring中AOP切面的定义方式

Spring切面的定义有四种:

   一,基于Annotation也就是@AspectJ的注解方式

   二,基于<aop:aspect>的方式

   三,基于<aop:advisor>的方式

   四,基于Advisor类的方式

 

注:第四种一般不用,其他的看情况用

 

先看第一种基于Annotation的:

beans.xml(此处要加aop和context的命名空间的名字和location,然后加那三个配置,第一个是基于annotation,第二个是自动检测组件,第三个就是aspectj的支持)

<?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: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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.lbx"/>
 	<aop:aspectj-autoproxy />
 	
</beans>

 

AOP就类似拦截器,使用AOP就可以轻松的处理很多的东西,例如:事务的处理,权限的处理,日记的处理,统计等

下面就是一个Advice(增强)(其中@Aspect表示是一个切面,@Pointcut是一个切点,相当于一个查询条件,@Before是一个前置增强,@After是一个后置增强)

package com.lbx.aop;

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

@Aspect
@Component
public class LogInterceptor {
	
	@Pointcut("execution(public * com.lbx.dao..*.*(..))")
	public void getMethod(){}
	
	@Before("getMethod()")
	public void before(){
		System.out.println("LogInterceptor.before()");
	}
	
	//@After("getMethod()")
	//@AfterReturning("getMethod()")
	@AfterReturning("getMethod()")
	public void afterReturning(){
		System.out.println("LogInterceptor.afterReturning()");
	}
	
}	

 

测试方法没写,只要是满足那个@Pointcut条件的都可以进行增强

 

 

 

第二种基于<aop:aspect>的方式

bean.xml文件配置(把要植入的那个Advice加一个bean,然后使用<aop:aspect>来配置)

<?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: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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.lbx"/>

	<bean id="logInterceptor" class="com.lbx.aop.LogInterceptor"></bean>
	<aop:config>
		<aop:pointcut expression="execution(public * com.lbx.dao..*.*(..))" id="servicePointCut"/>
		<aop:aspect id="logAspect" ref="logInterceptor">
			<aop:before method="before" pointcut-ref="servicePointCut"/>
			<aop:after method="afterReturning" pointcut-ref="servicePointCut"/>
		</aop:aspect>
	</aop:config>
 	
</beans>

 Advice代码

package com.lbx.aop;


public class LogInterceptor {
	
	public void getMethod(){}
	
	public void before(){
		System.out.println("LogInterceptor.before()");
	}
	
	public void afterReturning(){
		System.out.println("LogInterceptor.afterReturning()");
	}
	
}	

 

测试没写(效果和上面的一样)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值