首页 > 代码库 > Spring 的切面编程如何实现 注入切面程序

Spring 的切面编程如何实现 注入切面程序

首先定义


一个切面类

@Aspect
@Component

用以上注解表示该类为切面类

并在该切面类中自定义两个方法,分别是执行前执行后的

public void before(JoinPoint jp) {
       ……
    }

    public void after(JoinPoint jp) {
        ……
    }


spring配置文件中注入该类


然后通过aop配置切面程序

<aop:config>
        <aop:pointcut id="beforeMethod"
                      expression="execution(public * com.DemoClass.*(..))" />
        <aop:aspect id="myAspect" ref="acpectInterceptor">
            <aop:pointcut id="afterMethod"
                      expression="execution(public * com.DemoClass.*(..))" />
            <aop:before method="before" pointcut-ref="beforeMethod"/>
            <aop:after-returning method="after" pointcut-ref="afterMethod"/>
        </aop:aspect>
    </aop:config>


切面拦截处理程序

package com.tree.common;

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

@Aspect
@Component
public class MyAspectInterceptor {

	public void before(JoinPoint jp) {
		Object[] args = jp.getArgs();
		for(int i=0;i<args.length;i++) {
			System.out.println("before:"+args[i].getClass().getName());
		}
		System.out.println("我是在切面类方法[前]执行的");
	}

	public void after(JoinPoint jp) {
		Object[] args = jp.getArgs();
		for(int i=0;i<args.length;i++) {
			System.out.println("after:"+args[i].getClass().getName());
		}
		System.out.println("我是在切面类方法[后]执行的");
	}

}

切面程序

package com.tree.demo.aop;

import org.springframework.stereotype.Service;

@Service
public class AspectProcessor {

	public String aspectBizProcess() {
		return "{result:我是切面处理程序}";
	}
	
}

action

package com.tree.aop.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tree.common.Constants;
import com.tree.demo.aop.AspectProcessor;


@Controller
@RequestMapping(value=http://www.mamicode.com/"/aop")>
配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
	default-autowire="byName">

	<aop:config>
        <aop:pointcut id="beforeMethod"
                      expression="execution(public * com.tree.demo.aop.AspectProcessor.*(..))" />
        <aop:aspect id="myAspectProcessor" ref="myAspect">
            <aop:pointcut id="afterMethod"
                      expression="execution(public * com.tree.demo.aop.AspectProcessor.*(..))" />
            <aop:before method="before" pointcut-ref="beforeMethod"/>
            <aop:after-returning method="after" pointcut-ref="afterMethod"/>
        </aop:aspect>
    </aop:config>
		

</beans>

启动tomcat服务器,访问action

http://localhost:8080/demo.web/aop/test.do



可以看出切面程序已经执行了。











Spring 的切面编程如何实现 注入切面程序