Spring declares slice/AOP in XML

Source: Internet
Author: User

In the Spring AOP configuration namespace, we can find the declarative slice selection, see below:

<aop:config><!--AOP definition begins--><aop:pointcut/><!--define pointcut--><aop:advisor/><!-- Define an AOP notifier--><aop:aspect><!--define a cut-off--><aop:pointcut/><!--define a pointcut--><aop:before/> <!--pre-notification--><aop:after-returning/>        <!--back notification--><aop:after-throwing/>           <!-- Post-exception notification--><aop:after/><!--post notification (regardless of whether the method of the notification succeeds)--><aop:around/><!--surround notification--><AOP: Declare-parents/>          <!--introduce notification  --></aop:aspect><!--Define the end of the slice--></aop:config><!- -AOP definition Ends--

I. declaration of Facets

Facets are objects that contain pointcuts and notifications, which are defined in a spring container as a Bean,schema method that requires a facet support bean that provides the state and behavior information for the slices and specifies the pointcut and notification implementations by configuration.

Facets are specified using the <aop:aspect> tag, and the ref attribute is used to refer to the slice support bean.

Slice support the Bean "Aspectsupportbean" is used exactly like a normal bean, and the slice uses the "ref" attribute to reference it.

Ii. Declaration of entry points

The pointcut is also a bean,bean definition in spring in a way that can be three different ways:

1) Under <aop:config> tags use <aop:pointcut> declare a pointcut bean, which can be used by multiple facets, preferably in a pointcut that needs to be shared. The pointcut uses the id attribute to specify the bean name, which is used by the Pointcut-ref property to refer to the pointcut using the ID property when the notification definition is specified, and the expression property specifies the Pointcut expressions:


<aop:config><aop:pointcut expression= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..))" Id= " Pointcut "/><aop:aspect ref=" Audienceaspect "><aop:before pointcut-ref=" pointcut "method=" taskSeats "/ ></aop:aspect></aop:config>
 2) Use <aop:pointcut> declare a pointcut bean under the <aop:aspect> tab, the pointcut can be used by multiple slices, but generally the pointcut is used only by that slice, and of course it can be used by other facets, but it is best not to use it, which uses the id attribute to specify the bean name, which is used by the Pointcut-ref property to refer to the pointcut at the notification definition. The expression property specifies Pointcut expressions:
<aop:config><aop:aspect ref= "Audienceaspect" ><aop:pointcut expression= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..)) " Id= "Pointcut"/><aop:before pointcut-ref= "pointcut" method= "Taskseats"/></AOP:ASPECT></AOP: Config>

    3) Anonymous pointcut Bean,You can specify a pointcut expression through the Pointcut property when declaring a notification, which is an anonymous pointcut that is used only by that notification:
<aop:config><aop:aspect ref= "Audienceaspect" ><aop:before pointcut= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..)) " Method= "Taskseats"/></aop:aspect></aop:config>

