Java 如何将代理注入到服务中?

Java 如何将代理注入到服务中?,java,spring,dependency-injection,proxy,Java,Spring,Dependency Injection,Proxy,tomcat引擎中有一些我们想要访问运行时的信息,因此我们在应用程序上下文中有以下内容(从中获得): 我们不能像在博客文章中那样连接org.apache.catalina.Engine,因为该类在构建时不可用。它只在不同机器上运行所有不同的tomcat版本时才可用 我们能够使用反射从@Autowire获取所需的信息。现在,我们希望将此功能移动到服务中。我将此添加到我们的应用程序上下文中: <bean id="myService" class="com.foo.bar.MyServiceIm

tomcat引擎中有一些我们想要访问运行时的信息,因此我们在应用程序上下文中有以下内容(从中获得):

我们不能像在博客文章中那样连接
org.apache.catalina.Engine
,因为该类在构建时不可用。它只在不同机器上运行所有不同的tomcat版本时才可用

我们能够使用反射从@Autowire获取所需的信息。现在,我们希望将此功能移动到服务中。我将此添加到我们的应用程序上下文中:

<bean id="myService" class="com.foo.bar.MyServiceImpl">
    <constructor-arg ref="tomcatEngineProxy" />
</bean>
执行此操作时,会出现以下错误:

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.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.jmx.access.MBeanProxyFactoryBean]: no matching editors or conversion strategy found

基本上不知道代理如何工作以及如何使用它们,我不知道如何让它工作。是否有一些声明可以用于将匹配的构造函数arg?控制器中的@Autowire起作用,而构造函数arg不起作用,两者之间有什么不同?

我认为
@Autowired
起作用,因为绑定是根据Bean的类型完成的(即
MBeanProxyFactoryBean
),但是,到构造函数参数的绑定是按名称完成的,并且您提供的名称与您期望的类型不匹配,因为使用FactoryBean创建的bean的类型与

看起来最简单的解决方案是将
MyServiceImpl
更改为:

public class MyServiceImpl implements MyService
{
    @Autowired 
    private MBeanProxyFactoryBean tomcatEngineProxy;

    public MyServiceImpl() throws Exception
    {
         //stuff with the proxy
    }
    .....
}

这应该可以做到。

这是因为您的工厂bean将结果公开为引擎接口:

<property name="proxyInterface" value="org.apache.catalina.Engine" />

因此,如果您尝试连接“tomcatEngineProxy”bean本身,它唯一兼容的赋值是“org.apache.catalina.Engine”,因为创建的代理只实现该接口

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



如上所述,我们在构建时没有org.apache.catalina.Engine可用。它的可用性将把我们与构建它所依据的tomcat的任何版本联系在一起,并且我们在所有不同的服务器上运行着不同版本的tomcat。我不知道怎么做,但控制器中的自动接线确实起作用了。@dnc253:对不起,我错过了问题的这一部分。太好了!工作得很有魅力。我以前从未见过这种语法。我确实必须这样做:
,但这只是让XML快乐的问题。谢谢NP只是不要在EL表达式中尝试这个,因为它已经坏了:)我有一个bug。
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.SpringProxy,org.springframework.aop.framework.Advised] to required type [org.springframework.jmx.access.MBeanProxyFactoryBean]: no matching editors or conversion strategy found
public class MyServiceImpl implements MyService
{
    @Autowired 
    private MBeanProxyFactoryBean tomcatEngineProxy;

    public MyServiceImpl() throws Exception
    {
         //stuff with the proxy
    }
    .....
}
<property name="proxyInterface" value="org.apache.catalina.Engine" />
<constructor-arg ref="&tomcatEngineProxy" />