Meavn Code
<!--SPRINGAOP Core Pack -<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-aop</Artifactid> <version>4.0.2.RELEASE</version></Dependency> <!--using the ASPECTJ mode annotation requires the appropriate package - <Dependency> <groupId>Org.aspectj</groupId> <Artifactid>Aspectjrt</Artifactid> <version>1.8.9</version> </Dependency> <Dependency> <groupId>Org.aspectj</groupId> <Artifactid>Aspectjweaver</Artifactid> <version>1.8.9</version> </Dependency>
Note: This version number must be compatible with the JDK or it will be reported error at:: 0 can ' t find referenced pointcut pointcutname errors
Spring-mvc.xml Configuration
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "http://www.springframework.org/schema/aop"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/sprin G-beans-4.0.xsd Http://www.springframework.org/schema/context http://www . springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http: WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd" >
<!--enable ASJECTJ support-
<aop:aspectj-autoproxy proxy-target-class= "true"/>
Note: The underscore is an item that must be configured
Annotation class Configuration
ImportOrg.apache.log4j.Logger;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.After;Importorg.aspectj.lang.annotation.AfterReturning;Importorg.aspectj.lang.annotation.AfterThrowing;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.springframework.stereotype.Component;/*** AOP definition in annotated form*/@Aspect//the label declares the Loggeraspect class as a slice@Component//This tag puts the Loggeraspect class in the IOC container Public classlogaopaction {/*** Execution: The connection point used to match the execution method * A: @Pointcut ("Execution (* com.aijava.springcode.service). *.*(..))") * The first * means matching any of the method return values: (Two dots) represents 0 or more, the first of the above: Represents a service package and its child packages, the second * represents all classes, the third represents all methods, and the second: Indicates the number of arguments of the method * B: @Pointcut ("Within (com.aijava.springcode.service.*)") * Within the connection point of the qualifying match method, above which represents any connection point under the matching service package * C: @Pointcut ("This (com.aijava.springcode.service.UserService)") * This is used to qualify an AOP proxy to be an instance of a specified type, as above, specifying a specific instance, Is UserService * D: @Pointcut ("Bean (UserService)") * beans are also very common, beans can specify the name of the bean in the IOC container * @within (Org.springfram Ework.stereotype.Service), block all methods of the class with @Service annotations * @annotation ( org.springframework.web.bind.annotation.RequestMapping), interception of annotated methods with @rquestmapping*/@Pointcut ("Execution (* com.cn.xxx). *.*(..))")//Match all methods under the specified package path Private voidPointCut () {}/*** method to start execution*/@Before ("PointCut ()") Public voidDobefore (Joinpoint point) {Class Clazz=point.gettarget (). GetClass (); String TargetName=Clazz.getsimplename (); String MethodName=point.getsignature (). GetName (); Logger Logger=Logger.getlogger (Clazz); Object[] args=Point.getargs (); StringBuilder Formatlog=NewStringBuilder ("Enter Method" + clazz.getpackage (). GetName () + TargetName + "." + MethodName + ", parameter is:"); for(Object Ignored:args) {formatlog.append ("{},"); } logger.info (Formatlog.tostring ()+args); }/*** Method ends the action after execution*/@AfterReturning (pointcut= "PointCut ()",returning = "ReturnVal") Public voidDoafter (joinpoint point, Object returnval) {Class clazz=point.gettarget (). GetClass (); String TargetName=Clazz.getsimplename (); String MethodName=point.getsignature (). GetName (); String logstring= "Exit Method:" + TargetName + "." + MethodName + ", the return result is:" +ReturnVal; Logger Logger=Logger.getlogger (Clazz); Logger.info (logstring); } /*** Method has an exception when the Operation*/@AfterThrowing (pointcut= "PointCut ()",throwing = "Error") Public voidDoafterthrow (joinpoint point, throwable error) {Class clazz=point.gettarget (). GetClass (); String TargetName=Clazz.getsimplename (); String MethodName=point.getsignature (). GetName (); Logger Logger=Logger.getlogger (Clazz); String logstring= "Exception occurred in method:" + TargetName + "." + MethodName + ", exception is:"; Logger.error (logstring, error); } /*** AOP Wrapping Method Execution *@paramPJP *@return * @throwsThrowable*/@Around ("PointCut ()") Public voidAround (Proceedingjoinpoint proceedingjoinpoint)throwsThrowable {Try{proceedingjoinpoint.proceed (); }Catch(Exception e) {e.tostring (); } }}
Note: The red part must correspond if the wrong should be reported illegalargumentexception:error at:: 0 formal unbound in pointcut error, this error I have been troubleshooting for a long time found that the project has been started wrong, Remember that the parameters must correspond.
SPIRNGMVC AOP Configuring facets and Illegalargumentexception:error with annotations: 0 formal unbound in pointcut anomaly analysis