SpringMVC 如何配置aop

aop 面向切面的编程

Spring如何配置一个简单的aop

第一步:首先是编写一个Operator的类,(类的名字你随便起,开心最为重要)

package com.wdg.operator;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.AopInvocationException;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Operator
{

    private Logger logger = Logger.getLogger(this.getClass());

    public void pointCut() {
    }

    public void doBefore(JoinPoint joinPoint) {
        System.out.println("AOP Before Advice...");
        return;
    }

    public void doAfter(JoinPoint joinPoint) {
        System.out.println("AOP After Advice...");
        return;
    }

    public Object afterReturn(JoinPoint joinPoint, Object returnVal) {
        System.out.println("AOP AfterReturning Advice:" + returnVal);
        return null;
    }

    public void afterThrowing(JoinPoint joinPoint, Throwable error) {
        System.out.println("AOP AfterThrowing Advice..." + error);
        System.out.println("AfterThrowing...");
    }

    public Object around(ProceedingJoinPoint pjp) {
        System.out.println("AOP Aronud before...");
        try {
            System.out.println(pjp.toString());
        }
        catch (AopInvocationException e) {
            logger.info(e.getMessage());
        }
        catch(Exception e){
            logger.info(e.getMessage());
        }
        System.out.println("AOP Aronud after...");
        return null;
    }

}

第二步:引入配置文件,这个地方我专门为aop的配置新增了一个配制文件spring-aop.xml,在applicationContent.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/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<import resource="classpath:spring-beans.xml" />
	<import resource="classpath:spring-jdbc.xml" />
	<import resource="classpath:spring-aop.xml" />
	<!-- <import resource="classpath:spring-monodb.xml" /> <import resource="classpath:spring-redis.xml" 
		/> -->
</beans>

spring-aop.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/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<aop:aspectj-autoproxy />

	<!-- 使用注解时要开启注解扫描 要扫描的包 -->
	<bean id="operator" class="com.wdg.operator.Operator"></bean>

	<aop:config proxy-target-class="true">
		<aop:pointcut id="servicePointCut"
			expression="execution(* com.wdg.login.impl..*..*(..))" />
		<aop:aspect id="serviceAspect" ref="operator">
			<aop:before method="doBefore" pointcut-ref="servicePointCut" />
			<!-- <aop:around method="around" pointcut-ref="servicePointCut" /> -->
			<aop:around method="doAfter" pointcut-ref="servicePointCut" />

		</aop:aspect>
	</aop:config>
</beans>

这样当我们调用com.wdg.login.impl中的下面的类,里面的任意的方法的时候,都能够监控到,并且执行Opertor中的doBefore和doAfter的方法

希望对你有所帮助

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值