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



我有一个工作弹簧 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>

主控制器:

@Controller公共类 主页控制器 {

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 标签,它会给我以下错误,因为没有这样的豆子:

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

有什么建议吗?

你不需要在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" />
参考

:春季社交脸书参考

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

@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

最新更新