spring - 将代理目标类 ="true"与 Spring bean 一起使用

标签 spring jersey

我正在使用 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/

相关文章:

java - 集成 JPA 和 SpringIOc

java - 为什么我的 Jersey 方法被调用两次?

java - Jersey REST Web 服务获取 Web 项目 URL

java - servlet容器和spring容器有什么区别?

java - Spring:根据MessageSource获取ResourceBundle

java - 如何在 spring xml 文件上启用 ehcache 统计信息

java - 删除方法在 Spring Boot&Thymeleaf 中不起作用

java - Jersey 中的 multipart/form-data 文件上传+其他参数

java - Jersey Hello World 给出 404

rest - Dropwizard 测试 - javax.validation.ConstraintViolationException : The request entity was empty