29th-Spring AOP_AspectJ @ annotation

Source: Internet
Author: User

On September 16, June 14, it rained heavily. "The Four Sides are surrounded by ten LI He, where the most flowers to the cloud, the painting building on the south side of the sunset and. At first glance, the weather is cool and lonely. Time must be spent in wine, and you can hear the song in the flowers ."

The Aspect-Oriented Framework AspectJ encounters Spring, which not only creates a conciseness, but also brings more choices.

The aspect and proxy configuration methods in the previous article are cumbersome and cumbersome. Fortunately, Spring has been integrated with AspectJ, and the interface and configuration have been exhausted.

Next we will rewrite the AOP example of Spring in the previous article to achieve the same effect.

Steps 1-3 remain unchanged.
4. Define an Aspect partition class BallHandler. java

Package edu. eurasia. aop; import org. apache. log4j. logger; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. pointcut; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. afterReturning; @ Aspectpublic class BallHandler {static Logger logger = Logger. getLogger (TestBall. class); @ Pointcut ("execution (* edu. eurasia. aop. watchBallImpl. watchfoot *(..)) ") publi C void watchfootball () {}; @ Pointcut ("execution (* edu. eurasia. aop. watchBallImpl. watchbasket *(..)) ") public void watchbasketball () {}; @ Before (value =" watchfootball () & args (city ,..) ") public void watchBefore (String city) {logger. debug ("before watching the ball, go to the" + city + "cafe to have a cup of Brazilian coffee... ") ;}@ AfterReturning (" watchbasketball () & args (city ,..) ") public void watchAfter (String city) {logger. debug ("go to" + city + "McDonald's to eat hamburgers ! ");}}

@ Aspect annotation marked above the class, which indicates that this class is an Aspect (actually Advisor)-cut class. This class does not need to implement any interface, but only needs to define a method (the method name does not matter ).

@ Pointcut this method can be used to declare the edu. eurasia. aop. WatchBallImpl. watchfoot * (...) method as poincut, that is, the entry point. When the watchfoot * (...) method is called, The watchfootball () method is executed. Execution is the starting point for matching method execution, that is, the most common starting point definition method in spring.

@ Before (value = "watchfootball () & args (city ,..)): @ Before: the statement is executed Before the start point method is executed, but the start point is not directly declared, but value = "watchfootball ()", this is because if @ afterReturning and so on are all changed, we should replace them with the watchfootball of Pointcut. In this case, only one change is required. Other @ AfterReturning classes are the same. & Args (city,...) can obtain the parameter of the entry point method watchfoot.

The following analyzes the tangent expression:

Execution (* edu. eurasia. aop. WatchBallImpl. watchfoot *(..))

Execution (): indicates the interception method. Rules to be matched can be defined in brackets.

The first "*" indicates that the return value of the method is arbitrary.

The second "*" indicates matching all methods in this class.

(...): Indicates that the parameters of the method are arbitrary.

5. Compile the spring configuration file applicationContext. xml.

 
     
          
        
          
           
          
 
6. Compile TestBall. java
Package edu. eurasia. aop; import org. apache. log4j. logger; import org. junit. test; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class TestBall {static Logger logger = Logger. getLogger (TestBall. class); @ Testpublic void testball () {logger. debug ("test begin"); ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); WatchBall watchball = (WatchBall) ctx. getBean ("watchBall"); watchball. watchfootball ("Rio de Janeiro"); logger. debug ("----------------------------"); watchball. watchbasketball ("Texas ");}}
7. Running result and environment Configuration

The running result is as follows:

DEBUG [main] (BallHandler. java: 21)-before watching the ball, go to the cafe Rio de Janeiro to have a cup of Brazilian coffee...
DEBUG [main] (WatchBallImpl. java: 11)-go to Rio de Janeiro to watch the 2014 Brazil World Cup
DEBUG [main] (TestBall. java: 25 )-----------------------------
DEBUG [main] (WatchBallImpl. java: 16)-go to Texas to watch the NBA Finals

DEBUG [main] (BallHandler. java: 26)-go to McDonald's in Texas to have a hamburger after watching the ball!

This example uses spring 2.5.6, in addition to finding out spring. jar, commons-logging-1.1.1.jar two jar packages, plus a log4j. jar. Download the aspectj-1.7.0.jar and unzip it with four JAR: aspectjrt. jar, aspectjtools. jar, aspectjweaver. jar, and org. aspectj. matcher. jar. You also need to download a aopalliance-1.0.jar.

The directory structure is as follows:

8. configuration-based AspectJ

In addition to using the @ Aspect annotation to define the partition class, Spring AOP also provides a configuration-based method to define the partition class.

(1) modify the configuration file applicationContext. xml as follows:

Xml version = "1.0" encoding = "UTF-8"?>
 
  
  
  
  
 

(String ,..) the declared entry point must contain at least one String type parameter. It can obviously match watchfoot * (String city,...) in WatchBallImpl ,..); args (n ,..) declare it to watchfoot * (String city ,..) the first parameter starts with the alias "n" passed to Advice. If the arg-names in is not "n", an exception is thrown.

(2) BallHandler. java code does not change much:

Package edu. eurasia. aop; import org. apache. log4j. logger; public class BallHandler {static Logger logger = Logger. getLogger (TestBall. class); public void watchBefore (String city) {logger. debug ("before watching the ball, go to the" + city + "cafe to have a cup of Brazilian coffee... ");} public void watchAfter (String city) {logger. debug ("go to" + city + "McDonald's to eat hamburgers! ");}}
(3) The remaining code and configuration remain unchanged, and the running result is the same.




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.