码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-12-10 19:56:18      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:spring   aop   

首先定义


一个切面类

@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="/aop")
public class TestAopAction {
	
	private static Log log = LogFactory.getLog(Constants.LOGFILE);
	
	private AspectProcessor ap;
	
	
	public void setAp(AspectProcessor ap) {
		this.ap = ap;
	}


	@RequestMapping(value="/test",produces = "application/json")
	@ResponseBody
	public String testAop() {
		log.info("进入aoptest  Action");
		return ap.aspectBizProcess();
	}
	
}

配置文件

<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

bubuko.com,布布扣

bubuko.com,布布扣

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











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

标签:spring   aop   

原文地址:http://blog.csdn.net/simonchi/article/details/39927853

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!