簡體   English   中英

我想創建非 web spring boot 應用程序

[英]i want to create the non-web spring boot application

我想創建非 web spring boot 應用程序。 但我收到以下錯誤

@SpringBootApplication
public class TaskApplication implements CommandLineRunner {

    @Autowired
    TaskService taskService;

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

    @Override
    public void run(String... args) throws Exception {

        taskService.print();

    }

}

public interface TaskService  {

    public void print();
}



public class TaskServiceImpl implements TaskService {

    @Override
    public void print() {
        System.out.println("sam");
    }

}

特性:

spring.datasource.url=jdbc:mysql://localhost:3306/demo?verifyServerCertificate=false&useSSL=false&requireSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root@123
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.main.allow-bean-definition-overriding=true
##spring.jpa.hibernate.ddl-auto=update

錯誤

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

Description:

Field taskService in com.example.task.TaskApplication required a bean of type 'com.example.task.service.TaskService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

行動:

考慮在您的配置中定義一個“com.example.task.service.TaskService”類型的 bean。

@M。 Deinum 已經在評論中解釋過了。 您需要使用@Service@Component批注標記類以創建 spring bean 並在主要方法中自動裝配。

    // mark it with service annotation.
    @Service
    public class TaskServiceImpl implements TaskService {

        @Override
        public void print() {
            System.out.println("sam");
        }

    }

要了解為什么這里需要這些注釋,請訪問此鏈接: @Component vs @Repository 和 @Service in Spring

您需要在主類上配置 @EnableAutoConfiguration 並且您的 TaskService 必須使用 @Service 進行注釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM