Ssm-spring-13:spring Regexpmethodpointcutadvisor Regular Method entry-point consultant

Source: Internet
Author: User

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

Regexpmethodpointcutadvisor: Regular method Entry-point consultant

Core:

<property name= "pattern" value= ". *do.*" ></property> means full name (package name, interface name, method name)
Operator name meaning
. The dot number represents any single character
+ PLUS sign indicates one or more occurrences of the previous character
* Asterisks indicate that the previous character appears 0 or more times

Specific Use cases:

A class that implements two methods: Someserviceimpl

Package Cn.dawn.day16advisor02;/** * Created by Dawn on 2018/3/8.*/ Public classSomeserviceimpl { Public voidDosome () {System. out. println ("Do something"); }     Public voidDoany () {System. out. println (" Do any"); }}

A way to implement an arbitrarily enhanced interface: Here is the post-enhancement, I named Loggerafter

Package Cn.dawn.day16advisor02;import Org.springframework.aop.afterreturningadvice;import Java.lang.reflect.Method;/** * Created by Dawn on 2018/3/5.*//*Post Enhancement*/ Public classLoggerafter implements Afterreturningadvice { Public voidafterreturning (Object o, Method, object[] objects, Object O1) throws Throwable {System. out. println ("===============after=================="); }}

Large configuration file:

<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdHttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--objects to enhance--<bean id="Service" class="Cn.dawn.day16advisor02.SomeServiceImpl"></bean> <!--enhanced Content-<bean id="Myadvice" class="Cn.dawn.day16advisor02.LoggerAfter"></bean> <!--Advisor--<bean id="Myadvisor" class="Org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="Advice" ref="Myadvice"></property> <property name="pattern"Value=". *do.*"></property> <!--The name above can also be a patterns,value can write multiple regular. A comma-delimited, matching multiple regular, method name as long as appropriate one of the regular is enhanced-</bean> <!--agent Factory bean--> <bean id="proxyfactory" class="Org.springframework.aop.framework.ProxyFactoryBean"> <!--objects to enhance--<property name="Target" ref="Service"></property> <!--enhanced Content-<property name="Interceptornames"Value="Myadvisor"></property> </bean></beans>

Single test method:

Package Cn.dawn.day16advisor02;import Org.junit.test;import org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Dawn on 2018/3/3.*/ Public classtest20180310 {@Test/*AOP Agent Factory bean Exception Enhancement*/     Public voidt01 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext-day16advisor02.xml"); Someserviceimpl Service= (Someserviceimpl) Context.getbean ("proxyfactory");        Service.dosome ();    Service.doany (); }}

Ssm-spring-13:spring Regexpmethodpointcutadvisor Regular Method entry-point consultant

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.