Open Tao Spring3 (6.2)-AOP 6.2 AOP HelloWorld

Source: Internet
Author: User

6.2.1 Preparation Environment

Prepare the required JAR packages first

Org.springframework.aop-3.0.5.release.jar

Com.springsource.org.aspectj.weaver-1.6.8.release.jar

Com.springsource.org.aopalliance-1.0.0.jar

Com.springsource.net.sf.cglib-2.2.0.jar

Currently, the package in the project is

6.2.2 Defining the Target class

1) define the target interface:

 Package Lqy.springh6_2;  Public Interface Ihelloworldservice {      publicvoid  SayHello ();  }  

2) define the target interface implementation:

 Package Lqy.springh6_2;  Public class Implements Ihelloworldservice {      publicvoid  SayHello () {          System.out.println ("= = = =========hello world! " );      }  }

Note: In the daily development of the business logic is defined in a dedicated service package, and implementation is defined in the service package under the IMPL package, services interface in ixxxservice form, and service implementation is Xxxservice, this is the statute design, see the name of the meaning. Of course, you can use the company's internal better form, as long as everyone is well understood on it.

6.2.2 Defining slice support classes

        has the target class, the definition slices, the slice is the combination of the notice and the pointcut, and the facets are defined by the configuration, so this definition tangent, we need to define the slice support class, the slice support class provides the notification implementation:

 package   Lqy.springh6_2;  public  class   Helloworldaspect {  pre-notification  public  void   Beforeadvice () {System.out.println ( "===========before advice" );  //  post final notification  public  void   Afterfinallyadvice () {   SYSTEM.OUT.PRINTLN ( ===========after finally advice "); }  }

The Helloworldaspect class here is not a true facet implementation, but a class that defines the notification implementation, where we can think of it as a tangent point.

Note: The AOP-related classes are finally placed under a package, such as the "AOP" package, because AOP is dynamically woven, so if a target class is being intercepted by AOP and the notification is applied, it may be difficult to find out which package the notification is implemented in, so it is recommended to use the protocol naming, Facilitates later maintenance personnel to find the corresponding AOP implementation.

6.2.3 Configuration in XML

With notification implementations, let's configure the facets:

<?XML version= "1.0" encoding= "UTF-8"?>  <Beansxmlns= "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/SPR Ing-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/sch Ema/aop/spring-aop-3.0.xsd "  >                                  <BeanID= "HelloWorldService"class= "Lqy.springh6_2.HelloWorldService"/>  <BeanID= "aspect"class= "Lqy.springh6_2.HelloWorldAspect"/>  <Aop:config>  <Aop:pointcutID= "Pointcut"expression= "Execution (* lqy.springh6_2..*.* (..))"/>      <Aop:aspectref= "aspect">          <Aop:beforePointcut-ref= "Pointcut"Method= "Beforeadvice"/>          <Aop:afterpointcut= "Execution (* lqy.springh6_2..*.* (..))"Method= "Afterfinallyadvice"/>      </Aop:aspect>  </Aop:config> </Beans>  

The pointcut uses the <aop:pointcut> configuration under the <aop:config> tab, the expression attribute is used to define the pointcut pattern, and the default is the ASPECTJ syntax, "Execution (* Lqy.springh6_2..*.* (..))" Represents the matching lqy.springh6_2 package and any method execution under the child package.

The slice uses the <aop:aspect> tag configuration under the <aop:config> tab, where "ref" is used to refer to the method of the slice support class.

The pre-notification is defined using the <aop:before> tag under the <aop:aspect> tab, and the Pointcut-ref property is used to refer to the pointcut bean, and method is used to refer to the methods in the implementation class of the slice notification implementation , which is the method that is called before the target class method executes.

The final notification is defined using the <aop:after > tag under the <aop:aspect> tab, and in addition to using the Pointcut-ref property to refer to an existing pointcut, you can also use the Pointcut property to define, as pointcut= "Execution (* lqy.springh6_2..*.* (..))", the Method property is also a specified notification implementation that is called after the target class method executes.

6.2.4 Running tests

The test class is very simple, and invoking a proxy bean is exactly the same as calling a normal bean, and Spring AOP creates an AOP proxy for the target object, with the following test code:

 PackageLqy.springh6_2;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest1 {/**     * @paramargs*/     Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Lqy/springh6_2/springh6_2.xml"); Ihelloworldservice HelloWorldService= Ctx.getbean ("HelloWorldService", Ihelloworldservice.class);      Helloworldservice.sayhello (); }}
the test will output the following:

Pan 1: Error occurred in

Originally:

Execution (*lqy.springh6_2..*.* (..))

Execution (* lqy.springh6_2..*.* (..))

Take a closer look

* The space between lqy can not be less.

from the output we can see that the pre-notification is allowed before the connection point (method) selected by the Pointcut, and the latter notification will be executed after the connection point (method), and the AOP agent is generated, and the execution procedure 6-4 is shown.

Open Tao Spring3 (6.2)-AOP 6.2 AOP HelloWorld

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.