You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot升级至2.0.0.RELEASE后Facebook Bean缺失问题求助

解决Spring Boot 2.0升级后Facebook Bean找不到的问题

Hey there, let's fix this issue you're running into when upgrading to Spring Boot 2.0. The root cause here is that Spring Boot 2.x removed the automatic configuration for Spring Social's Facebook API bean that was present in 1.5.x. Even if you added the correct spring-social-facebook version, you need to manually configure the necessary beans or adjust your code to fetch the Facebook API instance properly.

Step 1: Verify Dependencies

First, make sure your pom.xml has the correct, compatible versions for Spring Social components (match Spring Boot 2.0.x with Spring Social 2.0.x):

<dependencies>
    <!-- Spring Social Facebook -->
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-facebook</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
    <!-- Required core and web dependencies -->
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-core</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-web</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>
</dependencies>

Step 2: Configure Facebook Beans Manually

Create a configuration class to set up the FacebookConnectionFactory and either a FacebookTemplate (for public API calls) or a way to fetch authenticated user connections:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;

@Configuration
public class FacebookSocialConfig {

    @Value("${spring.social.facebook.app-id}")
    private String facebookAppId;

    @Value("${spring.social.facebook.app-secret}")
    private String facebookAppSecret;

    @Bean
    public FacebookConnectionFactory facebookConnectionFactory() {
        return new FacebookConnectionFactory(facebookAppId, facebookAppSecret);
    }

    // For unauthenticated/public API calls
    @Bean
    public Facebook facebookTemplate() {
        return new FacebookTemplate();
    }
}

Step 3: Adjust Controller to Use the Bean

If your HelloController was directly autowiring Facebook, update it to use the configured bean. Choose the option that fits your use case:

Option A: Use Public API Template

For accessing public Facebook data without user authentication:

import org.springframework.social.facebook.api.Facebook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    private final Facebook facebook;

    // Inject the manually configured FacebookTemplate
    public HelloController(Facebook facebook) {
        this.facebook = facebook;
    }

    @GetMapping("/hello")
    public String hello() {
        // Example: Fetch public page data
        return "Facebook Page Name: " + facebook.pageOperations().getPage("springframework").getName();
    }
}

Option B: Handle Authenticated Users

For users who have logged in via Facebook, fetch their authenticated connection:

import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    private final ConnectionRepository connectionRepository;

    public HelloController(ConnectionRepository connectionRepository) {
        this.connectionRepository = connectionRepository;
    }

    @GetMapping("/hello")
    public String hello() {
        Facebook facebook = connectionRepository.findPrimaryConnection(Facebook.class).getApi();
        return "Hello, " + facebook.userOperations().getUserProfile().getName() + "!";
    }
}

Step 4: Verify Application Properties

Make sure your application.properties has your Facebook app credentials:

spring.social.facebook.app-id=your-facebook-app-id
spring.social.facebook.app-secret=your-facebook-app-secret

This should resolve the NoSuchBeanDefinitionException by explicitly providing the required Facebook bean and ensuring compatibility between Spring Boot 2.0 and Spring Social versions.

内容的提问来源于stack exchange,提问作者Sultan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:08:30