应用程序无法启动 Spring 工具套件

Application Failed to Start Spring Tool Suite

我正在从头开始创建 spring 引导项目并尝试 运行 我的项目,但它无法启动。我遇到以下错误:

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    
    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Field users in com.example.demo.DataInitializer required a bean of type 'com.example.repository.UserDao' that could not be found.
    
    The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)
    
    Action:
    
    Consider defining a bean of type 'com.example.repository.UserDao' in your configuration.

这是我的 UserDao(存储库):

package com.example.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.model.User;

@Repository
public interface UserDao extends JpaRepository<User, Long>{
    User findByEmail(String email);
    User findByStatus(int status);
}

这是我的 DataInitializer 文件,我正在尝试 运行:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.example.model.User;
import com.example.repository.UserDao;

@Component
public class DataInitializer implements CommandLineRunner{

    @Autowired
    UserDao users;
    @Autowired
    private PasswordEncoder passwordEncode;
    @Override
    public void run(String... args) throws Exception {
        //System.out.println("Running spring boot demo application");
        this.users.save(User.builder().email("example@gmail.com").phone("1234567890")
                .username("Example Name").userid(10001L).build());
    }
}

您要么必须在演示包(应用程序 class)中使用 @ComponentScan,要么从演示中创建包,例如:

example.demo.package1
example.demo.package2

而不是:

example.package1

(右键单击演示包并从中创建包)

如果您想使用组件扫描并保留您的包名称,请参阅 https://www.baeldung.com/spring-component-scanning

如果您已经将包和 classes 放在一个地方,您所要做的就是将它们拖到正确的包中并重构它,因为它会让您自动更改名称。