解决NoSuchBeanDefinitionException: No qualifying bean of type ‘bean.User‘ available

简介: 解决NoSuchBeanDefinitionException: No qualifying bean of type ‘bean.User‘ available

在springboot启动后,发生如下错误,编译器告诉我们是没有匹配的Bean

如下所示为启动类中的代码:

package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
      ConfigurableApplicationContext run= SpringApplication.run(MainApplication.class,args);
      User user=new User("张三",21);
      run.getBean(User.class);
      System.out.println(user);
    }
}

User类:

package com.springboot;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Controller
public class User {
    private String name;
    private Integer age;
}

去检查了一下确实如此,如下所示:

User确实不在启动类的包或者子包下,所以导致出现了没有被扫描到的情况

解决办法如下所示:

我们只需要将组件User添加至启动类的包或者子包下即可!

输出如下所示:

相关文章
|
2月前
|
Java Spring
No qualifying bean of type 'XXXXX' available
该文档描述了Spring框架中`org.springframework.beans.factory.NoSuchBeanDefinitionException`异常的处理方法,该异常是因为无法找到类型为`com.weblog.auth.mapper.UserMapper`的bean导致。解决办法包括在对应的Mapper上添加`@Mapper`注解,并在启动类上添加`@MapperScan`注解以扫描包含Mapper接口的文件夹。
88 8
|
4月前
|
XML 数据格式
Could not autowire. No beans of ‘VideoDao‘ type found.
Could not autowire. No beans of ‘VideoDao‘ type found.
|
6月前
No qualifying bean of type [com.XX.mapper.SysNameMapper ] found for dependency
No qualifying bean of type [com.XX.mapper.SysNameMapper ] found for dependency
93 0
|
Java Spring 容器
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
939 0
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
|
Java 容器 Spring
No qualifying bean of type或Unsatisfied dependency expressed through field ‘XXXService‘或@Autowired 注入
No qualifying bean of type或Unsatisfied dependency expressed through field ‘XXXService‘或@Autowired 注入
161 0
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
293 0
|
NoSQL Java Redis
5. 成功解决:Could not autowire. No beans of 'RedisConnectionFactory' type found.
今天建了一个新项目,使用 Spring Boot 整合 Redis 时,IDEA 被提醒“Could not autowire. No beans of 'RedisConnectionFactory' type found. ”错误,意思是不能自动装配“RedisConnectionFactory”。
3113 1
|
Java 数据库连接 Spring
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ XXX‘
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ XXX‘
208 0
|
Java 编译器
Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.已解决
在创建redisTemplate时,形参列表爆出了如下错误
1096 0
Could not autowire. No beans of ‘RedisConnectionFactory‘ type found.已解决
|
Java Spring 容器
报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xxx'is defined
报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xxx'is defined
483 0
报错:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xxx'is defined