java - spring bean 与 Set 属性相关的问题

标签 java spring

我有 bean ,例如

<bean id="manager" class="com.Manager" init-method="init">
       <property name="services">
        <set>
            <ref bean="service" />
        </set>
    </property>
</bean>

<bean id="myService"
        class="com.MyService" abstract="true">
</bean>

<bean id="service" class="com.SpecificService" parent="myService">

</bean>

service(SpecificService) 是使用抽象方法 init() 扩展抽象类 MyService 的类 并使用方法 SpecificLogic() 实现接口(interface) MyInterface。

因此,管理器在服务对象上调用 init() 方法,如下所示:

private Set<MyService> services;

public void init() {

    for (MyService service : services) {
        service.init();
    }



}

但是当bean初始化时我遇到以下问题:

Failed to convert property value of type 'java.util.LinkedHashSet' to required type 'java.util.Set' for property 'services'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy108 implementing com.MyInterface,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.MyService] for property 'services[0]': no matching editors or conversion strategy found.

最佳答案

您确定已发布所有配置吗? 因为周围有一些 AOP“隐藏”(可能是通过注释)代码,因为 service bean 被代理。 你必须选择:

  1. 更改 private Set<MyService> services 的声明到“私有(private)设置服务”
  2. 在 AOP 代码中让代理公开目标类 ( MyService ) 而不是接口(interface) ( MyInterface )

检查您的代码

关于java - spring bean 与 Set 属性相关的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22655085/

相关文章:

java - 使用 Apache Maven 在 IntelliJ 中构建 Java 项目的内存问题,发生在 Java 7 中,使用 Java 8 构建良好

java - 用户注销后重定向到登录页面,然后使用 Spring Security 按浏览器后退按钮

java - Spring MVC 缺少 URI 模板变量

java - Spring Batch - 如何使用从 REST API 请求收到的文件名启 Action 业

java - 在 Spring Boot 中重新创建查询

java - 从 servlet 调用独立的 java 程序

java - 根据模式验证 XML - 找不到元素声明

java - 使用 Spring Kafka 反序列化来自同一 Kafka 主题的不同 JSON 有效负载

java - 运行集成测试时嵌入 MongoDB

java - Java 如何决定具有相同名称但参数类型不同的方法?