logs, security, and transaction management are all places where AOP can be applied.
Features that are distributed across multiple applications are referred to as crosscutting concerns.
Meaning of the tangent parameter:
Execution (* com.spring.service.aservice.* (..))
The first * represents any return type, Com.spring.service.AService has an interface, and the second * represents any method that contains an interface. Represents an arbitrary parameter type. The interface Aservice is specified, and any subclasses that implement the interface are included.
Execution (* com.spring.service.aservice.* (..)) && within (com.spring.service.impl.*)
Represents the implementation class for the Aservice interface at the same time if the Com.spring.service.impl package. The operators between expressions include &&,| |,! or And,or,not.
If you need to know how long a method takes to execute, surround notification is a good fit to complete.
Add parameter information Execution (* com.spring.service.aservice.* (String)) and args (thoughts) for tangency points, Pass the string type thoughts in the Aservice implementation class method as a parameter to the interceptor.
When using annotations, @pointcut needs to be attached to a method, which itself is unimportant and can be an empty implementation.
Implement IOC and AOP based on annotations and automatic scanning.
Configuration file Contextbyaspect.xml Information:
<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config/> <!--auto-scan (auto-inject)--<context:component-scan base- Package= "Com.spring". * "/> <aop:aspectj-autoproxy/> </beans>
Define the interface class:
Package com.spring.service;/** * Interface A */public interface Aservice {public void Fooa (String _msg); public void BarA ();}
To define an implementation class:
Package Com.spring.service.impl;import Org.springframework.stereotype.component;import com.spring.service.aservice;/** * Interface a implementation class */@Component ("Aservice") public class Aserviceimplbyaspect implements Aservice {public aserviceimplbyaspect () {System.out.println ("a begin!");} public void BarA () { System.out.println ("Aserviceimpl.bara ()"); } public void Fooa (String _msg) { System.out.println ("Aserviceimpl.fooa (msg:" +_msg+ ")");} }
Defining Facets:
Package Com.spring.aop;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.pointcut;import org.springframework.stereotype.component;/** * Facets * */@ Aspect@componentpublic class Testaspectbyaspect {@Pointcut (value = "Execution (* com.spring.service.aservice.* (..))") public void before () {} public void Doafter (Joinpoint jp) {System.out.println ("Log ending Method:" + Jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ()); } public Object Doaround (Proceedingjoinpoint pjp) throws Throwable {long time = System.currenttimemillis (); Object RetVal = Pjp.proceed (); Time = System.currenttimemillis ()-time; SYSTEM.OUT.PRINTLN ("Process time:" + time + "MS"); return retVal; } @Before (value = "before ()") public void Dobefore (Joinpoint JP) { System.out.println ("Log begining Method:" + Jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName ()); } public void Dothrowing (Joinpoint JP, Throwable ex) {System.out.println ("method" + jp.gettarget (). GetClass (). GetName () + "." + jp.getsignature (). GetName () + "throw exception"); System.out.println (Ex.getmessage ()); private void Sendex (String ex) {//todo send SMS or email alert}}
To define a unit test class:
Package Com.spring.test;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.stereotype.component;import Org.springframework.test.abstractdependencyinjectionspringcontexttests;import Com.spring.service.AService; Import Com.spring.service.impl.bserviceimplbyaspect;public class Aoptestbyaspect extends abstractdependencyinjectionspringcontexttests {@Autowired (required = false) Private Aservice aservice;protected String[] Getconfiglocations () {string[] configs = new string[] {"/contextbyaspect.xml"};return configs;} /** * Test normal call */public void Testcall () {System.out.println ("springtest junit test"), Aservice.fooa ("JUnit test Fooa"); Aservice.bara ();} public void Setaservice (Aservice service) {aservice = service;}}
Spring's AOP