2012-10-02 74 views
8

我花了幾個小時試圖讓Twitter集成與Spring Social一起使用XML配置方法。我可以在網上找到的所有實例(和計算器)始終使用@Config方法如圖所示samples如何通過XML配置Spring Social

無論出於何種原因bean定義來獲得一個實例到Twitter API拋出一個AOP例外:

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. 

下面是完整的配置文件,我有:

<?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實例工作完全正常(我註釋掉嘰嘰喳喳豆和測試代碼!)?!?它使用相同的功能:請求範圍和接口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的一部分)

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

我主要指的documentation這是足夠小,閱讀和tutorial它更多的是與整合春天的安全。我希望這有助於某種方式。

+0

那麼,正如我所說......官方文件不包括我的問題涉及到的缺失部分。 spring-social-showcase應用程序使用基於註解的@Config配置。你的例子不包括缺少的部分 - 「twitter」的bean定義 - 但是,儘管如此,謝謝!乾杯! –

0

我有一個工作在tomcat7的xml spring-mvc/spring-social配置,在 this question I posted

這個問題發佈很久以前,但也許在我的文章中的配置將節省一些人一些時間。我花了相當長的時間來建立XML配置和最新的春季4.2.4 MVC,包括 spring-social(1.1.4)和spring-social-twitter(1.1.2)twitter連接。 我在這裏編寫版本,因爲彈簧版本之間有不同的東西。