Dependency Injection and AOP Brief (12)--Dependency Injection object's behavior enhancement (AOP).

Source: Internet
Author: User

Iv. behavior Enhancement for Dependency Injection objects (AOP)

As mentioned earlier, the most striking feature of the dependency injection framework is the ability to provide container-managed dependencies and to provide behavior enhancement (AOP) functionality to objects, so this chapter discusses the topic of AOP.

1. Behavior Enhancement for dependent objects

The so-called AOP, is aspect oriented programming (aspect-oriented programming), the core idea is to put an "aspect" independent, so as to achieve loose coupling between components. It may be obscure, so let's look at a simple example.

In our reliance on banks, suppose there is a need to output log information before and after each withdrawal transaction. So we need to modify our code in this way:

public class BANKICBC implements Bank {

     private static Logger Logger = Logger. GetLogger (Bank. class . GetName ());

 

    @Override

    public Cash Withdraw (depositbook depositbook, BigDecimal amount) {

        logger . Log (level. INFO , "withdraws starting ...");

       //...

        logger . Log (level. INFO , "withdraws ended ...");

   }

}

 


As you can see, we need to add code to define logger, output logs, and so on, in order to achieve this requirement. And this code is what we call the "aspect" of independence. Why do you say that? Because the output of the log is not related to what the developer really wants to do with the withdrawal business. Assuming, if we need to do the same log processing in many other places, and such processing is completely independent of those local logic, then we need to open all the code page, regardless of the situation of CTRL + C and CTRL + V, this undoubtedly to the development and maintenance of a lot of trouble.

As a result of AOP, the "aspect" of this independent log processing can be separated from the actual dependent object, and when the dependent object is running, this "aspect" can be added to the dependent object to run, that is, the behavior of the dependent object is enhanced, Because its behavior not only realizes its own logic, but also realizes the other "aspect" logic which is enhanced. In an AOP system, the components used to enhance the logic of other "aspects" to an object are often referred to as interceptor (interceptors).

Spring, seam, and guice all provide the appropriate interceptor definition, since seam is based on the definition of the original annotation pattern, which is slightly inconvenient for developers to use, so let's take spring as an example to briefly describe how to apply the "log processing aspect" to our program.

First, we separate the "log processing aspect" as a separate class, which is the "aspect" of being independent.


Public class Loginterceptor {

private static Logger Logger = Logger. GetLogger (Loginterceptor. class. GetName ());

Public Object log (Proceedingjoinpoint call) throws Throwable {

logger. Log (level. INFO, "withdraws starting ...");

Try {

return call.proceed ();

} finally {

logger. Log (level. INFO, "withdraws ended ...");

}

}

}


Spring provides many kinds of interceptor assembly methods, where we use XML configuration to enhance this "aspect" to our bank dependent objects:

<beans>

<!--......-->

<!--the "facet" class is declared here as a dependency of spring management--

<bean id="Logger" class="Tutorial.di.ch01.LogInterceptor"/>

<!--here to enhance the "aspect" of the declaration to where it is needed--

<aop:config>

<aop:aspect ref="Logger">

<aop:pointcut id="Pointcuts.withdrawmethod"

expression="Execution (* tutorial.di.ch01.BankICBC.withDraw (..))" />

<aop:around pointcut-ref="Pointcuts.withdrawmethod" method="log"/>

</aop:aspect>

</aop:config>

</beans>


Since then the original BANKICBC class will not need to write any more about the log output code, you can import the function. Conversely, if you want to remove this demand, also do not need to change the BANKICBC class, only the interceptor configuration is deleted, which greatly reduces the program logic and other "aspects" of the coupling degree.

Note that the underlying implementation of all AOP features is implemented by the dynamic agent mechanism of Java, often based on the JDK's own proxy class, or javassist, cglib tools, and so on, so the object of AOP cannot be a private method, a static method, or a final method.

Dependency Injection and AOP Brief (12)--Dependency Injection object's behavior enhancement (AOP).

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.