Spring First (1): Making Java Development easier (bottom)

Source: Internet
Author: User

Apply facets (aspects)

  Di can keep your software components loosely coupled, while aspect-oriented programming (AOP) enables you to capture the functionality of components that can be reused throughout your application. In software systems, AOP is often defined as a technique for promoting separation of concerns. The system consists of a number of components, each of which is responsible for part of the functionality. But these components often have additional responsibilities in addition to the core functionality. such as log, transaction management and security, these system services are introduced into the component. These services are often referred to as crosscutting concerns (Cross-cutting-concerns) because they often run through multiple components.

Propagate these concepts across multiple components and you'll introduce two complex layers into your code

    • Implementing system-level concept code will recur across multiple components, meaning that if you need to change these concepts, you must modify the contents of multiple components. Even if you abstract these concepts into a separate method to make it a single method, the invocation of the method will appear in many places.
    • Your component is now flooded with functionality that is not related to core code. A method that adds its entry to the Address book should only care about how to add rather than security and transactional issues

Show the complexity of it. The business object on the left and the system service on the right are closely related.

AOP enables these services to be modularized and declared using them in the component to eliminate these effects. This makes the components more cohesive, so that they focus only on their own specific concepts and completely ignore any system services involved. In short, facets ensure that pojos remain simple.

You can envision facets as blankets that cover many components, as shown in. An application consists of several modules that implement business functions. With AOP, you can use the functional layer to cover your core applications. These layers can be declared to penetrate into your application without your core code knowing that they exist. This is a powerful concept because it will be independent of the application of core business and system services.

Let's look at a specific example, now is a knight's heroic story, how the story passed down, of course, through the singer's song to pass

 public  class   Minstrel {  PrintStream stream;  public   Minstrel (PrintStream stream) {
    this . Stream = stream;  public  void   Singbeforequest () {stream.println ( "Fa la La, the knight is so brave!"    );  public  void   Singafterquest () {stream.println ( "Tee hee hee, the Brave Knight" + "did embark on a quest!"    ); }}

As you can see, Minstrel is a simple class and has two methods. The Singbeforequest () method is called before the knight starts exploring, and the Singafterquest () method should be called after the Knight Quest is complete. In these two use cases, minstrel the knight's deeds by injecting Printsteam into the constructor.

Now all you have to do is bring these people into your code----you just need to inject braveknight, let's fine-tune the braveknight to use minstrel. The following shows how to merge them together

 

 Public classBraveknightImplementsKnight {PrivateQuest Quest; PrivateMinstrel Minstrel;  Publicbraveknight (Quest Quest, Minstrel Minstrel) { This. Quest =Quest;  This. Minstrel =Minstrel; }     Public voidEmbarkonquest ()throwsquestexception {minstrel.singbeforequest ();        Quest.embark ();    Minstrel.singafterquest (); }}    

It should be able to run, all you have to do is go back to your config file and then declare a minstrel bean and inject it into the Braveknight constructor. But wait ...

  

Spring First (1): Making Java Development easier (bottom)

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.