org.springframework.beans.factory.BeanCreationException& org.springframework.beans.BeanInstantiationException,找不到默认构造函数

时间:2013-11-04 11:45:50

标签: java spring hibernate annotations

我正在尝试使用facebook api创建一个使用spring + thymeleaf + hibernate的facebook应用程序,如果我试图将一个Facebook参数注入构造函数

显示以下错误

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'facebookController' defined in file [D:\ff-sdefault constructor found; nested exception is java.lang.NoSuchMethodException: com.ff.FacebookController$$EnhancerByCGLIB$$80bb261.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:975)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:665)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext.refresh(AnnotationConfigEmbeddedWebApplicationContext.java:193)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:514)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:281)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:697)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:686)
    at com.fff.Application.main(Application.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ff.FacebookController$$EnhancerByCGLIB$$80bb261]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.fff.FacebookController$$EnhancerByCGLIB$$80bb261.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1023)
    ... 21 more
Caused by: java.lang.NoSuchMethodException: com.fff.FacebookController$$EnhancerByCGLIB$$80bb261.<init>()
    at java.lang.Class.getConstructor0(Class.java:2800)
    at java.lang.Class.getDeclaredConstructor(Class.java:2043)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
    ... 22 more
2013-11-04 16:30:27.157  INFO 5132 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

这是代码

package com.fff;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.inject.Inject;

@Controller
@RequestMapping("/facebook")
@Configuration
@ComponentScan("com.fff.service")
public class FacebookController {
    private Facebook facebook;

    @Autowired
    private RegisterUserService registerUserService;

    @Inject
    public FacebookController(Facebook facebook) {
        this.facebook = facebook;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(Model model) {
        if (!facebook.isAuthorized()) {
            return "redirect:/connect/facebook";
        }
        String email = facebook.userOperations().getUserProfile().getEmail();
        String name = facebook.userOperations().getUserProfile().getName();
        String firstName = facebook.userOperations().getUserProfile().getFirstName();
        String lastName = facebook.userOperations().getUserProfile().getLastName();
        String middleName = facebook.userOperations().getUserProfile().getMiddleName();
        String dateOfBirth = facebook.userOperations().getUserProfile().getBirthday();
        String userName = facebook.userOperations().getUserProfile().getUsername();
        String gender = facebook.userOperations().getUserProfile().getGender();
        String religion = facebook.userOperations().getUserProfile().getReligion();

        model.addAttribute("user", new User(name, firstName, lastName));

        return "registration";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Model model, User user) {
        registerUserService.registerUser(user);
        return "redirect:/";
    }
}

1 个答案:

答案 0 :(得分:1)

你的&#34; FacebookController&#34;没有默认构造函数public FacebookController(),Spring需要它来创建一个新的bean。当你声明构造函数public FacebookController(Facebook facebook)时,你告诉Spring,唯一的现有构造函数会收到一个参数。

您可以默认使用

这样的构造函数
@Autowire
private Facebook facebook;

public FacebookController() { }