编程语言
首页 > 编程语言> > java-没有找到依赖类型为[org.springframework.social.twitter.api.Twitter]的匹配bean

java-没有找到依赖类型为[org.springframework.social.twitter.api.Twitter]的匹配bean

作者:互联网

我正在使用Spring Social连接到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 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配置如下:(仅显示相关部分)

<!-- Spring Social -->
    <bean id="connectionFactoryLocator"
        class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
        <property name="connectionFactories">
            <list>
                <bean
                    class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
                    <constructor-arg value="${twitter.consumerKey}" />
                    <constructor-arg value="${twitter.consumerSecret}" />
                </bean>
                <bean
                    class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                    <constructor-arg value="${facebook.clientId}" />
                    <constructor-arg value="${facebook.clientSecret}" />
                </bean>
            </list>
        </property>
    </bean>

    <bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors"
        factory-method="noOpText" />

    <bean id="usersConnectionRepository"
        class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
        <constructor-arg ref="jpaDataSource" />
        <constructor-arg ref="connectionFactoryLocator" />
        <constructor-arg ref="textEncryptor" />
    </bean>

    <bean id="connectionRepository" factory-method="createConnectionRepository"
        factory-bean="usersConnectionRepository" scope="request">
        <constructor-arg value="#{request.userPrincipal.name}" />
        <aop:scoped-proxy proxy-target-class="false" />
    </bean>

    <bean class="org.springframework.social.connect.web.ConnectController">
        <!-- relies on by-type autowiring for the constructor-args -->
        <property name="applicationUrl" value="${application.url}" />
    </bean>
    <!-- Spring Social -->

请指导.我会非常感激.

编辑

我想我忘了补充

@Bean
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)   
    public Twitter twitter() {
        Connection<Twitter> twitter = connectionRepository().findPrimaryConnection(Twitter.class);
        return twitter != null ? twitter.getApi() : new 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();
    }
}

然后将其包含在基于XML的配置中

<bean class="com.joinups.config.SocialApiConfig" />

感谢大家的指导,以得到正确的答案!非常感谢.你们好棒!

解决方法:

看来您没有声明Twitter类型的bean.

spring documentation开始,我可以看到您需要以某种方式实例化Twitter.

尝试声明一个bean< bean id =“ twitter” factory-bean =“ twitterConnectionFactory” />具有实例化TwitterTemplate对象所需的正确参数

编辑:

这是通过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>

参见this ohter – possible dupe – question

标签:spring-social,twitter,spring,java,spring-mvc
来源: https://codeday.me/bug/20191127/2074231.html