gpt4 book ai didi

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'demoRestController'

转载 作者:太空狗 更新时间:2023-10-29 23:00:50 28 4
gpt4 key购买 nike

我是 Spring 的新手,我正在尝试制作一个学习应用程序,但我在 Autowiring 中遇到问题,我正在添加我的代码。我正在研究 spring boot。

Spring 启动代码

public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

登录Bean.java

@Service
@Component
public class LoginBean {
private String userId;
private String pwd;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

DemoRestController.java

@RestController
@EnableAutoConfiguration
@RequestMapping("/demo")
@Component
public class DemoRestController {


private final LoginBean loginBean;

@Autowired
public DemoRestController(LoginBean loginBean) {
this.loginBean=loginBean;
}

@RequestMapping(value = "/login/{id},{pwd}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody LoginBean loginService(@PathVariable String id, @PathVariable String pwd) {
//LoginBean loginBean = new LoginBean();

loginBean.setUserId(id);
loginBean.setPwd(pwd);
return loginBean;
}

我尝试了以下场景来使我的@Autowired 工作:

  1. @Autowired 到 LoginBean loginBean;
  2. 在 Controller 类中创建 LoginBean 的 getter setter 和 Autowiring 的 setter;
  3. 创建 Controller 的构造函数并 Autowiring ,如上面的代码所示;

下面是我得到的错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController': Unsatisfied dependency expressed through constructor argument with index 0 of type [com.ag.digital.demo.bean.LoginBean]: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at com.ag.digital.demo.boot.DemoApplication.main(DemoApplication.java:14) [classes/:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ag.digital.demo.bean.LoginBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 19 common frames omitted

最佳答案

您的 DemoApplication 类在 com.ag.digital.demo.boot 包中,您的 LoginBean 类在 com.ag.digital.demo.bean 包。默认情况下,如果组件(用 @Component 注释的类)位于主应用程序类 DemoApplication 的同一个包或子包中,则会被找到。这意味着未找到 LoginBean,因此依赖注入(inject)失败。

有几种方法可以解决您的问题:

  1. LoginBean 移动到 com.ag.digital.demo.boot 或子包中。
  2. 使用 @SpringBootApplicationscanBasePackages 属性配置扫描组件的包,该属性应该在 DemoApplication 上。

其他一些不会导致问题,但与您发布的代码不太正确的事情:

  • @Service@Component 的特化,因此您不需要 LoginBean
  • 同样,@RestController@Component 的特化,因此您不需要在 DemoRestController
  • 上同时使用它们
  • DemoRestController@EnableAutoConfiguration 的一个不寻常的地方。该注解通常可以在您的主应用程序类 (DemoApplication) 上直接找到,也可以通过 @SpringBootApplication 找到,它是 @ComponentScan 的组合>@Configuration@EnableAutoConfiguration

关于java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'demoRestController' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37214538/

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