Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – 如何将代理注入服务?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

tomcat引擎中有一些我们想要访问运行时的信息,所以我们在app环境中有以下内容(从this blog post开始):

tomcatENGIneProxy" class="org.springframework.jmx.access.MBeanProxyfactorybean">
    

在控制器中,我们然后像这样自动装配它:

@Autowired
private MBeanProxyfactorybean tomcatENGIneProxy = null;

我们不能像在博客文章中那样连接org.apache.catalina.ENGIne,因为在构建时我们无法使用该类.它仅在运行时可用,并且在不同的计算机上运行所有不同的tomcat版本.

我们能够使用反射从这个@Autowire获取我们需要的信息.现在,我们希望将此功能转移到服务中.我将此添加到@R_268_9616@上下文中:

这堂课看起来像这样

public class MyserviceImpl implements Myservice
{
    public MyserviceImpl(MBeanProxyfactorybean tomcatENGIneProxy) throws Exception
    {
         //stuff with the proxy
    }
    .....
}

当我这样做时,我收到以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creaTing bean with name 'myservice' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.jmx.access.MBeanProxyfactorybean]: Could not convert constructor argument value of type [$Proxy44] to required type [org.springframework.jmx.access.MBeanProxyfactorybean]: Failed to convert value of type '$Proxy44 implemenTing org.apache.catalina.ENGIne,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type 'org.springframework.jmx.access.MBeanProxyfactorybean'; nested exception is java.lang.IllegalStateException: CAnnot convert value of type [$Proxy44 implemenTing org.apache.catalina.ENGIne,org.springframework.aop.framework.Advised] to required type [org.springframework.jmx.access.MBeanProxyfactorybean]: no matching editors or conversion strategy found

基本上不知道代理如何工作以及如何使用它们,我不知道如何使这项工作成功.是否有一些声明我可以用于我的构造函数arg匹配?在工作的控制器中的@Autowire和不起作用的构造函数arg之间有什么不同?

最佳答案
是因为您的工厂bean将结果公开为引擎接口:

因此,如果您尝试连接“tomcatENGIneProxy”bean本身,那么只有兼容的赋值是“Org.apache.catalina.ENGIne”,因为创建的代理只实现了该接口.

尝试直接引用工厂bean(注意&符号,这是查找创建对象而不是对象本身的实际工厂bean的语法):

How to inject FactoryBean instead of object it produces?

大佬总结

以上是大佬教程为你收集整理的java – 如何将代理注入服务?全部内容,希望文章能够帮你解决java – 如何将代理注入服务?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。