Exception
In the use of the time, encountered a partial exception, I use the latest Spring version, Spring-4.2.5 version of the first to ensure that your configuration file introduced in the following Red section.
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " <span style=" color: #ff0000; " > xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" </span> xsi:schemalocation= "http +/ Www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd<span style= "COLOR: #ff0000;" >http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd "< /span>>
Then there are several packages that need to be imported:
1. Aspectjrt.jar
2. Aspectjweaver.jar
3. Aopalliance-1.0.jar
When I was using it, the exception information was caused by the absence of a third jar package:
Warning: Exception encountered during context initialization-cancelling refresh attempt: Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Org.springframework.aop.config.internalAutoProxyCreator ': Instantiation of Bean failed; Nested exception is org.springframework.beans.BeanInstantiationException:Failed to instantiate [ Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; Nested exception is java.lang.noclassdeffounderror:org/aopalliance/intercept/methodinterceptorexception in thread " Main "Org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' Org.springframework.aop.config.internalAutoProxyCreator ': Instantiation of Bean failed; Nested exception is org.springframework.beans.BeanInstantiationException:Failed to instantiate [ Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; Nested exception isJava.lang.noclassdeffounderror:org/aopalliance/intercept/methodinterceptorat Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean ( abstractautowirecapablebeanfactory.java:1105) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance ( abstractautowirecapablebeanfactory.java:1050) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( abstractautowirecapablebeanfactory.java:510) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:482) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:306) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) at Org.springframework.beans.factOry.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:202) at Org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors ( postprocessorregistrationdelegate.java:228) at Org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors ( abstractapplicationcontext.java:687) at Org.springframework.context.support.AbstractApplicationContext.refresh ( abstractapplicationcontext.java:523) at org.springframework.context.support.classpathxmlapplicationcontext.< Init> (classpathxmlapplicationcontext.java:139) at Org.springframework.context.support.classpathxmlapplicationcontext.<init> ( classpathxmlapplicationcontext.java:83) at Com.siti.spring20160315aop.MainTest.main (Maintest.java:9) caused by: Org.springframework.beans.BeanInstantiationException:Failed to instantiate [ Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; Nested exception is Java.lang.NoClassDefFouNderror:org/aopalliance/intercept/methodinterceptorat Org.springframework.beans.BeanUtils.instantiateClass ( beanutils.java:163) at Org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate ( simpleinstantiationstrategy.java:89) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean ( abstractautowirecapablebeanfactory.java:1098) ... More
Start with ASPECTJ for slice configuration
AspectJ Annotation method for slicing configuration
Define four interfaces, let the Wangyang class implement the person's interface, and implement the method.
Package Com.siti.spring20160315aop;public interface Person {void say (); void Sayname (String name); String Saysth (string name); void Sayex ();}
Where saysth this method set the return value, this place will be afterreturning weaving, and then Sayex method in order to test the intentional use of NULL pointer exception, test afterthrowing will not be woven into.
Package Com.siti.spring20160315aop;public class WangYang implements person{@Overridepublic void Say () { System.out.println ("wy!");} @Overridepublic void Sayname (String name) {System.out.println ("name-->" + name);} @Overridepublic string Saysth (string name) {System.out.println ("name-->" + name); return name; @Overridepublic void Sayex () {Object obj = null;obj.equals ("1");}}
aspect-oriented programming ASPECTJ implementation, the main way is as follows, where after notification is mainly used to release resources, afterreturning notifications can make changes to the return value, afterthrowing notification can be exception capture to generate exception records, Before this notification can be used to verify the parameters.
Package Com.siti.spring20160315aop;import Java.util.arrays;import Org.aspectj.lang.joinpoint;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; @Aspectpublic class Myinterceptor { @Pointcut ("Execution (* com.siti.spring20160315aop.wangyang.* (..))") public void Anymethod () {} @Before ("Anymethod ()") public void Checkmessage (Joinpoint joinpoint) {System.out.println ("@ Before-check!!! "); System.out.println ("@Before-target object:" + Joinpoint.gettarget ()); System.out.println ("@Before-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@Before-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} @AfterReturning (pointcut = "Anymethod ()", returning = "obj") public void Checkreturn (Joinpoint joinpoint, OBject obj) {System.out.println ("@AfterReturning-target method return value:" + obj); System.out.println ("@AfterReturning-Generate log"); System.out.println ("@AfterReturning-target object:" + Joinpoint.gettarget ()); System.out.println ("@AfterReturning-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@AfterReturning-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} @After ("Anymethod ()") public void Checkafter (Joinpoint joinpoint) {System.out.println ("@After-Frees resources! "); System.out.println ("@After-target object:" + Joinpoint.gettarget ()); System.out.println ("@After-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@After-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} @Around ("Anymethod ()") Public Object Checkaround (Proceedingjoinpoint joinpoint) throws Throwable{system.out.println ( "@Around! "); object[] args = Joinpoint.getargs (); if (args! = null && args.length > 0 && args[0].getclass () = = Stri Ng.class) {Args[0] = "@Around" + args[0];} Object result = Joinpoint.proceed (args); if (result! = null && Result.getclass () = = String.class) {result = result + "--wy";} return result;} @AfterThrowing (throwing = "Ex", Pointcut = "Anymethod ()") public void checkafterthrowing (Throwable ex) { System.out.println ("@AfterThrowing-Exception thrown! "); System.out.println ("@AfterThrowing-Exception Log! ");}}
The configuration file needs to be "registered" in the configuration file by the class of the slice that we wrote ourselves.
<?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 " xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd "><aop:aspectj-autoproxy/><bean id =" Inter "class =" Com.siti.spring20160315aop.MyInterceptor "></bean><bean id =" WY "class =" Com.siti.spring20160315aop.WangYang "></bean></beans>
Test:
Package Com.siti.spring20160315aop;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Maintest {public static void main ( String[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext20160315.xml"); Person WY = Context.getbean ("WY", Person.class); Wy.say (); Wy.sayname ("wangyang-"); String str = wy.saysth ("hello--"); SYSTEM.OUT.PRINTLN ("str value:" + str);//Wy.sayex ();//The test that throws the exception}}
How XML configuration files are
Based on the configuration file configuration and the way the previous annotations are basically similar, here just will, change the place listed, do not introduce.
The configuration file is as follows:
<?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" Xsi:sche malocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd "><aop:config><!--define a common pointcut expression--><aop:pointcut expression=" Execution (* com.siti.spring20160315aopwithxml.wangyang.* (..)) " Id= "Pointcutcommon"/><!--defined facets, named Advicerespect priority is 2 Ref: Indicates who is being enhanced with--><aop:aspect id = "Advicerespect" ref = "Inter" order= "2" ><aop:before pointcut-ref= "Pointcutcommon" method= "Checkmessage"/><AOP: After-returning pointcut-ref= "Pointcutcommon" method= "Checkreturn" returning= "obj"/><aop:around pointcut-ref = "Pointcutcommon" method= "Checkaround"/><aop:after pointcut-ref= "Pointcutcommon" method= "checkafter"/></aop:aspect><aop:aspect id = "FirstExecute" ref = "inter" order = "1" ><aop:after-throwing pointcut-ref= "Pointcutcommon" method= "checkafterthrowing" throwing= "ex"/> </aop:aspect></aop:config><bean id = "Inter" class = "com.siti.spring20160315aopwithxml.MyInterceptor "></bean><bean id =" WY "class =" Com.siti.spring20160315aopwithxml.WangYang "></bean></beans >
Package Com.siti.spring20160315aopwithxml;import Java.util.arrays;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;public class Myinterceptor {public void Anymethod () {}public void Checkmessage ( Joinpoint joinpoint) {System.out.println ("@Before-check!!!"); System.out.println ("@Before-target object:" + Joinpoint.gettarget ()); System.out.println ("@Before-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@Before-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} public void Checkreturn (Joinpoint joinpoint, Object obj) {System.out.println ("@AfterReturning-target method return value:" + obj); System.out.println ("@AfterReturning-Generate log"); System.out.println ("@AfterReturning-target object:" + Joinpoint.gettarget ()); System.out.println ("@AfterReturning-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@AfterReturning-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} public void Checkafter (Joinpoint joinpoint) {System.out.println ("@After-Frees resources! "); System.out.println ("@After-target object is:" + Joinpoint.gettarget ()); System.out.println ("@After-target method:" + joinpoint.getsignature (). GetName ()); System.out.println ("@After-parameters of the target method:" + arrays.tostring (Joinpoint.getargs ()));} Public Object Checkaround (Proceedingjoinpoint joinpoint) throws Throwable{system.out.println ("@Around! "); object[] args = Joinpoint.getargs (); if (args! = null && args.length > 0 && args[0].getclass () = = Stri Ng.class) {Args[0] = "@Around" + args[0];} Object result = Joinpoint.proceed (args); if (result = = null && result.getclass () = = String.class) {result = result + "--wy";} return result;} public void checkafterthrowing (Throwable ex) {System.out.println ("@AfterThrowing-Exception thrown! "); System.out.println ("@AfterThrowing-Exception Log! ");}}
Spring Advanced Path (11)-Slice programming with ASPECTJ facet configuration and XML configuration file