簡體   English   中英

spring 引導中的自動裝配接口 null

[英]Autowired interface null in spring boot

嘗試構建一個名為 AccountService 的服務,但我不知道為什么我的 @Autowired 字段是 null。我嘗試了多種方法,例如 ComponentScan 以及您在谷歌上找到的所有東西。

先感謝您:)

正在自動接線的 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 ,因為您不能注入帶有聲明為 static 字段的@Autowired的 bean(有關更多信息,請參閱您可以將 @Autowired 與 static 字段一起使用嗎? )。

暫無
暫無

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

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