[Spring Study Notes] [6.2 HelloWorld of AOP]

Source: Internet
Author: User

6.2.1 prepare the environment

First, release the jar package, go to spring-framework-3.0.5.release-dependencies.zip and find the following jar package in the spring-framework-3.0.5.RELEASE-with-docs:




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











Add these jar packages to "Build Path.

6.2.2 define the target class

1) define the target interface:


Java code:

package cn.javass.spring.chapter6.service; public interface IHelloWorldService {     public void sayHello(); }

2) define the target interface implementation:


Java code:

package cn.javass.spring.chapter6.service.impl; import cn.javass.spring.chapter6.service.IHelloWorldService; public class HelloWorldService implements IHelloWorldService {     @Override     public void sayHello() {         System.out.println("============Hello World!");     } } 

Note: In daily development, the business logic is finally defined under a dedicated service package, while the implementation is defined in the impl package under the service package. The service interface is in the form of IXXXService, the service implementation is XXXService, which is the design of the Protocol. Of course, you can use a better internal form within the company, as long as everyone understands it.

6.2.2 define the aspect support Class

With the target class, this definition section is the combination of the notification and the starting point, and the section is defined by the configuration method. Therefore, before this definition section, we need to define the aspect Support class, the aspect Support class provides notification implementation:


Java code:
Package cn. javass. spring. chapter6.aop; public class HelloWorldAspect {// public void beforeAdvice () {System. out. println ("============= before advice");} // The final public void afterFinallyAdvice () {System. out. println ("=========== after finally advice ");}}

Here, the HelloWorldAspect class is not a real aspect implementation, but defines the notification implementation class. Here we can regard it as a lack of the starting point.


NOTE: For the AOP-related classes, they are finally placed under a specific package, such as the "aop" package. Because AOP is dynamically woven, if a target class is intercepted by AOP and a notification is applied, it may be difficult to find out which package the notification is implemented. Therefore, we recommend that you use the naming conventions to facilitate later maintenance personnel to find the corresponding AOP implementation.

6.2.3 configure in XML

With the notification implementation, let's configure the aspect:

1) configure the AOP namespace. The configuration header is as follows:


Java code:
<?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-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> </beans>

2) configure the target class:


Java code:
<bean id="helloWorldService" class="cn.javass.spring.chapter6.service.impl.HelloWorldService"/>

3) configuration aspect:


Java code:
<bean id="aspect" class="cn.javass.spring.chapter6.aop.HelloWorldAspect"/> <aop:config> <aop:pointcut id="pointcut" expression="execution(* cn.javass..*.*(..))"/>     <aop:aspect ref="aspect">         <aop:before pointcut-ref="pointcut" method="beforeAdvice"/>         <aop:after pointcut="execution(* cn.javass..*.*(..))" method="afterFinallyAdvice"/>     </aop:aspect> </aop:config>

The entry point uses the <aop: pointcut> Configuration Under the <aop: config> label. The expression attribute is used to define the entry point mode. The default is the AspectJ syntax, "execution (* cn. javass .. *. *(..)) "indicates matching cn. the S s package and any method under the sub-package.


The section uses the <aop: aspect> label configuration under the <aop: config> label. "ref" is used to reference the method of the Section Support class.


Pre-notifications are defined using the <aop: before> label under the <aop: aspect> label. The pointcut-ref attribute is used to reference the entry-point Bean, and the method is used to reference methods in the implementation class of the section notification, this method is the notification implementation, that is, the method called before the target class method is executed.


The final notification is defined using the <aop: after> label under the <aop: aspect> label. Besides using the pointcut-ref attribute to reference an existing entry point, you can also use the pointcut attribute, for example, pointcut = "execution (* cn. javass .. *. *(..)) ", the method attribute also specifies the notification implementation, that is, the method called after the target class method is executed.

6.2.4 run the test

The test class is very simple. Calling the proxy Bean is exactly the same as calling a common Bean. Spring AOP creates an AOP proxy for the target object. The specific test code is as follows:


Java code:
package cn.javass.spring.chapter6; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.javass.spring.chapter6.service.IHelloWorldService; import cn.javass.spring.chapter6.service.IPayService; public class AopTest {     @Test     public void testHelloworld() {         ApplicationContext ctx =  new ClassPathXmlApplicationContext("chapter6/helloworld.xml");         IHelloWorldService helloworldService =         ctx.getBean("helloWorldService", IHelloWorldService.class);         helloworldService.sayHello();     } }

The test will output the following content:


Java code:
===========before advice ============Hello World! ===========after finally advice

From the output, we can see that the pre-notification is allowed before the connection point method selected by the start point, and the post-notification will be executed after the connection point method), as shown in the specific process of generating the AOP proxy and executing it 6-4.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613343317-0.JPG "/>

Figure 6-4 process of generating an AOP proxy using the Spring AOP framework


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1285950

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.