Spring AOP Demo and objects to get the Cglib agent

Source: Internet
Author: User
Tags aop static class

This article is divided into two parts: 1 gives an example of Spring AOP (which uses the Cglib proxy), and 2 gives the original object to get the Cglib agent.

1. Spring AOP Demo

This section refers to the blog post (http://www.voidcn.com/blog/daguanjia11/article/p-4628090.html)

1.1 Add Maven Dependencies

<dependency> <groupId>org.springframework</groupId> <artifactId> spring-core</artifactid> <version>4.0.5.RELEASE</version> </dependency> <dependency > <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> < version>4.0.5.release</version> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-context</artifactId> <version>4.0.5.
    release</version> </dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId> <version>4.0.5.RELEASE</version> </dependency>
    <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.1</version> </dependency> 


1.2 Adding test classes

public class Springaoptest {public

    static class car {public
        void Go () {
            System.out.println (' Go to go! ');
        }
    }

    public static class Carlogger {public

        void Beforerun () {
            System.out.println ("The car being going to run")
        ;

        public void Afterrun () {
            System.out.println (' car is running ');
        }

    public static void Main (string[] args) {
        ApplicationContext context = new Classpathxmlapplicationcontext (" Spring-aop-test.xml ");
        Car car = [car] context.getbean ("car");
        System.out.println (Car.getclass ());
        Car.go ();
    }


1.3 Add Spring configuration file

Placed in the root directory of the classpath, the amount, in the Resources directory is also the line.

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd "> <beans xmlns=" http:// Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:p=" http:// www.springframework.org/schema/p "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:aop=" http ://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop.xsd "> <bean id=" car "class=" com. Springaoptest.car"/> <bean id=" logger "class=" com. Springaoptest.carlogger "/> <aop:config> <aop:aspect ref=" Logger "> < Aop:pointcut expression= "Execution" (* com. SpringAopTest.Car.go (..)) " Id= "Go"/> <aop:before pointcut-ref= "Go" method= "Beforerun"/> <aop:after Tcut-ref= "Go" method= "Afterrun"/> </aop:aspect> </aop:config> </beans> < /beans>

1.4 Running main function

The following results are obtained:

Car was going to run go
go!
Car is running


Well, you see the go go! before and after the addition of the Action Bar.


2. Get the object that is Cglib agent

This section refers to the blog post (http://blog.csdn.net/liufeng520/article/details/43563161)

Let's go through debug to see where the agent is hidden.



OK, after we finish, we'll write the code as follows:


Import Java.lang.reflect.Field;
Import Org.springframework.aop.framework.AdvisedSupport;
Import Org.springframework.aop.support.AopUtils;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;
 /** * Created by QM on 17/2/15. */public class Springaoptest {public static class car {public void Go () {System.out.println ()

    Go to go! ");}
        public static class Carlogger {public void Beforerun () {System.out.println ("the car being going to run");
        public void Afterrun () {System.out.println (' car is running '); } public static Object Gettarget (object beaninstance) {if (!
        Aoputils.isaopproxy (beaninstance)) {return beaninstance; else if (Aoputils.iscglibproxy (beaninstance)) {try {Field h = beaninstance.getclass (). getd
  Eclaredfield ("Cglib$callback_0");              H.setaccessible (TRUE);

                Object dynamicadvisedinterceptor = H.get (beaninstance);
                Field advised = Dynamicadvisedinterceptor.getclass (). Getdeclaredfield ("advised");

                Advised.setaccessible (TRUE);
                Object target = ((Advisedsupport) Advised.get (Dynamicadvisedinterceptor)). Gettargetsource (). Gettarget ();
            return target;
            catch (Nosuchfieldexception e) {e.printstacktrace ();
            catch (Illegalaccessexception e) {e.printstacktrace ();
            catch (Exception e) {e.printstacktrace ();

    } return null; public static void Main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("s
        Pring-aop-test.xml ");
        Car car = [car] context.getbean ("Car");
        Object target = Gettarget (car);
        System.out.println (target = car); System.out.printlN (Car.getclass ());
        System.out.println (Target.getclass ());
    Car.go (); }
}






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.