Java EE-------The application of ASPECTJ in spring

Source: Internet
Author: User

AspectJ can be used and the whole project needs.

Things like database processing, etc.

Demo instances are mainly:

In the same way as before, the Java manual writing method is used first.

Slice technology = notification + Tangent point

<span style= "FONT-SIZE:18PX;" > @Testpublic void Test1 () {//1, get agent factory proxyfactory factory = new Proxyfactory (new Person ());//2, Tangent aspectjexpressionpointcut cut = new Aspectjexpressionpointcut ();//The following is mainly about ASPECTJ tangent syntax, if the <bean> Configured in XML and using automatic proxies, all beans are cut//cut.setexpression ("Execution (void Cn.hncu.aop.Person.run ())") according to the following matching principle;// Specify BLOCK: The null parameter in the person class run () method//cut.setexpression ("Execution (* cn.hncu.aop.person.*n ())");//Block: Method name ends with "n" and Null parameter method// Cut.setexpression ("Execution (void cn.hncu.aop.person.* ())");//Block: A method//cut.setexpression ("Execution (*) that returns a value of void and null argument cn.hncu.aop.person.* (*));//BLOCK: The number of parameters is 1 method//cut.setexpression ("Execution (* cn.hncu.aop.person.* (*,*))");// Block: The number of parameters is 2 method//cut.setexpression ("Execution (* cn.hncu.aop.person.* (*,..))"); /BLOCK: The number of parameters >=1 method//cut.setexpression ("Execution (* cn.hncu.aop.person.* (..))"); /BLOCK: Parameter arbitrary, return type any//cut.setexpression ("Execution (* cn.hncu.aop.person.* (String,..))"); /BLOCK: The first parameter must be of type string and any other parameter. Returns the type of any//cut.setexpression ("Execution (* cn.hncu). *(..) )");/ /"Cn.hnCu.. " In the ".." Refers to any directory under CN.HNCU (no matter how deep) cut.setexpression ("Execution (* CN.AOP). *(..))");/ /Notice Advice Advice =new Methodinterceptor () {@Overridepublic Object invoke (Methodinvocation inv) throws Throwable { System.out.println ("Intercept before using AspectJ method"); Object obj=inv.proceed (); SYSTEM.OUT.PRINTLN ("intercept later"); return obj;}}; Advisor Advisor =new Defaultpointcutadvisor (cut, advice); Factory.addadvisor (advisor); Person P = (person) factory.getproxy ();p. Run ();p. Say (); </span>
it contains some regular expressions.

Then the way the configuration file is used (this is just a simple test)

<bean id= "Person" class= "Cn.aop.Person" ></bean><bean id= "stud" class= "Cn.aop.Student" ></bean >    <bean class= "Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" ></ Bean><bean id= "advisor" class= "Org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor" >     <property name= "expression" value= "Execution (* cn.aop.person.* (..))"/>   <property name= "Advice" > <bean   class= "Cn.aop.demo1.Around"/>   </property></bean>

We can see a package that modifies the lower plane

<bean id= "advisor" class= "org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor" >

specifically to intercept what, the main configuration

<property name= "expression" value= "Execution (* cn.aop.person.* (..))" />

Implementation classes for interfaces (simple tests)

Package Cn.aop.demo1;import Org.aopalliance.intercept.methodinterceptor;import Org.aopalliance.intercept.methodinvocation;public class Around implements Methodinterceptor {@Overridepublic Object Invoke (Methodinvocation method) throws Throwable {System.out.println ("start intercept"); Object obj= method.proceed (); SYSTEM.OUT.PRINTLN ("Intercept returned"); return obj;}}


Test class:

@Testpublic void Test () {ApplicationContext ctx =new classpathxmlapplicationcontext ("Cn/aop/aspectj/aspectj.xml"); Person P=ctx.getbean (Person.class);p. Run ();p. Say ();


Java EE-------The application of ASPECTJ in spring

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.