java - 使用不同的构造函数参数并使用 Autowiring 创建同一类的 2 个 bean

标签 java spring spring-mvc spring-security

您好,我正在开发一个项目,其中我需要创建两个相同类但构造函数参数不同的 bean 实例。现在仅具有“生产环境”的功能,因此我有如下 xml 文件:

<context:component-scan base-package="com.xxx.yyy" /> 
    <bean id="id1" class="someCompanySpecificURLForTheClass" scope="singleton"/>

  <bean name="name1" factory-bean="id1" factory-method="createFullClient">
    <constructor-arg index="0">
        <ref bean="someJSONBean"/>
    </constructor-arg>
    <constructor-arg value="productionEnv" index="1"/>
</bean>  

</beans>

现在我还需要包含测试环境的功能。所以我将xml文件更改为:

<context:component-scan base-package="com.xxx.yyy" />
<context:component-scan base-package="com.zzz.yyy" /> 

 <bean id="id1" class="someCompanySpecificURLForTheClass" scope="prototype"/>

  <bean name="name1" factory-bean="id1" factory-method="createFullClient">
    <constructor-arg index="0">
        <ref bean="someJSONBean"/>
    </constructor-arg>
    <constructor-arg value="${value.name}" index="1"/>
</bean> 

在 2 个 Java 文件中,我在 setClients() 方法和创建 someJSONBean 对象上使用 @Autowired 注释。

但我收到错误:

BeanCreationException: could not autowire method: public void com.zzz.yyy.sometthing.setClients(someCompanySpecificURLForTheClass) throws IllegalArgumentException and UnsupportedEncodingException.
nested exception is defined: NoSuchBeanDefinitionException: no unique bean of type(someCompanySpecificURLForTheClass) is defined: expected single matching bean, found 2(name1, name2).

我是 Spring 框架的新手。谁能告诉我发生了什么问题,我做的 Autowiring 是错误的吗?我该如何解决这个问题?

编辑: 这里 someCompanySpecificURLForTheClass 是同一个类,并且 beans(name1 和 name2)使用此 bean 作为具有不同构造函数参数的工厂 bean。
我使用的是 spring 3.1。
经过大量搜索后,我想我可以使用占位符作为构造函数参数值,对吗? 上面是正在使用的编辑好的xml文件。 更新了 Java 类中的代码: 1.

@ConfigAdapter
 class class1{

  @Autowired
  private someJSONBean obj;

  @Autowired
  @Value("${value.name:thisismyvalue}")
  public void setClients(){}
}

 @ConfigAdapter
 class class2{

  @Autowired
  private someJSONBean obj;

  @Autowired
  @Value("${value.name:thisismyvalue}")
  public void setClients(){}
}

但是我收到以下错误:

ConversionNotSupportedException: failed to convert 'java.lang.string' to 'someCompanySpecificURLForTheClass'.
nested execption is IllegalStateException
Cannot convert 'java.lang.string' to 'someCompanySpecificURLForTheClass' of required type: no matching editors or conversion strategy found.

我是否错误地指定了注释值? 为什么会发生这种情况?

最佳答案

您必须将@Qualifier注释与@Autowired一起使用。由于您正在创建同一类的两个 bean,因此 spring 不知道要 Autowiring 哪一个。您可以使用@Qualifer("id/name")注释给出提示。

@Autowired
@Qualifier("name1")
 //yourProperty

@Autowired
@Qualifier("name2")
//yourProperty

看看这个 link

如果您想将这两个 bean 注入(inject)到您的类中,那么您需要创建两个不同的属性,每个属性都指向不同的实例。

@Autowired
@Qualifer("name1")
//property1

@Autowired
@Qualifer("name2")
//property2

关于java - 使用不同的构造函数参数并使用 Autowiring 创建同一类的 2 个 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33969509/

相关文章:

java - 找不到 appengine 1.7.4 sdk 和 com.google.appengine.datanucleus.DatastoreManager

java - 通过 Spring 集成从分页 REST 服务读取和下载

spring - FlywayDB 迁移删除

java - Swagger未获取PathVariable

java - JHipster ldap 认证

java - Spring Boot MVC - 如何在 application.properties 中配置多个 View 目录

java - 运行命令时遇到内部错误 : Error: Error occured while starting App

java - ADF Web 应用程序 : how to add and modify UI components on page loading (before it's displayed)?

java - 将java中的时间格式从YYYY-MM-DD转换为Day-Month-Year

java - 项目正在寻找数据源,但我正在使用 MongoDB