gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:53:39 30 4
gpt4 key购买 nike

如果我尝试向构造函数注入(inject) Facebook 参数,我将尝试使用 facebook api 使用 spring + thymeleaf + hibernate 创建 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:/";
}
}

最佳答案

你的“FacebookController”没有默认构造函数public FacebookController(),Spring需要它来创建一个新的bean。当您声明构造函数 public FacebookController(Facebook facebook) 时,您是在告诉 Spring 唯一现有的构造函数接收一个参数。

你可以让你像默认构造函数

@Autowire
private Facebook facebook;

public FacebookController() { }

关于java - org.springframework.beans.factory.BeanCreationException & org.springframework.beans.BeanInstantiationException,未找到默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19767065/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com