3

私は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 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" />

正しい答えを得るように私を導いてくれたみんなに感謝します!どうもありがとう。あなたたち最高!

4

4 に答える 4

3

タイプ の Bean を宣言していないようですTwitter

春のドキュメントから、Twitter何らかの方法でインスタンス化する必要があることがわかります。

オブジェクト<bean id="twitter" factory-bean="twitterConnectionFactory" />のインスタンス化に必要な正しいパラメーターを使用して Bean を宣言してみてください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>

この他のものを参照してください - だまされる可能性があります - 質問

于 2012-10-26T15:30:48.440 に答える
1

デバッガーでコンテキスト内の Bean を調べて、そこに org.springframework.social.twitter.api.Twitter から派生したクラスがあるかどうかを確認してください。あるとは思えません。Twitter API を使用したことはありませんが、Spring にそれを使用するように指示する XML 構成要素があるか、クラスパスのどこかに必要な Twitter API jar があると思います。

これが役立つことを願っています。

于 2012-10-26T15:24:47.473 に答える
0

アノテーションベースの宣言を使用しているTwitterので、おそらく春の設定にこのようなものを追加する必要があります

<?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>

アノテーションベースのコンテナ構成

于 2012-10-26T15:44:50.020 に答える
0

コンポーネント スキャンは、構成内の Bean がロードされる前に発生します。インジェクションの時点では、Twitter は存在しません。PH が述べているように、XML で Twitter Bean を定義することもできます。また、コントローラーに依存関係を提供するために @Inject の代わりに @Autowired を使用してみてください。

于 2012-10-26T15:43:25.857 に答える