Fetching data from facebook by Spring social

2019-05-29 01:41发布

问题:

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)}

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

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

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";
    }

}

回答1:

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