facebook - Spring Social 从 facebook 获取数据

标签 facebook spring spring-mvc spring-social spring-social-facebook

我正在使用 Spring Social 从 facebook 获取数据,但遇到了一个奇怪的错误

    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。我不必手动定义它。

我的 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

<?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

<?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>

在我的 Controller 中,我正在 Autowiring 我的 Facebook 对象。

@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 Social 快速入门示例。可能你无法自己注入(inject)它,而必须使用类似工厂的方法:

@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

关于facebook - Spring Social 从 facebook 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479190/

相关文章:

ios - 使用Facebook IOS SDK 3.1和Graph API发布带标题的图像

java - Spring Aspect 的监控方法

java - 如何在 session 中设置一个对象并在所有 Controller 中使用它?

python - Flask_login.current_user.is_authenticated 与 Flask-dance facebook.authorized

facebook - 如何将 Facebook 提要添加到我们的网站

java - Utdao.save(object) - java.lang.NullPointerException

java - 将证书导入 Spring 的问题

java - Spring @ExceptionHandler 返回 HTTP 406

flash - Facebook 政策文件错误

java - Tomcat WAR远程地址