How Spring's slice programming implements the injection plane program

Source: Internet
Author: User
Tags log log

First define


A slice class

@Aspect @component

Use the above annotations to indicate that the class is a slice class

And customize two methods in the Slice class, respectively, after executing the

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


Inject the class in 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 interception Handler

Package Com.tree.common;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.annotation.aspect;import org.springframework.stereotype.Component; @Aspect @componentpublic 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 executed in the tangent class method [ex]");} 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 executed in the tangent class method [after]");}}

Slice Program

Package Com.tree.demo.aop;import Org.springframework.stereotype.Service; @Servicepublic 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);p rivate aspectprocessor ap;public void Setap (Aspectprocessor ap) { This.ap = AP;} @RequestMapping (value= "/test", produces = "Application/json") @ResponseBodypublic 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/beanshttp:// www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp:// www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp:// Www.springframework.org/schema/aop/spring-aop-3.0.xsd "default-autowire=" ByName "><aop:config> &LT;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 meth OD= "Before" pointcut-ref= "Beforemethod"/> <aop:after-returning method= "after" pointcut-ref= "AfterMethod"/&        Gt </aop:aspect> </aop:config></beans>

Start the Tomcat server and access the action

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



You can see that the cut-off program has been executed.











How Spring's slice programming implements the injection plane program

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.