Spring (11) implementing aspect-oriented AOP using spring annotations

Source: Internet
Author: User
Tags aop xmlns

Some of the concepts that AOP involves:

Aspect: The abstraction of crosscutting concerns is a tangent, similar to a class, except that the focus of the two is different, and the class is an abstraction of the feature of the object, while the tangent is the abstraction of the crosscutting concern.
Joinpoint (connection point): The so-called connection point refers to those points that are intercepted. In spring, these points refer to methods because spring only supports connection points for method types (in fact joinpoint can also be field or class constructors).
Pointcut (entry point): The so-called pointcut refers to the definition of what we want to intercept for those joinpoint.
Advice (notice): The so-called notification refers to the interception to joinpoint after the things to do. Notification is divided into pre-notification, post-notification, exception notification, final notification, surround notification.
Target object: The target object of the agent.
Weave (weaving): Refers to the process of applying aspect to a target object and causing the proxy object to be created.
Introduction (introduced): without modifying the class code, Introduction can dynamically add methods or field to the class at run time.

Spring provides AOP development based on both XML configuration and annotation-based approaches.

The use of AOP also has its own configuration in Beans.xml:

<?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:context= "Http://www.springframework.org/sch Ema/context "
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<aop:aspectj-autoproxy/>
<bean id= "Myinterceptor" class= "Test.spring.aop.MyInterceptor" ></bean>
<bean id= "Personservice" class= "Test.spring.service.impl.PersonServiceBean" ></bean>
</beans>

import some required jar packages,

Package test.spring.service;

Public interface Personservice {public

	void Save (String name);

	Public String GetResult ();

}

Package Test.spring.service.impl;

Import Test.spring.service.PersonService;

The proxy object implements the target object all interfaces public
class Personservicebean implements Personservice {public

	Personservicebean () {

	}

	@Override public
	void Save (String name) {
		System.out.println ("Save ()->>" + name);
		throw new RuntimeException (">>----Custom Exception----<<");
	}

	@Override public
	String GetResult () {return
		' GetResult () ==>> returns the result ";
	}

}
Section

Package TEST.SPRING.AOP;
Import Org.aspectj.lang.ProceedingJoinPoint;
Import Org.aspectj.lang.annotation.After;
Import org.aspectj.lang.annotation.AfterReturning;
Import org.aspectj.lang.annotation.AfterThrowing;
Import Org.aspectj.lang.annotation.Around;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Before;
Import Org.aspectj.lang.annotation.Pointcut;

Import org.springframework.stereotype.Component; 
	@Aspect public class Myinterceptor {//Pointcut @Pointcut ("Execution (* test.spring.service.impl.personservicebean.* (..))") private void Anymethod () {}//declares a pointcut///parameterless Pointcut//@Before ("Anymethod ()")//defines a pointcut with parameters, and the parameter names need to be the same. There is only one String parameter to the method that is intercepted @Before ("Anymethod () && args (userName)") public void Doaccesscheck (string
	UserName) {System.out.println ("Pre-notification-->>" + userName); }//Get notification @AfterReturning with return result (Pointcut = "Anymethod ()", returning = "result") public void doafterreturning (String re Sult) {System.out.println ("POST notification-->> "+ result);
	} @After ("Anymethod ()") public void Doafter () {System.out.println ("final Notice"); }//The method called will not call this method @AfterThrowing (pointcut = "Anymethod ()", throwing = "E") public void doafterthrowing (Exception
	e) {System.out.println ("Exception notification--" + e); } @Around ("Anymethod ()") Public Object Doaround (Proceedingjoinpoint pjoinpoint) throws Throwable {System.out.println
		("Surround notice");
		If Pjoinpoint.proceed () is not executed here, the methods that are intercepted later are not executed, and are well suited for rights management Object result = Pjoinpoint.proceed ();
		System.out.println ("Exit");
	return result;
 }

}

Define the facets and give it to spring management. There are two ways to give spring, one is to define beans in beans.xml, and the other is to scan through @component

Package test.spring.junit;

Import Org.junit.Test;
Import Org.springframework.context.support.AbstractApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import Test.spring.service.PersonService;

public class AOPTest3 {

	@Test public
	void Test () {
		Abstractapplicationcontext acontext =//
		NEW Classpathxmlapplicationcontext ("Beans.xml");
		Personservice Pservice = (personservice) acontext
				. Getbean ("Personservice");
		Pservice.save ("Lindl");
		Pservice.getresult ();
		Acontext.close ();
	}

}
Test the Save method



Test GetResult 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.