Java EE------APECTJ's cutting technology = = = Label

Source: Internet
Author: User

one of the ======== of AOP technology is now more popular Label

implementation steps:One, import AOP tags

method to open the AOP package. It's inside.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "> This one's inside.


Then select the version number of spring.

Configure in config file

For example, the following:

<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

This imports the AOP tags


Two, configuring pointcuts and notifications

<aop:config><aop:aspect ref= "Myadvior" ><!--need to import classes as facets--><aop:pointcut expression= "execution (* CN.. person.* (..)) " Id= "Cut"/><aop:before method= "test1" pointcut-ref= "Cut"/> <!--This is the notification that intercepts the pointcut position. That's what you need to do when you intercept.  --><aop:aftermethod= "test1" pointcut-ref= "Cut"/> <!--the action that was run after the core was intercepted, all written in the Test1 method ><!--<aop:before method= "test1" pointcut= "Execution (* cn). person.* (..)) " /> This is also able, you do not have to cut the point, directly written in the face--></aop:aspect></aop:config>
There are very many types of configuration notifications inside

three. The proxy class and its own active annotation class

<span style= "FONT-SIZE:18PX;" ><!--the required three-dimensional (by proxy class, their own active proxy class, cut-off) label. The facets are a little different from the previous. Just a simple pojo import--><bean id= "person" class= "Cn.aop.aspectj3.Person" ></bean></span>
<span style= "FONT-SIZE:18PX;" > <!--annotate your own active tag and take the initiative to find classes and methods with annotations--><span style= "White-space:pre" ></span> <AOP: Aspectj-autoproxy></aop:aspectj-autoproxy></span>

Four. To import our classes as slices
<bean class= "Cn.aop.aspectj3.MyAdvisor" id= "Myadvior"/><!--as a slice of pojo injection--

The configuration file is complete.


Five, the class that is imported as a slice

public class Myadvisor {public void test1 () {System.out.println ("This is Test ...");}}
A very common category. The method name is in the configuration slice, and the notification time is complete. This will enable you to complete the desired action.

The label is equivalent to the previous, there is a very large optimization, such as in the core module completely do not know is all done interception, further realize the understanding of decoupling.

========================= has been introduced here ====================



Source Code and test

1, configuration file

<?

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/schema/context "xmlns: tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /SPRING-CONTEXT-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd "><!--required Ternary (by proxy class. Own active proxy class, slice) adopt label, slice and a bit different before, just a simple pojo import--><bean id= "person" class= "Cn.aop.aspectj3.Person" ></bean ><!--own active agent annotations <bean class= "Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJautoproxycreator ></bean>-<!--annotate your own active tab and take the initiative to find classes and methods with annotations--<aop:aspectj-autoproxy> </aop:aspectj-autoproxy> <aop:config><aop:aspect ref= "Myadvior" ><aop:pointcut expression= " Execution (* CN.. person.* (..)) " Id= "Cut"/><aop:before method= "test1" pointcut-ref= "Cut"/> <!--This is the notification that intercepts the pointcut position. That's what you need to do when you intercept.--><aop:aftermethod= "Test1" pointcut-ref= "Cut"/> <!--the action that was run after the core was intercepted. All written in the Test1 method inside the--><!--<aop:before method= "test1" pointcut= "Execution" (* cn). person.* (..)) " /> is also able to do so. You don't have to cut it out, just write it in here.--></aop:aspect></aop:config><bean class= "Cn.aop.aspectj3.MyAdvisor" id= " Myadvior "/><!--injected as Pojo of the slices--></beans>

2, as a class of facets

Package Cn.aop.aspectj3;public class Myadvisor {public void test1 () {System.out.println ("This is Test ...");}}
3, the object being proxied

Package Cn.aop.aspectj3;public class Person {public void say () {System.out.println ("... This is say. ");} public void Run () {System.out.println ("This is the Run method in person");}}
4, test

@Testpublic void Test2 () {ApplicationContext context =new classpathxmlapplicationcontext ("cn/aop/aspectj3/ Aspectj3.xml "); Person P =context.getbean (person.class);p. Run ();p. Say ();




Java EE------APECTJ's cutting technology = = = Label

Related Article

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.