简体   繁体   中英

Spring-boot:bean could not be found

I am new to spring boot and i am writing CRUD operation for basic practices, here is my code.

DemoApplication.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);
  }

}

User.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;
}

}

UserServices.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";

    }
 }

ServiceController.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);
  }
}

above code giving me the error Field userServices in com.example.controller.ServiceController required a bean of type 'com.example.services.UserServices' that could not be found.

This is because your DemoApplication is defined in he following package com.example.controller . Thus by default Spring will only scan that package and desendence of it. Eg com.example.controller.something . It will not scan in parent packages.

Either you move your DemoApplication to the parent package or you have to specify the correct packages for component-scan.

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

I suggest to move the class to the parent package and let spring boot do the magic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM