[Pin to top] Spring cut-point aspect AOP -- XML non-Annotation

Source: Internet
Author: User
The AOP proxy is a method (called a "cut point"), which is defined in the "cut" section for what to do, what to do, what to do before the method is executed, and what to do after the exception. <! -- Add the AOP header, a total of five --> <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" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-2.5.xsdhttp: // beans http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <! -- In the spring-context.jar's meta-INF // spring-context.jar, determine which version of XSD is --> <! -- <Context: annotation-config> </Context: annotation-config> --> <! -- Tell spring which package to scan. I asked him to scan all classes of proxy. Spring. Agent -->

<Context: component-scan base-package = "proxy. Spring. Agent. *"> </Context: component-scan> <! -- Define two bean classes --> <bean id = "myagent" class = "proxy. spring. agent. agentproxy "> </bean> <bean id =" Singer "class =" proxy. spring. agent. mysinger "> </bean> <! -- This is the question. Spring AOP cut points must have cut points and cut points. below is the cut point pointcut and cut point aspect --> <AOP: config> <! -- Define a cut point --> <! -- The first execution expression indicates any return type (public ,..), the second parameter indicates proxy. spring. agent. any method in mysinger ,*(..) any parameter of any method, that is, all methods in this class will be proxies --> <AOP: pointcut expression = "execution (* proxy. spring. agent. mysinger. *(..)) "id =" point1 "/> <! -- Define a plane --> <AOP: aspect id = "aspect1" ref = "myagent"> <! -- This is what to do before, after, and when an exception is thrown --> <AOP: Before method = "before" pointcut-ref = "point1"/> <AOP: after method = "after" pointcut-ref = "point1"/> <AOP: After-returning method = "afterreturn" pointcut-ref = "point1"/> <AOP: after-throwing method = "afterthrow" pointcut-ref = "point1"/> <AOP: Und und method = "around" pointcut-ref = "point1"/> </AOP: aspect> </AOP: config> </beans>

----------------------------------------- Source code example (artist example )----------------------------------

----------------------------------------- XML ---------------------- (Conventions between singers and brokers )--------------------------------

<? XML version = "1.0" encoding = "UTF-8"?> <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" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2. 5. xsdhttp: // response "> <! -- In the spring-context.jar's meta-INF // spring-context.jar, determine which version of XSD is --> <! -- <Context: annotation-config> </Context: annotation-config> --> <context: component-scan base-package = "proxy. spring. agent. * "> </Context: component-scan> <bean id =" myagent "class =" proxy. spring. agent. agentproxy "> </bean> <bean id =" Singer "class =" proxy. spring. agent. mysinger "> </bean> <AOP: config> <AOP: pointcut expression =" execution (* proxy. spring. agent. mysinger. *(..)) "id =" point1 "/> <AOP: aspect id =" aspect1 "ref =" myagent "> <AOP: before method = "before" pointcut-ref = "point1"/> <AOP: After method = "after" pointcut-ref = "point1"/> <AOP: after-returning method = "afterreturn" pointcut-ref = "point1"/> <AOP: After-throwing method = "afterthrow" pointcut-ref = "point1"/> <AOP: around method = "around" pointcut-ref = "point1"/> </AOP: aspect> </AOP: config> </beans>

----------------------------------------------- Proxy. Spring. Agent. agentproxy ----------------- (the artist's agent is responsible for the singer's meal ..) ------------------------------

Package proxy. spring. agent; import javax. sound. midi. sysexmessage; import Org. aspectj. lang. proceedingjoinpoint; public class agentproxy {public void before () {system. out. println ("before ()........ ");} public void after () {system. out. println ("after ()...... ");} public void afterthrow () {system. out. println ("after ()...... ");} public void around (proceedingjoinpoint p) {// obtain the parameter object [] O = P. getargs (); For (INT I = 0; I <O. length; I ++) {object = O [I]; system. out. println (object);} Try {// required. If no value is added, the original function P is not executed. proceed ();} catch (throwable e) {// todo auto-generated catch blocke. printstacktrace ();} system. out. println ("around ().... ");} public void afterreturn () {system. out. println ("afterreturn ()...... ");}}

-------------------------------------------- Proxy. Spring. Agent. mysinger ------------------- (famous singer )--------------------

Package proxy. spring. agent; public class mysinger {public void sing () {system. out. println ("I sing");} Public String SS (string name) {system. out. println ("mysinger" + ".... "+ name); Return name ;}}

-------------------------------- Test -------- main ------------------

Package proxy. spring. agent; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class test {public static void main (string [] ARGs) {applicationcontext APP = new classpathxmlapplicationcontext ("Proxy/spring/agent/application. XML "); mysinger singer = (mysinger) app. getbean ("Singer"); singer. sing (); system. out. println ("Main" + ".... "+ singer. SS ("Jay Chou "));}}

-------------------- Result -------------------

Before ()........
I sing
After ()......
Afterreturn ()......
Around ()....
Before ()........
Jay Chou
Mysinger... Jay Chou
After ()......
Afterreturn ()......
Around ()....
Main... null
------------------------------------------------------------- Annotations used in the next article

 

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.