1

我使用彈簧社會對於從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> 

在我的控制,我自動裝配我的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"; 
    } 

} 

回答

2

取自春季社交快速入門示例。也許你不能自行注入它,而不是必須使用的工廠類方法:

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

它需要其他依賴,沒有一點他們都在這裏複製粘貼。看看: ​​