spring 引导中的自动装配接口为空

Autowired interface null in spring boot

正在尝试构建名为 AccountService 的服务,但我不知道为什么我的 @Autowired 字段为空。我尝试了很多东西,比如 ComponentScan 和你在 google.

上找到的所有东西

提前谢谢你:)

正在自动连接的class:

package de.scroll.AccountService.Account;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface AccountRepository extends MongoRepository<Account, String> {

    public Optional<Account> findById(String id);
    public List<Account> findByEmailaddress(String email);
}

自动接线 class 应该在其中使用的 class:

package de.scroll.AccountService;

import de.scroll.AccountService.Account.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class AccountServiceApplication {

    @Autowired
    public static AccountRepository repository;

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

        test();
    }

    public static void test() {
        repository.findAll();
    }
}

错误:

Exception in thread "main" java.lang.NullPointerException
    at de.scroll.AccountService.AccountServiceApplication.test(AccountServiceApplication.java:22)
    at de.scroll.AccountService.AccountServiceApplication.main(AccountServiceApplication.java:18)

字段 repositorynull,因为您不能将 @Autowired 声明为静态字段的 bean 注入(有关详细信息,请参阅 Can you use @Autowired with static fields?)。