2012-10-02 12 views
8

Twitterの統合をSpring Socialと連携させるために、XML設定手法を使用して数時間を費やしています。XMLを使ってSpring Socialを設定する方法

:何らかの理由で samples

Bean定義に示すように、私はウェブ上で見つけることができるすべての例は、(とstackoverflowの上で)常にTwitterのAPIにインスタンスを取得する@Configアプローチを使用することはAOPの例外がスローされます

<?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:jaxrs="http://cxf.apache.org/jaxrs" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:cxf="http://cxf.apache.org/core" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 
     http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> 

    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DefaultDB" /> 

    <!-- initialize DB required to store user auth tokens --> 
    <jdbc:initialize-database data-source="dataSource" ignore-failures="ALL"> 
     <jdbc:script location="classpath:/org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql"/> 
    </jdbc:initialize-database> 

    <bean id="connectionFactoryLocator" 
     class="org.springframework.social.connect.support.ConnectionFactoryRegistry"> 
     <property name="connectionFactories"> 
      <list> 
       <ref bean="twitterConnectFactory" /> 
      </list> 
     </property> 
    </bean> 

    <bean id="twitterConnectFactory" class="org.springframework.social.twitter.connect.TwitterConnectionFactory"> 
     <constructor-arg value="xyz" /> 
     <constructor-arg value="xzy" /> 
    </bean> 

    <bean id="usersConnectionRepository" 
     class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository"> 
     <constructor-arg ref="dataSource" /> 
     <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 id="twitter" factory-method="findPrimaryConnection" 
     factory-bean="connectionRepository" scope="request" depends-on="connectionRepository"> 
     <constructor-arg value="org.springframework.social.twitter.api.Twitter" /> 
     <aop:scoped-proxy proxy-target-class="false" /> 
    </bean> 


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

    <bean id="connectController" class="org.springframework.social.connect.web.ConnectController"> 
     <constructor-arg ref="connectionFactoryLocator"/> 
     <constructor-arg ref="connectionRepository"/> 
     <property name="applicationUrl" value="https://socialscn.int.netweaver.ondemand.com/socialspringdemo" /> 
    </bean> 

    <bean id="signInAdapter" class="com.sap.netweaver.cloud.demo.social.SimpleSignInAdapter" /> 

</beans> 

私は何パズルconnectionRepositoryインスタンス化が(!私はTwitterの豆を、コメントアウトしてコードをテストした)完全に正常に動作することをされています。

Caused by: java.lang.IllegalStateException: Cannot create scoped proxy for bean 'scopedTarget.twitter': Target type could not be determined at the time of proxy creation. 

ここで私が持っている完全な設定ファイルですか?!?それは同じ機能を使用します:リクエストスコープとインタフェースAOPプロキシとは動作しますが、twitter beanインスタンス化に失敗しますか?次のように

春のソーシャル設定コードは(?私はどんな違いを見ることができない、することができます)になります。

@Configuration 
public class SocialConfig { 

    @Inject 
    private Environment environment; 

    @Inject 
    private DataSource dataSource; 

    @Bean 
    @Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES) 
    public ConnectionFactoryLocator connectionFactoryLocator() { 
     ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry(); 
     registry.addConnectionFactory(new TwitterConnectionFactory(environment.getProperty("twitter.consumerKey"), 
       environment.getProperty("twitter.consumerSecret"))); 
     return registry; 
    } 

    @Bean 
    @Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES) 
    public UsersConnectionRepository usersConnectionRepository() { 
     return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(), Encryptors.noOpText()); 
    } 

    @Bean 
    @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES) 
    public ConnectionRepository connectionRepository() { 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if (authentication == null) { 
      throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in"); 
     } 
     return usersConnectionRepository().createConnectionRepository(authentication.getName()); 
    } 

    @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 
    public ConnectController connectController() { 
     ConnectController connectController = new ConnectController(connectionFactoryLocator(), connectionRepository()); 
     connectController.addInterceptor(new PostToWallAfterConnectInterceptor()); 
     connectController.addInterceptor(new TweetAfterConnectInterceptor()); 
     return connectController; 
    } 

    @Bean 
    public ProviderSignInController providerSignInController(RequestCache requestCache) { 
     return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository(), new SimpleSignInAdapter(requestCache)); 
    } 
} 

すべてのヘルプ/ポインタをいただければ幸いです!

答えて

5

私はSpring Social Facebookとの統合に適した構成を持っています。 (私はそれにTwitterの構成を有している、しかし、私はそれでTwitterの部分をテストしていません)

<bean class="org.springframework.social.connect.web.ProviderSignInController"> 
<!-- relies on by-type autowiring for the constructor-args -->  
<constructor-arg ref="signInAdapter" /> 
</bean> 

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

<bean id="signInAdapter" class="com.test.social.SimpleSignInAdapter"/> 

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

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

私は主に読みに十分に小さいとして統合を行うことが多くを持っていたtutorialdocumentationを参照していますスプリングセキュリティ。これが何らかの形で役立つことを願っています。

+0

まあ、私が言ったように...公式の文書には、私の質問に関連する欠けている部分は含まれていません。 spring-social-showcaseアプリケーションは、@Configアノテーションベースの設定を使用します。あなたの例には、 "twitter"のbean定義が欠落している部分は含まれていませんが、やはりお試しいただきありがとうございます!乾杯! –

0

tomcat7のxml spring-mvc/spring-social設定が this question I postedにあります。

この質問は長年前に投稿されましたが、おそらく私の投稿の設定によって一部の人が時間を節約できます。 spring-social(1.1.4)とspring-social-twitter(1.1.2)のtwitter接続を含むXML設定と最新のspring 4.2.4 MVCをセットアップするのにかなり時間がかかりました。 ここにはバージョンが書かれています。なぜなら、春バージョン間にはかなりの違いがあるからです。

関連する問題