5.9 two proxies for AOP in spring

Source: Internet
Author: User

 

5.9 two proxies for AOP in spring

At the beginning of this chapter, we will first start with the dynamic proxy of Java to introduce the AOP proxy of spring. In the previous example, we used the Java Dynamic proxy supported by spring, in fact, spring also supports cglib proxy.

5.9.1 Java Dynamic proxy

In the previous example, the Java Dynamic proxy supported by spring is used, that is, the proxy is an interface, and spring uses the dynamic proxy of Java by default.

5.9.2 cglib proxy

As mentioned above, spring also provides support for cglib proxy. The main change is to set the proxytargetclass attribute of proxyfactorybean and set the attribute value to true. Because you want to use the cglib proxy, you need to add the cglib-nodep-2.1_3.jar to classpath, and the following example uses the previous around notification for implementation. The procedure is as follows:

(1) Use logaround, the class responsible for outputting log information, which implements the interface methodinterceptor and overwrites the invoke () method. The sample code of logaround. Java is as follows:

// ******** Logaround. Java **************

Package com. gc. Action;

Import org. aopalliance. Intercept. methodinvocation;

Import org. aopalliance. Intercept. methodinterceptor;

Import org. Apache. log4j. level;

Import org. Apache. log4j. Logger;

// This class implements the interface methodinterceptor

Public class logaround implements methodinterceptor {

Private logger = logger. getlogger (this. getclass (). getname ());

// Code responsible for outputting log information

Public object invoke (methodinvocation mi) throws throwable {

Logger. Log (level. info, mi. getarguments () [0] + "start to review data ...");

Try {

Object result = mi. Proceed ();

Return result;

}

Finally {

Logger. Log (level. info, mi. getarguments () [0] + "audit data ends ...");

}

}

}

(2) If you modify the spring configuration file, add a property proxytargetclass to the bean whose ID is logproxy, and set the property value to true. The sample code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype beans public "-// spring // DTD bean // en"

Http://www.springframework.org/dtd/spring-beans.dtd>

<Beans>

<Bean id = "helloworld" class = "com. gc. Action. helloworld" depends-on = "date">

<Property name = "MSG">

<Value> helloworld </value>

</Property>

<Property name = "date">

<Ref bean = "date"/>

</Property>

</Bean>

<Bean id = "date" class = "Java. util. Date"/>

<! -- The following is the bean for log output using Spring AOP -->

<Bean id = "log" class = "com. gc. Action. logaop"/>

<Bean id = "timebook" class = "com. gc. Action. timebook"/>

<Bean id = "logproxy" class = "org. springframework. AOP. Framework. proxyfactorybean">

<Property name = "proxytargetclass">

<Value> true </value>

</Property>

<Property name = "target">

<Ref bean = "timebook"/>

</Property>

<! -- Specify proxy class -->

<Property name = "interceptornames">

<List>

<Value> log </value>

</List>

</Property>

</Bean>

</Beans>

(3) modify the test code testhelloworld and use the proxy class proxyfactorybean provided by spring to output logs. The sample code of testhelloworld. Java is as follows:

// ******** Testhelloworld. Java **************

Package com. gc. test;

Import com. gc. Action. timebook;

Import com. gc. Action. timebookproxy;

Import com. gc. impl. timebookinterface;

Public class testhelloworld {

Public static void main (string [] ARGs ){

// Obtain the configuration document through applicationcontext

Applicationcontext actx = new filesystemxmlapplicationcontext ("config. xml ");

Timebookinterface timebookproxy = (timebookinterface) actx. getbean ("logproxy ");

Timebookproxy. doauditing ("James ");

}

}

(4) run the test program and you can see the exception information output, as shown in Figure 5.10.

Figure 5.10 output of exception information

This is because the spring configuration document specifies that the proxy is implemented in the cglib mode, but the cglib-nodep-2.1_3.jar is not added to the classpath. Add the spring-framework-2.0-m1 under the cglib-nodep-2.1_3.jar/lib/cglib directory to the classpath according to the method described in Chapter 2nd.

(5) run the test program again and you can see the log information output by using the cglib agent, as shown in Figure 5.11.

Figure 5.11 log information output by using cglib proxy

The advantage of using cglib proxy is that you no longer need to implement a specific interface like using Java Dynamic proxy, just a common Java class.

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.