簡體   English   中英

Spring-boot:找不到bean

[英]Spring-boot:bean could not be found

我是 spring 引導的新手,我正在為基本實踐編寫 CRUD 操作,這是我的代碼。

演示應用程序.java:

 package com.example.controller;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;

 @SpringBootApplication
  public class DemoApplication {

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

}

用戶.java

   package com.example.model;

  public class User {
   String userName;
  String password;

public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

用戶服務.java:

 package com.example.services;
 import com.example.model.User;
 import org.springframework.stereotype.Repository;
 import org.springframework.stereotype.Service;

 @Repository
 public interface UserServices {
     public String loginService(User user);
 }

UserServiceImplementatioin.java:

package com.example.serviceimplementation;
import com.example.model.User;
import com.example.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImplementation implements UserServices {
    public String loginService(User user) {
     if(user.getUserName().equals("demouser") && user.getPassword().equals("demopass")) {
        return "Login successfully";
     }
    return "Invalid Username and password";

    }
 }

服務控制器.java:

  package com.example.controller;
  import com.example.services.UserServices;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.web.bind.annotation.*;
  import com.example.model.User;

 @RestController
 @RequestMapping(value="/askmeanything")
  public class ServiceController {
  @Autowired
  private UserServices userServices;

  public UserServices getUserServices() {
    return userServices;
  }

  public void setUserServices(UserServices userServices) {
    this.userServices = userServices;
  }

 @CrossOrigin(origins = "*")
 @RequestMapping(value = "/login", method = RequestMethod.POST)
  public String getMsg(@RequestBody User user) throws  Exception {
    return userServices.loginService(user);
  }
}

上面的代碼給了我錯誤com.example.controller.ServiceController 中的字段 userServices 需要找不到類型為“com.example.services.UserServices”的 bean。

這是因為您的DemoApplication是在 package com.example.controller之后定義的。 因此,默認情況下 Spring 將僅掃描 package 及其衍生。 例如com.example.controller.something 它不會掃描父包。

要么將DemoApplication移動到父 package,要么必須為組件掃描指定正確的包。

@SpringBootApplication(scanBasePackages={"com.example"})

我建議將 class 移動到父 package 並讓 spring 引導發揮作用。

暫無
暫無

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

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