Spring's slice programming AOP how to implement the injection slice program __ Programming

Source: Internet
Author: User
Tags aop log log tomcat server

First define


A slice class

@Aspect
@Component

Use the above annotation to indicate that this class is a slice class

And customize two methods in this slice class, respectively, before execution

public void before (Joinpoint jp) {
       ...
    }

    public void after (Joinpoint jp) {
        ...
    }


This class is injected into the spring configuration file


Then configure the slice program with 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>


Slice intercept Handler

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 ("I was executing" in the cut-cut class method [before]);
	}

	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 ("I was executing" in the cut-cut class method [after]);
	}



Slice Program

Package COM.TREE.DEMO.AOP;

Import Org.springframework.stereotype.Service;

@Service public
class Aspectprocessor {public

	String aspectbizprocess () {return
		' {result: I am a slice handler} ';
	}
	
}

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 ("Enter Aoptest  Action");
		return ap.aspectbizprocess ();
	}
	


Configuration file

<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> &LT;AOP :p ointcut 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" poi ntcut-ref= "Aftermethod"/> </aop:aspect> </aop:config> </beans>

Start Tomcat server, Access action

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



You can see that the slice program has been executed.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.