How to insert a Proxied JDK class in Spring that protected methods?

I have a rather strange set of circumstances. I am using Spring 3.0.6 with a bean that implements an interface as well. Still pretty normal stuff. My bean also has some protected methods. I use some AOP (JDK Proxies) and everything works fine.

My problem arises when I want to insert this bean into another package class. Under normal circumstances, I could implement an implementation and access protected methods. Unfortunately, since this is a JDK proxy, I can only enter based on the interface.

Since I need access to protected methods, I cannot declare methods in the interface, so I'm a bit in a catch-22 situation. I tried switching to CGLIB proxies, but they crashed with other advisors advising beans with final methods, etc.), so this is not a solution.

Any suggestions on what I can do? I tried using the @PostConstruct method to extract a bean from the application context, but there (unsurprisingly) it can only get a Proxied bean and therefore cannot relate it to the required implementation.

Any suggestions would be appreciated.

Thanks!

Eric

+2
source share
1 answer

The solution I can come up with is to fetch the implementation from the proxy object.

http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/

As indicated in the link above (((Advised) proxy) .getTargetSource (). GetTarget () "returns the target implementation for the proxy object. Now you can apply this target to the Impl class, and then you can call the protected method on this impl class.

See if this works.

+1
source

Source: https://habr.com/ru/post/920782/


All Articles