简体   繁体   English

"" 中构造函数的参数 0 需要一个无法找到的类型为 "" 的 bean

[英]Parameter 0 of constructor in "" required a bean of type "" that could not be found

The @RestController and its setup works well. @RestController及其设置运行良好。 I hit a snag when I was trying to add the service to the controller and auto-wiring them.当我尝试将服务添加到控制器并自动连接它们时,我遇到了障碍。

As far as I know everything looks like they've been wired well: it builds fine, and the IDE can't detect any problems.据我所知,一切看起来都连接良好:它构建良好,IDE 无法检测到任何问题。 But I seem to be lacking something because it won't run.但我似乎缺少一些东西,因为它不会运行。

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.church.id.controller.ChapterController required a bean of type 'org.church.id.service.ChapterService' that could not be found.


Action:

Consider defining a bean of type 'org.church.id.service.ChapterService' in your configuration.

ChapterController.java章控制器.java

@RestController
@Log4j
public class ChapterController {

    @Getter
    private Map<Integer, Chapter> chapters;

    private ChapterService chapterService;

    @Autowired
    public ChapterController(ChapterService chapterService) {

        this.chapterService = chapterService;
    }

    public void loadChapters() throws SQLException {

        log.debug("Loading chapters...");

        if (chapters != null) {
            chapters.clear();
        }
        chapters = chapterService.findAll().stream().collect(Collectors.toMap(Chapter::getId, Function.identity()));
    }

    @GetMapping("/chapter/list")
    public String list() throws SQLException {

        if (chapters == null) {
            loadChapters();
        }
        return chapters.values().stream().map(Chapter::getName).collect(Collectors.joining(", "));
    }
}

Inside my applicationContext.xml (My bad. I originally typed pom.xml)在我的 applicationContext.xml 里面(我不好。我最初输入的是 pom.xml)

    ...
    <bean id="chapterController" class="org.church.id.controller.ChapterController">
        <constructor-arg name="chapterService" ref="chapterService"/>
    </bean>

    <bean id="chapterService" class="org.church.id.service.impl.ChapterServiceImpl">
        <constructor-arg name="config" ref="dbConfig"/>
        <constructor-arg name="addressUtil" ref="addressUtil"/>
    </bean>
    ...

The real pom.xml真正的 pom.xml

        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
            <scope>test</scope>
        </dependency>
        ...

ChapterServiceImpl.java章节ServiceImpl.java

@Log4j
@AllArgsConstructor
@Setter
@Service
public class ChapterServiceImpl implements ChapterService {

    private DBConfig config;

    private AddressUtil addressUtil;
    ...
}

ChapterService.java (Interface) ChapterService.java(接口)

public interface ChapterService extends Service<Chapter> {

    Collection<Chapter> findByCountry(Country country) throws SQLException;
}

I've looked around here but the following have /not/ worked for me我环顾四周,但以下/不/为我工作

  • [x] Adding (exclude = {DataSourceAutoConfiguration.class}) to @SpringBootApplication [x] 添加(exclude = {DataSourceAutoConfiguration.class})@SpringBootApplication
  • [x] Prepending @Qualifier("chapterService") to the constructor's param: ChapterService chapterService [x] 将@Qualifier("chapterService")到构造函数的参数: ChapterService chapterService

Also to note: I've modified it to have only a default constructor, but the same type of error happens when I try to inject through a setter.还要注意:我已经将它修改为只有一个默认构造函数,但是当我尝试通过 setter 注入时会发生相同类型的错误。 I don't know what's wrong.我不知道怎么了。 :( :(

EDIT: MainClassController.java编辑:MainClassController.java

@SpringBootApplication
public class MainClassController {

    private ClassInfo model;

    private HashMap<Integer, String> classMap;

    public MainClassController() {
        loadData();
    }

    public void loadData() {

    }

    public static void main(String[] args) {

        SpringApplication.run(MainClassController.class, args);
    }
}

A little background: I started my project years ago, but got side tracked and now picking it up again.一点背景:我几年前开始了我的项目,但后来被搁置了,现在又重新开始了。

What worked for me was starting with a base spring-boot project and adding my classes and fixing as I went.对我有用的是从一个基本的 spring-boot 项目开始,然后添加我的类并在我进行时进行修复。

As what @JoãoDias linked in the comments: https://www.baeldung.com/spring-boot-start is where I got my base spring boot.正如@JoãoDias在评论中链接的那样: https : //www.baeldung.com/spring-boot-start是我获得基础弹簧靴的地方。 (Check Spring Initializr in the article). (查看文章中的 Spring Initializr)。

Also advised by@AbhijitSarkar was that "no one uses Spring XML config anymore" which actually is a reflection on when I started this project. @AbhijitSarkar还建议“没有人再使用 Spring XML 配置”,这实际上反映了我何时开始这个项目。 Very perceptive too that he was able to get the hint that my problem was due to combined internet searches.也很有洞察力,他能够得到暗示我的问题是由于联合互联网搜索造成的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ ”中构造函数的参数 0 需要找不到类型为“ ”的 bean - Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found 构造函数中的参数 3 需要一个无法找到的“包”类型的 bean - Parameter 3 of constructor in required a bean of type 'package' that could not be found Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found B类中构造函数的参数0需要一个找不到A类类型的Bean - Parameter 0 of constructor in Class B required a bean of type Class A that could not be found ... 中构造函数的参数 5 需要找不到类型为“...Mapper”的 bean - Parameter 5 of constructor in … required a bean of type '…Mapper' that could not be found 构造函数的参数 0 需要一个找不到的 bean - Parameter 0 of constructor required a bean that could not be found 构造函数需要一个找不到的 bean 类型 - Constructor required a bean type that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM