JavaEE ------ ApectJ aspect technology === tag

Source: Internet
Author: User

JavaEE ------ ApectJ aspect technology === tag

Currently, one of the most popular aop technologies is ========Tag

Implementation steps: 1. Import the aop label

Method to open the aop package.

This contains

Then, select the spring version. Configure in the configuration file

As follows:

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 ">

In this way, the aop label is imported.

2. Configure cut points and notifications
<aop:config><aop:aspect ref="myadvior"><!--{cke_protected}{C}%3C!%2D%2D%20%E9%9C%80%E8%A6%81%E5%AF%BC%E5%85%A5%E4%BD%9C%E4%B8%BA%E5%88%87%E9%9D%A2%E7%9A%84%E7%B1%BB%2D%2D%3E--><aop:pointcut expression="execution(* cn..Person.*(..))" id="cut"><aop:before method="test1" pointcut-ref="cut"> <!--{cke_protected}{C}%3C!%2D%2D%20%E8%BF%99%E6%98%AF%E9%80%9A%E7%9F%A5%EF%BC%8C%E6%8B%A6%E6%88%AA%E5%88%87%E7%82%B9%E4%BD%8D%E7%BD%AE%EF%BC%8C%E4%B9%9F%E5%B0%B1%E6%98%AF%E6%8B%A6%E6%88%AA%E6%97%B6%20%20%E9%9C%80%E8%A6%81%E5%81%9A%E7%9A%84%E4%BA%8B%E6%83%85%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%8B%A6%E6%88%AA%E6%A0%B8%E5%BF%83%E4%B9%8B%E5%90%8E%E6%89%A7%E8%A1%8C%E7%9A%84%E5%8A%A8%E4%BD%9C%EF%BC%8C%E5%85%A8%E9%83%A8%E5%86%99%E5%9C%A8test1%E6%96%B9%E6%B3%95%E9%87%8C%E9%9D%A2%E4%BA%86%20%2D%2D%3E--><!--{cke_protected}{C}%3C!%2D%2D%20%3Caop%3Abefore%20method%3D%22test1%22%20pointcut%3D%22execution(*%20cn..Person.*(..))%22%2F%3E%20%E8%BF%99%E6%A0%B7%E4%B9%9F%E6%98%AF%E5%8F%AF%E4%BB%A5%E7%9A%84%EF%BC%8C%E5%B0%B1%E4%B8%8D%E7%94%A8%E5%88%87%E7%82%B9%E4%BA%86%EF%BC%8C%E7%9B%B4%E6%8E%A5%E5%86%99%E5%9C%A8%E8%BF%99%E9%87%8C%E9%9D%A2%20%2D%2D%3E--></aop:before></aop:pointcut></aop:aspect></aop:config>
There are many configuration notification types

 

Iii. proxy class and automatic annotation class
<span style="font-size:18px;"><!--{cke_protected}{C}%3C!%2D%2D%20%E9%9C%80%E8%A6%81%E7%9A%84%E4%B8%89%E5%85%83%E7%B4%A0%20%EF%BC%88%E8%A2%AB%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%8C%20%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%8C%E5%88%87%E9%9D%A2%EF%BC%89%E9%87%87%E7%94%A8%E6%A0%87%E7%AD%BE%EF%BC%8C%E5%88%87%E9%9D%A2%E5%92%8C%E4%B9%8B%E5%89%8D%E7%9A%84%E6%9C%89%E7%82%B9%E4%B8%8D%E5%90%8C%EF%BC%8C%E5%8F%AA%E6%98%AF%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84pojo%E5%AF%BC%E5%85%A5%20%2D%2D%3E--><bean id="person" class="cn.aop.aspectj3.Person"></bean></span>
<span style="font-size:18px;">&nbsp;<!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E8%87%AA%E5%8A%A8%E6%A0%87%E7%AD%BE%EF%BC%8C%E8%87%AA%E5%8A%A8%E5%8E%BB%E6%9F%A5%E6%89%BE%E5%B8%A6%E6%9C%89%E6%B3%A8%E8%A7%A3%E7%9A%84%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%20%2D%2D%3E--><span style="white-space:pre"></span><aop:aspectj-autoproxy></aop:aspectj-autoproxy></span>
4. Import the class we use as the cut surface
  
 

The configuration file has been completed.

 

5. The imported class as the cut surface

 