Third, the declaration of notice directly to see an example:
Package Cn.com.ztz.spring.service;public interface Showservice {public void show ();
Package Cn.com.ztz.spring.service;public class Showserviceimpl implements showservice{@Overridepublic void Show () {      Showbefore ();//showerror ();//anomaly Test    showend ();} public void Showbefore () {System.out.println ("showbefore============");} public void ShowError () {System.out.println ("showerror============"); throw new RuntimeException (); public void ShowEnd () {System.out.println ("showend===============");}}

Package Cn.com.ztz.spring.service;public class Audienceaspect {public void Taskseats () {System.out.println ("wait for show start = = = ");} public void Applaud () {System.out.println ("Applause ========="); public void Demandrefund () {System.out.println ("refund off-site ======");}}
 <bean id= "Showservice" class= "Cn.com.ztz.spring.service.ShowServiceImpl"/> <bean id= "Audienceaspect" class= "Cn.com.ztz.spring.service.AudienceAspect"/><AOP:CONFIG><AOP: Pointcut id= "pointcut" expression= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..))" /><aop:aspect ref= "Audienceaspect" ><aop:before pointcut-ref= "pointcut" method= "TaskSeats"/><AOP : after-returning pointcut-ref= "Pointcut" method= "applaud"/><aop:after-throwing pointcut-ref= "Pointcut" Method= "Demandrefund"/></AOP:ASPECT></AOP:CONFIG> 
public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Classpath: Applicationcontext.xml ");      Showservice HS = Ctx.getbean ("Showservice", showservice.class);      System.out.println ("======================================");    Hs.show ();    System.out.println ("======================================");}
Console output Results:======================================
Waiting for the show to start = =
showbefore============
showend===============
Applause =========
======================================
Iv. passing parameters for notificationsNow there are a lot of people watching the show, I want to pass in a person's name, how to do, to see:
Public interface Showservice {public void Showbefore (String param);
public class Showserviceimpl implements Showservice{public void Showbefore (String param) {System.out.println (" showbefore============ ");}}
<pre name= "code" class= "Java" >public class Audienceaspect {public void taskseats (String param) { System.out.println (param+ ", wait for program start = = =");}}
   <bean id= "Showservice" class= "Cn.com.ztz.spring.service.ShowServiceImpl"/> <bean    id= "Audienceaspect" class= "Cn.com.ztz.spring.service.AudienceAspect"/><aop:config><aop:aspect ref= "Audienceaspect" > <aop:before pointcut= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..)) and args (param)" method= " Taskseats (java.lang.String) "arg-names=" param "/></aop:aspect></aop:config>

public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Classpath: Applicationcontext.xml ");      Showservice HS = Ctx.getbean ("Showservice", showservice.class);      System.out.println ("======================================");    Hs.showbefore ("Zhang San");    System.out.println ("======================================");}
Console output Results:======================================
Zhang San, wait for the show to start = =
showbefore============
======================================
V. Declaration of Surround NoticeThe notification that surrounds the method at the connection point selected at the Pointcut, the surround notification is powerful, determines whether the target method executes, when it executes, whether the method parameter needs to be replaced at execution time, and whether the return value needs to be replaced by the <aop:aspect> tag. <aop:around > Label declaration:Surround notification The first parameter must be a org.aspectj.lang.ProceedingJoinPoint type, using the Proceedingjoinpoint proceed () method inside the notification implementation method to make the target method execute, proceed Method can pass in an optional object[] array whose value will be used as a parameter when the target method executes.
Public interface Showservice {public void Showaround (String param);
public class Showserviceimpl implements Showservice{public void Showaround (String param) {System.out.println (" showaround============ "+param);}}
public class Audienceaspect {public Object aroundadvice (Proceedingjoinpoint pjp) throws Throwable {      System.out.println ("Around before advice===========");      Object RetVal = pjp.proceed (new object[] {"Around"});      System.out.println ("Around after advice===========");      return retVal; }}
   <bean id= "Showservice" class= "Cn.com.ztz.spring.service.ShowServiceImpl"/> <bean    id= "Audienceaspect" class= "Cn.com.ztz.spring.service.AudienceAspect"/><aop:config><aop:aspect ref= "Audienceaspect" > <aop:around pointcut= "Execution (* cn.com.ztz.spring.service.showserviceimpl.* (..))" method= "AroundAdvice"/> </aop:aspect></aop:config>
public static void Main (string[] args) {<span style= "white-space:pre" ></span>applicationcontext ctx = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");      Showservice HS = Ctx.getbean ("Showservice", showservice.class);      System.out.println ("======================================");    Hs.showaround ("Zhang San");    System.out.println ("======================================");}
Console output Results:======================================
Around before advice===========
Showaround============around
Around after advice===========
======================================
Sixintroducedsome programming languages, such as Ruby and groovy, have the idea of an open class. They can add new methods to an object or class without directly modifying the definition of the object or class. Unfortunately, Java is not a dynamic language, and once the compilation is complete, it's hard to add new functionality to the class. think about it now. In fact, with the AOP concept called introduction, facets can add new methods to spring beans. Spring Introduction Allows the introduction of new interfaces for target objects by using < aop:declare-parents> tags within the < aop:aspect> tags.
<aop:declare-parents
types-matching= "ASPECTJ syntax type expression"
Interface introduced by implement-interface= "
Default-impl= "Introducing the default implementation of an interface"
Delegate-ref= "Introducing the default implementation bean reference for an interface"/>

Take a look at the example: we define a new interface and implementation
Public interface Declareservice {public void declare ();}

public class Declareserviceimpl implements Declareservice {@Overridepublic void declare () {SYSTEM.OUT.PRINTLN ("declare =====================");}}

   <bean id= "Showservice" class= "Cn.com.ztz.spring.service.ShowServiceImpl"/> <bean    id= "Declareservice" class= "Cn.com.ztz.spring.service.DeclareServiceImpl"/><AOP:CONFIG><AOP:ASPECT><AOP: Declare-parents types-matching= "cn.com.ztz.spring.service.showserviceimpl+" implement-interface= " Cn.com.ztz.spring.service.DeclareService "delegate-ref=" Declareservice "/></aop:aspect></aop:config >

public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext ("Classpath: Applicationcontext.xml ");  Declareservice HS = Ctx.getbean ("Showservice", declareservice.class);      System.out.println ("======================================");    Hs.declare ();    System.out.println ("======================================");}

We get or showservice bean, run the next test method output result:======================================
declare=====================
======================================

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring declares slice/AOP in XML

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.