我有一个可运行的spring mvc webapp,它是基于xml的,因此我必须使用相同的过程,而不是“ pure java configs”。

我正在尝试将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
公共类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


有什么建议么?

最佳答案

您无需将spring的facebook接口添加为xml中的类。而是使用您的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

10-07 19:00
查看更多