Public class MyAdvisor {public void test1 () {System. out. println ("this is test ...");}}
The method name is in the configuration section, and the notification time is complete .. Here you can implement the action you want to complete.

The tag is equivalent to the previous one and has been greatly optimized. For example, if the core module does not know that it is intercepted, decoupling is further implemented.

============================================= ==========

Source code and Testing

1. Configuration File

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%2D%2D%3E--><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"><!--{cke_protected}{C}%3C!%2D%2D%20%E9%9C%80%E8%A6%81%E7%9A%84%E4%B8%89%E5%85%83%E7%B4%A0%20%EF%BC%88%E8%A2%AB%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%8C%20%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%8C%E5%88%87%E9%9D%A2%EF%BC%89%E9%87%87%E7%94%A8%E6%A0%87%E7%AD%BE%EF%BC%8C%E5%88%87%E9%9D%A2%E5%92%8C%E4%B9%8B%E5%89%8D%E7%9A%84%E6%9C%89%E7%82%B9%E4%B8%8D%E5%90%8C%EF%BC%8C%E5%8F%AA%E6%98%AF%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84pojo%E5%AF%BC%E5%85%A5%20%2D%2D%3E--><bean id="person" class="cn.aop.aspectj3.Person"></bean><!--{cke_protected}{C}%3C!%2D%2D%20%E8%87%AA%E5%8A%A8%E4%BB%A3%E7%90%86%E6%B3%A8%E8%A7%A3%0A%09%3Cbean%20class%3D%22org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator%22%3E%3C%2Fbean%3E%0A%09%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E8%87%AA%E5%8A%A8%E6%A0%87%E7%AD%BE%EF%BC%8C%E8%87%AA%E5%8A%A8%E5%8E%BB%E6%9F%A5%E6%89%BE%E5%B8%A6%E6%9C%89%E6%B3%A8%E8%A7%A3%E7%9A%84%E7%B1%BB%E5%92%8C%E6%96%B9%E6%B3%95%20%2D%2D%3E--> <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"> <!--{cke_protected}{C}%3C!%2D%2D%20%E8%BF%99%E6%98%AF%E9%80%9A%E7%9F%A5%EF%BC%8C%E6%8B%A6%E6%88%AA%E5%88%87%E7%82%B9%E4%BD%8D%E7%BD%AE%EF%BC%8C%E4%B9%9F%E5%B0%B1%E6%98%AF%E6%8B%A6%E6%88%AA%E6%97%B6%20%20%E9%9C%80%E8%A6%81%E5%81%9A%E7%9A%84%E4%BA%8B%E6%83%85%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%8B%A6%E6%88%AA%E6%A0%B8%E5%BF%83%E4%B9%8B%E5%90%8E%E6%89%A7%E8%A1%8C%E7%9A%84%E5%8A%A8%E4%BD%9C%EF%BC%8C%E5%85%A8%E9%83%A8%E5%86%99%E5%9C%A8test1%E6%96%B9%E6%B3%95%E9%87%8C%E9%9D%A2%E4%BA%86%20%2D%2D%3E--><!--{cke_protected}{C}%3C!%2D%2D%20%3Caop%3Abefore%20method%3D%22test1%22%20pointcut%3D%22execution(*%20cn..Person.*(..))%22%2F%3E%20%E8%BF%99%E6%A0%B7%E4%B9%9F%E6%98%AF%E5%8F%AF%E4%BB%A5%E7%9A%84%EF%BC%8C%E5%B0%B1%E4%B8%8D%E7%94%A8%E5%88%87%E7%82%B9%E4%BA%86%EF%BC%8C%E7%9B%B4%E6%8E%A5%E5%86%99%E5%9C%A8%E8%BF%99%E9%87%8C%E9%9D%A2%20%2D%2D%3E--></aop:before></aop:pointcut></aop:aspect></aop:config><bean class="cn.aop.aspectj3.MyAdvisor" id="myadvior"><!--{cke_protected}{C}%3C!%2D%2D%E5%BD%93%E5%81%9A%E5%88%87%E9%9D%A2%E7%9A%84pojo%20%E6%B3%A8%E5%85%A5%20%2D%2D%3E--></bean></beans>

2, as the cut surface class

Package cn. aop. aspectj3; public class MyAdvisor {public void test1 () {System. out. println ("this is test ...");}}
3. Proxy object
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();}

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.