spring框架AOP配置

第一步:导入包
aopalliance-1.0.jar
aspectjweaver-1.8.13.jar
spring-aop-3.2.13.RELEASE.jar

spring开头的包自带的有
aopalliance,aspectjweaver去https://repo.spring.io/下载
还加上ioc的5个包:

commons-logging-1.2.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar

第二步:配置ApplicationContext.xml文件

<?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:aop="http://www.springframework.org/schema/aop"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
           	http://www.springframework.org/schema/beans/spring-beans.xsd
           	http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.2.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd">
			
	
	<bean id="usLogger" class="com.big3.aop.UserServiceLogger"/>
	<!-- 日志增强注入 -->
	<aop:config>
		<!--定义切入点-->
		<aop:pointcut id="pointcut" expression="execution(public void eat(com.big3.pojo.User))"/>
		<!--织入增强处理,ref:增强处理对象,注入的日志类-->
		<aop:aspect ref="usLogger">
			 <!--前置增强处理,method:被织入的方法,pointcut-ref:切入点-->
			<aop:before method="before" pointcut-ref="pointcut"/>
			 <!--后置增强处理,method:被织入的方法,pointcut-ref:切入点,returning:指定返回值的属性名为result-->
			<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
		</aop:aspect>
	</aop:config>

	<bean id="errorLogger" class="com.big3.aop.ErrorLogger"/>
	<!-- 异常增强注入 -->
	<aop:config>
		<!-- 定义切入点 -->
		<aop:pointcut id="point" expression="execution(public void save(com.big3.pojo.User))"/>
		<!-- 织入增强处理,ref:增强处理对象,注入的异常类 -->
		<aop:aspect ref="errorLogger">
			<!--after-throwing:异常增强, method:被织入的方法,pointcut-ref:切入点,throwing:为e参数注入异常实例 -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="point" throwing="e"/>
		</aop:aspect>
	</aop:config>

	<bean id="afterLogger" class="com.big3.aop.AfterLogger"  />
	<!-- 最终增强注入 -->
	<aop:config>
		<!-- 定义切入点 -->
		<aop:pointcut id="points" expression="execution(public void eat(int ,String))"/>
		<!-- 织入增强处理,ref:增强处理对象,注入的异常类-->
		<aop:aspect ref="afterLogger">
			<!--after:最终增强, method:被织入的方法,pointcut-ref:切入点 ;-->
			<aop:after method="after" pointcut-ref="points"/>
		</aop:aspect>
	</aop:config>

	<bean id="aroundLogger" class="com.big3.aop.AroundLogger"/>
	<!-- 环绕增强 -->
	<aop:config>
		<!-- 定义切入点 -->
		<aop:pointcut id="pointse" expression="execution(public int saves(com.big3.pojo.User))"/>
		<aop:aspect ref="aroundLogger">
			<aop:around method="aroundLogger" pointcut-ref="pointse"/>
		</aop:aspect>
	</aop:config>
</beans>

如果用注解配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
           	http://www.springframework.org/schema/beans/spring-beans.xsd
           	http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.2.xsd
			http://www.springframework.org/schema/aop
			http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!--扫描包中注解标注的类-->
	<context:component-scan base-package="com.big3.dao,com.big3.pojo,com.big3.aop"/>
	<!--注册Aspectj代理对象-->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

注意:如果用了spring mvc框架,IOC,AOP需要配置在springmvc配置文件里

<!--指定扫描注解定义的包,因为DAO层是自动实现,所以不用注解不用新建bean了,只需对service和pojo层aop层注解,实现loc新建bean的处理-->
<context:component-scan base-package="cn.kgc.service,cn.kgc.pojo,cn.kgc.service.impl,cn.kgc.controller,cn.kgc.aop"/>
<!--注册Aspectj代理对象,AOP切面增强用,注解方式-->
 <aop:aspectj-autoproxy/>

第三步:JAVA应用,如果不用注解,就不用注解标示

日志增强

@Aspect
@Component("usLogger")
public class UserServiceLogger {
	private static Logger log=Logger.getLogger(UserServiceLogger.class);
	//定义切入点方法
	@Pointcut("execution(public void eat(com.big3.pojo.User))")
	public void pointcut(){}

	//前置增强
	@Before("pointcut()")
	public void before(JoinPoint jp){
		log.info("调用"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法参数:\"+Arrays.toString(jp.getArgs()));\n" +
				"\t}"+Arrays.toString(jp.getArgs()));
	}

	//后置增强
	@AfterReturning(pointcut="pointcut()",returning="result")
	public void afterReturning(JoinPoint jp,Object result){
		log.info("调用"+jp.getTarget()+"的"+jp.getSignature()+"方法,方法返回值:"+result);
	}

异常增强

@Aspect //增强处理
@Component("errorLogger")
public class ErrorLogger {
    private static Logger log=Logger.getLogger(UserServiceLogger.class);

    //定义切入点方法
    @Pointcut("execution(public void save(com.big3.pojo.User))")
    public void pointcut(){}
    
    //定义异常增强方法
    @AfterThrowing(pointcut="pointcut()",throwing="e")
    public void afterThrowing(JoinPoint jp,RuntimeException e){
        log.error(jp.getSignature().getName()+"方法发生异常,"+e);
    }
}

最终增强

//最终增强,不管是否发生异常都执行,用于释放资源
@Aspect //增强处理
@Component("afterLogger")
public class AfterLogger {
    private static Logger log=Logger.getLogger(UserServiceLogger.class);

    //定义切入点方法
    @Pointcut("execution(public void eat(int ,String))")
    public void pointcut(){}

    //定义最终增强方法
    @After("pointcut()")
    public void after(JoinPoint jp){
        log.info(jp.getSignature().getName()+"方法结束了");
    }
}

环绕增

//环绕增强
@Aspect //增强处理
@Component("aroundLogger")
public class AroundLogger {
    private static final Logger log=Logger.getLogger(AroundLogger.class);

    //定义环绕增强方法
    @Around("execution(public int saves(com.big3.pojo.User))")
    public Object aroundLogger(ProceedingJoinPoint jp)throws Throwable{
        User user=(User)jp.getArgs()[0];
        log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法入参"+user.getName());
        try {
            int result=(int)jp.proceed();//执行目标方法并获得返回值
            log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。方法返回值"+(result+200));
            return result;
        } catch (Throwable throwable) {
            log.error(jp.getSignature().getName()+"方法发生异常"+throwable);
            throw throwable;
        }finally {
            log.info(jp.getSignature().getName()+"方法结束执行.");
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值