通过Spring Social从Facebook获取数据 [英] Fetching data from facebook by Spring social

查看:82
本文介绍了通过Spring Social从Facebook获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring social从Facebook提取数据,但是遇到了奇怪的错误

i am using Spring social for fetching the data from facebook but encountering a weird error

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.social.facebook.api.Facebook 

org.springframework.social.quickstart.HomeController.facebook; nested exception is 

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 qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我相信facebook对象将来自api.我不必手动定义它.

I believe facebook object will come from the api. I don't have to define it manually.

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">

    <display-name>Spring Social Facebook</display-name>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
      <servlet-name>appServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>appServlet</servlet-name>
      <url-pattern>/forms/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

rootcontext.xml

rootcontext.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- Enable @Annotation-drive bean configuration -->
    <context:annotation-config />

    <context:component-scan base-package="org" />

    <!-- Configures External Property Resolution -->
    <import resource="properties.xml" />

    <!-- Configures Shared Data Access Resources -->
    <!-- <import resource="data.xml" /> -->

    <!-- Configures Spring Social -->
    <bean class="org.springframework.social.quickstart.config.SocialConfig" />

</beans>

servlet-context.xml

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

</beans:beans>

在我的控制器中,我正在自动装配我的Facebook obj.

In my controller I am autowiring my Facebook obj.

@Controller
public class HomeController {

    @Autowired
    private Facebook facebook;
    /*
    @Inject
    public HomeController(Facebook facebook) {
        this.facebook = facebook;
    }*/

    @RequestMapping(value = "/fb", method = RequestMethod.GET)
    public String home(Model model) {
        System.out.println("Inside home method");
        List<Reference> friends = facebook.friendOperations().getFriends();
        model.addAttribute("friends", friends);
        return "home";
    }

}

推荐答案

摘自Spring社交快速入门示例.可能您不能自己注射,而是必须使用类似于工厂的方法:

taken from spring social quickstart example. Probably you cant inject it by yourself, instead you have to use factory-like method:

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

它需要其他依赖项,没有必要在此处复制粘贴所有内容.看一眼:

it needs other dependencies, no point to copy-paste them all here. Take a look at: https://github.com/spring-projects/spring-social-samples/blob/master/spring-social-quickstart/src/main/java/org/springframework/social/quickstart/config/SocialConfig.java

这篇关于通过Spring Social从Facebook获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