将Spring Socials(Facebook)与Spring MVC XML集成

时间:2016-02-08 16:06:47

标签: java spring spring-mvc spring-social

我有一个工作的spring mvc webapp,它是基于xml的,所以我必须使用相同的程序,而不是“纯java配置”。

我正在尝试将facebook登录集成到我的应用程序中,我试图遵循许多教程,但却无法让它们发挥作用。

以下是我的尝试之一:(https://spring.io/guides/gs/accessing-facebook/

修改

我的XML现在是这样的:

<?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:facebook="http://www.springframework.org/schema/social/facebook"
    xmlns:twitter="http://www.springframework.org/schema/social/twitter"
    xmlns:social="http://www.springframework.org/schema/social"
    xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
        http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
        http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

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

                    <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                        <constructor-arg value="${facebook.clientId}" />
                        <constructor-arg value="${facebook.clientSecret}" />                
                    </bean>
                </list>
            </property>
        </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 class="org.springframework.social.connect.web.ConnectController">
            <!-- relies on by-type autowiring for the constructor-args -->
        </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>



    <facebook:config app-id="962223610477458" app-secret="b7dfec28b08ac4e8c2a09cbac4662c15" app-namespace="setelog_selectandwin" />

</beans>

HomeController中:

@Controller 公共课HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

private Facebook facebook;
private ConnectionRepository connectionRepository;

@Inject
public HomeController(Facebook facebook, ConnectionRepository connectionRepository) {
    this.facebook = facebook;
    this.connectionRepository = connectionRepository;
}

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
     if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
            return "redirect:/connect/facebook";
        }

        model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
        PagedList<Post> feed = facebook.feedOperations().getFeed();
        model.addAttribute("feed", feed);
        return "facebook/hello";
}

}

现在错误是 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'scopedTarget.connectionFactoryLocator'的bean

如果我删除了facebook:config标签,它会给我以下错误,因为没有这样的bean:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.social.facebook.api.Facebook] found for dependency: expected at least 1 bean which

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您无需在xml中将spring的facebook界面添加为类。而是使用你的facebook客户端id和秘密使用xml或java代码创建FacebookConnectionFactory。

@Configuration
public class SocialConfig implements SocialConfigurer {

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        cfConfig.addConnectionFactory(new FacebookConnectionFactory(
            env.getProperty("facebook.clientId"),
            env.getProperty("facebook.clientSecret")));
    }

    ...
}

使用Spring Social Facebook的XML配置命名空间:

<facebook:config app-id="${facebook.clientId}"
                 app-secret="${facebook.clientSecret}"
                 app-namespace="socialshowcase" />

参考:Spring Social Facebook Reference

答案 1 :(得分:0)

摘自春季社交快速启动示例。可能你不能自己注射它,而是你必须使用类似工厂的方法:

@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)   
public Facebook facebook() {
    return connectionRepository().getPrimaryConnection(Facebook.class).getApi();
}

它需要其他依赖项,没有必要在这里复制粘贴它们。看看:https://github.com/spring-projects/spring-social-samples/blob/master/spring-social-quickstart/src/main/java/org/springframework/social/quickstart/config/SocialConfig.java