{"id":1279,"date":"2011-06-14T08:00:59","date_gmt":"2011-06-14T07:00:59","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=1279"},"modified":"2011-06-12T09:55:26","modified_gmt":"2011-06-12T08:55:26","slug":"finding-real-object-under-spring-proxies","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/","title":{"rendered":"Finding real object under spring proxies"},"content":{"rendered":"<p>As part of detecting side effects across tests, we needed to inspect some objects in tests autowired by <a href=\"http:\/\/www.springsource.org\/\">spring<\/a>. Unfortunately these objects were automatically proxied with the current configuration (and needed by some tests) so we needed some way of accessing the underlying proxied objects. <\/p>\n<p>We didn&#8217;t really find many good examples of how to go about bypassing the spring proxies created through <a href=\"http:\/\/static.springsource.org\/spring\/docs\/2.5.x\/reference\/aop.html\">AOP<\/a>. Doing a bit of debugging we found that spring used <a href=\"http:\/\/cglib.sourceforge.net\/\">CGLib<\/a> to generate its proxies and there is fortunately a method made accessible called <code><a href=\"http:\/\/static.springsource.org\/spring\/docs\/2.5.x\/api\/org\/springframework\/aop\/framework\/Advised.html#getTargetSource()\">getTargetSource()<\/a><\/code> that gives you the underlying object. Here&#8217;s the helper class we wrote to extract it.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class CglibHelper {\r\n    private final Object proxied;\r\n\r\n    public CglibHelper(Object proxied) {\r\n        this.proxied = proxied;\r\n    }\r\n\r\n    public Object getTargetObject() {\r\n        String name = proxied.getClass().getName();\r\n        if (name.toLowerCase().contains(&quot;cglib&quot;)) {\r\n            return extractTargetObject(proxied);\r\n        }\r\n        return proxied;\r\n    }\r\n\r\n    private Object extractTargetObject(Object proxied) {\r\n        try {\r\n            return findSpringTargetSource(proxied).getTarget();\r\n        } catch (Exception e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    private TargetSource findSpringTargetSource(Object proxied) {\r\n        Method&#x5B;] methods = proxied.getClass().getDeclaredMethods();\r\n        Method targetSourceMethod = findTargetSourceMethod(methods);\r\n        targetSourceMethod.setAccessible(true);\r\n        try {\r\n            return (TargetSource)targetSourceMethod.invoke(proxied);\r\n        } catch (Exception e) {\r\n            throw new RuntimeException(e);\r\n        }\r\n    }\r\n\r\n    private Method findTargetSourceMethod(Method&#x5B;] methods) {\r\n        for (Method method : methods) {\r\n            if (method.getName().endsWith(&quot;getTargetSource&quot;)) {\r\n                return method;\r\n            }\r\n        }\r\n        throw new IllegalStateException(\r\n                &quot;Could not find target source method on proxied object &#x5B;&quot; \r\n                        + proxied.getClass() + &quot;]&quot;);\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As part of detecting side effects across tests, we needed to inspect some objects in tests autowired by spring. Unfortunately these objects were automatically proxied with the current configuration (and needed by some tests) so we needed some way of accessing the underlying proxied objects. We didn&#8217;t really find many good examples of how to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,35],"tags":[],"class_list":["post-1279","post","type-post","status-publish","format-standard","hentry","category-agile","category-development","category-java","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Finding real object under spring proxies - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Finding real object under spring proxies - patkua@work\" \/>\n<meta property=\"og:description\" content=\"As part of detecting side effects across tests, we needed to inspect some objects in tests autowired by spring. Unfortunately these objects were automatically proxied with the current configuration (and needed by some tests) so we needed some way of accessing the underlying proxied objects. We didn&#8217;t really find many good examples of how to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2011-06-14T07:00:59+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Finding real object under spring proxies\",\"datePublished\":\"2011-06-14T07:00:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/\"},\"wordCount\":232,\"commentCount\":6,\"articleSection\":[\"Agile\",\"Development\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/\",\"name\":\"Finding real object under spring proxies - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2011-06-14T07:00:59+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2011\\\/06\\\/finding-real-object-under-spring-proxies\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Finding real object under spring proxies\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Finding real object under spring proxies - patkua@work","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/","og_locale":"en_US","og_type":"article","og_title":"Finding real object under spring proxies - patkua@work","og_description":"As part of detecting side effects across tests, we needed to inspect some objects in tests autowired by spring. Unfortunately these objects were automatically proxied with the current configuration (and needed by some tests) so we needed some way of accessing the underlying proxied objects. We didn&#8217;t really find many good examples of how to [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/","og_site_name":"patkua@work","article_published_time":"2011-06-14T07:00:59+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Finding real object under spring proxies","datePublished":"2011-06-14T07:00:59+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/"},"wordCount":232,"commentCount":6,"articleSection":["Agile","Development","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/","url":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/","name":"Finding real object under spring proxies - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2011-06-14T07:00:59+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2011\/06\/finding-real-object-under-spring-proxies\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Finding real object under spring proxies"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":4,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"predecessor-version":[{"id":1285,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/1279\/revisions\/1285"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}