Ssm-spring-12:spring namematchmethodpointcutadvisor Name Matching 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-------------

Advice is a notification
Advisor is a consultant

Advisor (advisor)  

notification advice is a facet (Aspect) provided by spring. However, it is too simple to weave the facets into all the target methods of the target class, and the facets cannot be woven into the specified target method.

  Advisor Advisor is another facet provided by spring. It can be used to perform more complex facets weaving functions, which can selectively enhance some of the methods in the facets.

Pointcutadvisor is one of the advisors that can specify a specific entry point. The consultant wraps the notice and weaves the slices into different pointcuts at different points in time, depending on the type of notification .

The Pointcutadvisor interface has two more commonly used implementation classes:
*:namematchmethodpointcutadvisor name matching method pointcut Consultant
*: Regexpmethodpointcutadvisor Regular Expression method entry-point Advisor

<property name= "pattern" value= ". *do.*" ></property> Represents the method full name (package name, interface name, method name)
operator name meaning

+ plus indicates that the previous character appears one or more times

Name matching method entry point consultant Namematchmethodpointcutadvisor
<!--03. Configure Advisor Advisor Packaging advice-->
<bean id= "Beforeadvisor" class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >
<property name= "Advice" ref= "Beforeadvice" ></property>
<property name= "Mappednames" value= "do*" ></property>
</bean>

Name matching method The case of the approach to the consultant method

A class that needs to be enhanced: Someserviceimpl

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

Classes that enhance content

Package Cn.dawn.day15advisor01;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=================="); }}

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.day15advisor01.SomeServiceImpl"></bean> <!--enhanced Content-<bean id="Myadvice" class="Cn.dawn.day15advisor01.LoggerAfter"></bean> <!--Advisor--<bean id="Myadvisor" class="Org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="Advice" ref="Myadvice"></property> <!--<property name="Mappednames"Value="do*"></property>--> <property name="Mappednames"Value="Doany"></property> </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>

The advisor's Mappednames value can be manipulated so that you want to match the method to match to, can be used to say yes? A wildcard character? Way, similar to do* or direct write method name Doany or separated by commas two methods Dosome,doany

Boy, do you get it?

Single test method:

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

Ssm-spring-12:spring namematchmethodpointcutadvisor Name Matching 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.