Java 没有为依赖项找到类型为[org.springframework.social.twitter.api.twitter]的匹配bean
我正在使用SpringSocial连接到Twitter。 连接部分工作正常,但当我尝试获取好友列表时,出现以下错误:Java 没有为依赖项找到类型为[org.springframework.social.twitter.api.twitter]的匹配bean,java,spring,spring-mvc,twitter,spring-social,Java,Spring,Spring Mvc,Twitter,Spring Social,我正在使用SpringSocial连接到Twitter。 连接部分工作正常,但当我尝试获取好友列表时,出现以下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.social.twitter.api.Twitter] found for dependency: expected at least 1
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.social.twitter.api.Twitter] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我的控制器类:
@Controller
@RequestMapping("/social")
public class SocialController {
private final Twitter twitter;
@Inject
public SocialController(Twitter twitter) {
this.twitter = twitter;
}
@RequestMapping(value="/twitter/friends", method=RequestMethod.GET)
public String friends(Model model) {
model.addAttribute("profiles", twitter.friendOperations().getFriends());
return "twitter/friends";
}
}
我的XML配置如下:(仅显示相关部分)
请导游。我会非常感激的
编辑
我想我忘记加了
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Twitter twitter() {
Connection<Twitter> twitter = connectionRepository().findPrimaryConnection(Twitter.class);
return twitter != null ? twitter.getApi() : new TwitterTemplate();
}
@Bean
@范围(value=“request”,proxyMode=ScopedProxyMode.INTERFACES)
公共推特{
Connection twitter=connectionRepository().findPrimaryConnection(twitter.class);
返回twitter!=null?twitter.getApi():新建TwitterTemplate();
}
到XML文件。任何关于它在XML上下文中如何表示的想法。我不熟悉基于注释的配置&因此使用基于xml的配置。请帮忙
编辑2
我决定采取变通办法。决定同时使用基于注释的配置和基于XML的配置。
加上我为大家做的事情:
我添加了一个配置:
public class SocialApiConfig {
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository connectionRepository) {
Connection<Facebook> facebook = connectionRepository.findPrimaryConnection(Facebook.class);
return facebook != null ? facebook.getApi() : new FacebookTemplate();
}
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public Twitter twitter(ConnectionRepository connectionRepository) {
Connection<Twitter> twitter = connectionRepository.findPrimaryConnection(Twitter.class);
return twitter != null ? twitter.getApi() : new TwitterTemplate();
}
}
public-class-SocialApiConfig{
@豆子
@范围(value=“request”,proxyMode=ScopedProxyMode.INTERFACES)
公共Facebook(ConnectionRepository ConnectionRepository){
ConnectionFacebook=connectionRepository.findPrimaryConnection(facebook.class);
返回facebook!=null?facebook.getApi():新建facebook模板();
}
@豆子
@范围(value=“request”,proxyMode=ScopedProxyMode.INTERFACES)
公共Twitter Twitter(ConnectionRepository ConnectionRepository){
Connection twitter=connectionRepository.findPrimaryConnection(twitter.class);
返回twitter!=null?twitter.getApi():新建TwitterTemplate();
}
}
然后将其包含在基于XML的配置中
<bean class="com.joinups.config.SocialApiConfig" />
感谢大家指导我得到正确答案!谢谢。你们太棒了 请在调试器中查看上下文中的bean,并查看是否存在从org.springframework.social.twitter.api.twitter派生的类。我怀疑有。我没有使用TwitterAPI,但我希望有一个XML配置元素告诉Spring使用它,或者有一个TwitterAPI jar需要在您的类路径上的某个地方
我希望这会有所帮助。看来您没有声明类型为
Twitter
的bean
从中我可以看出,您需要以某种方式实例化Twitter
尝试使用实例化TwitterTemplate对象所需的正确参数声明bean
编辑:
以下是通过xml进行的配置:
<bean id="twitter" factory-method="findPrimaryConnection"
factory-bean="connectionRepository" scope="request" depends-on="connectionRepository">
<constructor-arg value="org.springframework.social.twitter.api.Twitter" />
</bean>
请参阅在加载配置中的bean之前进行组件扫描。在注入时,Twitter并不存在。您可以像PH所说的那样,用XML定义twitterbean。还可以尝试使用@Autowired而不是@Inject来向控制器提供依赖关系。您正在为
Twitter
使用基于注释的声明,您可能需要在spring配置中添加类似的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
谢谢你的洞察力,克里斯。这绝对是个错误。我没有添加配置(现在编辑了问题,请检查编辑页脚)。您能指导我如何将其添加到application-context.xml中吗?我不熟悉基于注释的配置。只是一个想法。尝试删除@Scope注释以查看发生了什么。您是正确的,但我正在努力转换@Bean@Scope(value=“request”,proxyMode=ScopedProxyMode.INTERFACES)public Twitter(){Connection Twitter=connectionRepository().findPrimaryConnection(Twitter.class);return Twitter!=null?Twitter.getApi():新建TwitterTemplate();}这太棒了。我做了一个变通办法。请参见问题页脚中的编辑。但我仍然会接受这一备选答案。事实上,我的问题的实际答案。这似乎对我不起作用-ConnectionRepository#findPrimaryConnection返回一个连接,它与Twitter对象不同。创建一个中间对象调用连接#getApi的e工厂bean导致链接的dupe()中出现错误
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>