我正在使用 Jersey Rest 并希望 Jersey 过滤器能够访问一些 spring bean。
但是,正如我从其他线程中发现的那样,如果它们是 Java 代理而不是生成的 Java 代理,Jersey 不会获取 Spring bean。我想添加 proxy-target-class="true"
这样做有什么影响,也可以只在单个 bean 上设置还是需要在所有引用的 bean 上设置?
最佳答案
通过设置 proxy-target-class="true"
您将使用 CGLIB2
作为代理,而不是 jdk 代理。
含义如下,如 documentation 中所述。 :
final methods cannot be advised, as they cannot be overriden.
You will need the CGLIB 2 binaries on your classpath, whereas dynamic proxies are available with the JDK. Spring will automatically warn you when it needs CGLIB and the CGLIB library classes are not found on the classpath.
The constructor of your proxied object will be called twice. This is a natural consequence of the CGLIB proxy model whereby a subclass is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice. This behavior is not exhibited when using JDK proxies. Usually, calling the constructor of the proxied type twice, is not an issue, as there are usually only assignments taking place and no real logic is implemented in the constructor.
此外,您应该能够使用
为特定组件创建“目标代理”proxyMode=ScopedProxyMode.TARGET_CLASS
关于spring - 将代理目标类 ="true"与 Spring bean 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15568112/