0

Given the following Java code (from a 3rd-party-library outside of my control):

package some.third.party.lib;    

interface MyInterface { ... }

and the following class A with a package-private constructor (this is its only constructor):

package some.third.party.lib;
[...]

class A implements MyInterface {

  A() {}

}

ProxyFactoryBean.setTargetName expects the ID of an already instantiated bean. Since my Spring Java Config class is in an application-specific package (and I don't want to change it to some.third.party.lib), I can't instantiate class A, since it's package-private. I'm aware that I could use reflection to temporarily change the visibility of A's constructor, but I would like to avoid this if possible.

Q: (How) can I create a Spring (4.2.1.RELEASE) ProxyFactoryBean of a class A in Spring Java Config without having to use reflection (and without having to put my Java Config Class into the same package as A)?

Please note that when using Spring XML config, this situation does not occur, because Spring (in the background) creates a Bean for class A, probably also using reflection.

ptikobj
  • 2,690
  • 7
  • 39
  • 64
  • 2
    If the third-party library made this class constructor package-private, it means it's not supposed to be instantiated by any class that is not in the same package. And it also probably means that it's supposed to be instantiated by some factory class that is in the same package. Why are you trying to circumvent that, and not using this factory to instantiate the class? What are the third-party librray, the interface and the class? – JB Nizet Sep 12 '15 at 20:04
  • You could, of course, simply create your own Factory in the same package (with public methods), but that would be same quality of hack as the reflection solution. – Florian Schaetz Sep 12 '15 at 20:12

0 Answers0