Spring 手动连接bean(具有自动连接依赖项):转换失败

Spring 手动连接bean(具有自动连接依赖项):转换失败,spring,Spring,在util类中,我想使用我的一个服务。 现在这个服务是有线的,但是util不是 因此,在我的Util类中,我会: ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"context.xml"}); UserService userService = (UserService) ((BeanFactory)context).getBean("userServiceWired"); 在my c

在util类中,我想使用我的一个服务。 现在这个服务是有线的,但是util不是

因此,在我的Util类中,我会:

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"context.xml"});
UserService userService = (UserService) ((BeanFactory)context).getBean("userServiceWired");
在my context.xml中,我执行以下操作:

<bean id="userServiceWired" class="com.daniels.jack.service.userServiceImpl">
    <property name="restTemplate" value="restTemplateWired" />
</bean>
<bean id="restTemplateWired" class="org.springframework.web.client.RestTemplate"/>

但我得到:

无法将类型为“java.lang.String”的属性值转换为所需类型“org.springframework.web.client.RestTemplate”


UserService UserService=…
行上。

使用ref而不是value

<property name="restTemplate" ref="restTemplateWired" />

使用ref而不是value

<property name="restTemplate" ref="restTemplateWired" />


注意:您可以将代码的第二行简化为
UserService UserService=context.getBean(“userServiceWired”,UserService.class)注意:您可以将代码的第二行简化为
UserService UserService=context.getBean(“userServiceWired”,UserService.class)