Spring Learning 4-aspect-oriented (AOP) schema configuration method

Source: Internet
Author: User
Tags throwable

I. Implementation of AOP steps through scheme configuration ( Environment of Spring AOP Environment and previous blog post Spring interface in the same way)
Step One, write the business class:

public class Aspectbusiness {
public string Delete (String obj) {
System.out.println ("========== call Pointcut:" + obj + "said:" You dare to delete me! ===========\n ");
return obj + ": Aim ~";

publicstring Add (String obj) {
System.out.println ("================ This method cannot be cut ... ==============\n ");
return obj + ": Aim ~ Hehe! ";

publicstring Modify (String obj) {
System.out.println ("================= This also set join cut bar ====================\n");
return obj + ": Aim and AIM! ";


Step Two, write the slice class: The Slice class, contains all the notifications

public class Aspectadvice {

//Front-facing notifications
public void Dobefore (Joinpoint JP) {
System.out.println ("=========== into before advice============\n");

System.out.print ("Ready on" + Jp.gettarget (). GetClass () + "Object");
System.out.print (Jp.getsignature (). GetName () + "method to");
System.out.print (Jp.getargs () [0] + "' Delete! \ n ");

System.out.println ("To enter the Pointcut method \ n");

Post notification
@param JP
Connection point
@param result
return value

Public Voiddoafter (Joinpoint JP, String result) {
System.out.println ("========== into after advice=========== \ n");
System.out.println ("The Pointcut method has been executed \ n");

System.out.print (Jp.getargs () [0] + "in");
System.out.print (Jp.gettarget (). GetClass () + "on object");
System.out.print (Jp.getsignature (). GetName () + "method removed");
System.out.print ("Left only:" + result + "\ n");

Surround Notifications
Connection point

Public ObjectDoaround (Proceedingjoinpoint pjp) throws Throwable {
System.out.println ("=========== into around surround Method! =========== \ n ");

Action to execute before calling the target method
System.out.println ("Before calling Method: Execute!") \ n ");

Parameters of the Calling method
object[] args = Pjp.getargs ();
Method name to invoke
String method = Pjp.getsignature (). GetName ();
Get target Object
Object target = Pjp.gettarget ();
Execution of the return value of the method: calling the proceed () method triggers the Pointcut method execution
Object result = Pjp.proceed ();

System.out.println ("Output:" + args[0] + ";" + method + ";" + target + ";" + result + "\ n");
System.out.println ("Call method End: Execute after!") \ n ");
return result;

Exception notification

Public Voiddothrow (Joinpoint JP, Throwable e) {
System.out.println ("delete error");


step Four, the configuration file writing:

<?xml version= "1.0" encoding= "UTF-8"?>
<beansxmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "

<!--============================== using spring to configure aop================================--> with ASPECTJ

<!--declare a business class--
<bean id= "aspectbusiness" class= "aop.schema.AspectBusiness"/>

<!--Notice class--
<bean id= "Aspectadvice" class= "Aop.schema.advice.AspectAdvice"/>

<aop:aspect id= "Businessaspect" ref= "Aspectadvice" >
<!--Configure the object to be cut in--
<aop:pointcut id= "point_cut" expression= "Execution (*aop.schema.*.* (..))"/>
<!--match only the Add method as a pointcut
<aop:pointcut id= "Except_add" expression= "Execution (* aop.schema.*.add (..))" />

<!--front-facing notifications-
<aop:before method= "Dobefore" pointcut-ref= "Point_cut"/>
<!--post notification returning specify the return parameter--
<aop:after-returning method= "Doafter"
pointcut-ref= "Point_cut" returning= "result"/>
<aop:around method= "Doaround" pointcut-ref= "Point_cut"/>
<aop:after-throwing method= "Dothrow" pointcut-ref= "Point_cut" throwing= "E"/>


Step five, test class:

public class Debug {

publicstatic void Main (string[] args) {
ApplicationContext context = Newclasspathxmlapplicationcontext ("Aop/schema_aop.xml");
Aspectbusiness business = (aspectbusiness) context.getbean ("aspectbusiness");
Business.delete ("Cat");


Spring Learning 4-aspect-oriented (AOP) schema configuration method

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.