简体   繁体   English

创建 Spring Boot 应用程序时出错

[英]Error while creating Spring Boot Application

I am trying to make a spring boot application and connect it with mongodb in a multi module project.我正在尝试制作一个 Spring Boot 应用程序并将其与多模块项目中的 mongodb 连接。 But I am not able to setup the springboot properly.但我无法正确设置 springboot。 I am getting error when I run the application.运行应用程序时出现错误。

Project Structure:项目结构:

--MainModule

------SubModule

-----------Controller(UsersController.java)

-----------Main(UserApplication.java)

-----------Model(Users.java and Address.java)

-----------Repository(UsersRepository.java)

-----------Service(UsersService.java)

UserApplication.java用户应用程序

@SpringBootApplication
@ComponentScan({"repository","model","service"})
@EnableMongoRepositories("repository.UsersRepository")
public class UsersApplication {

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

UsersController.java用户控制器.java

 @RestController
    class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping(value = "/create" ,  method = RequestMethod.POST)
    public Users create(@RequestBody Users user) {

        return usersService.createUser(user);
    }

    @RequestMapping("/get/{id}")
    public Optional<Users> getUser(@PathVariable int id) {
        return usersService.getById(id);
    }
    @RequestMapping("/getAll")
    public List<Users> getAll(){
        return usersService.getAll();
    }
    @RequestMapping("/update/{id}")
    public Users update(@RequestBody Users user) {
        Users u = usersService.updateUser(user);
        return u;
    }
    @RequestMapping("/delete")
    public String delete(@PathVariable int id) {
        usersService.deleteUser(id);
        return "Deleted "+id;
    }
    @RequestMapping("/error")
    public String err(){return "error";}
}

UsersRepository.java用户存储库.java

@Repository
public interface UsersRepository extends MongoRepository<Users, Integer> {


}

UsersService.java用户服务.java

@Service
public class UsersService {

    @Autowired
    private UsersRepository usersRepository;

    //create a user
    public Users createUser(Users user)
    {
        return usersRepository.save(user);
    }

    //retrive all users
    public List<Users> getAll()
    {
        return usersRepository.findAll();

    }

    //retrive a user
    public Optional<Users> getById(int id)
    {
        return usersRepository.findById(id);
    }

    //update a user
    public Users updateUser(Users user)
    {
        Optional<Users> u = usersRepository.findById(user.getId());
        if(u.isPresent())
        {
            u.get().setName(user.getName());
            u.get().setAge(user.getAge());
            u.get().setAddress(user.getAddress());
        }
        return usersRepository.save(u.get());
    }

    //delete a user
    public void deleteUser(int id)
    {
        Optional<Users> user = usersRepository.findById(id);
        if(user.isPresent())
        {
            usersRepository.delete(user.get());
        }

    }
}

Users.java用户.java

@Document
public class Users {

    @Id
    int id;
    String name;
    int age;
    Address address;
    public Users(int id, String name, int age, Address address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }


}

Address.java地址.java

public class Address {

    private String mainAddress;
    private double lat;
    private double lon;
    private long pincode;
    public Address()
    {

    }
    public Address(String mainAddress) {
        super();
        this.mainAddress = mainAddress;
        this.lat = 0;
        this.lon = 0;
        this.pincode = 000000;
    }

    public Address(String mainAddress, double lat, double lon, long pincode) {
        super();
        this.mainAddress = mainAddress;
        this.lat = lat;
        this.lon = lon;
        this.pincode = pincode;
    }
    public String getMainAddress() {
        return mainAddress;
    }
    public void setMainAddress(String mainAddress) {
        this.mainAddress = mainAddress;
    }
    public double getLat() {
        return lat;
    }
    public void setLat(int lat) {
        this.lat = lat;
    }
    public double getLon() {
        return lon;
    }
    public void setLon(int lon) {
        this.lon = lon;
    }
    public long getPincode() {
        return pincode;
    }
    public void setPincode(long pincode) {
        this.pincode = pincode;
    }

}

Error错误

019-03-18 10:55:21.020  WARN 14486 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersService': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repository.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-03-18 10:55:21.024  INFO 14486 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-03-18 10:55:21.044  INFO 14486 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-18 10:55:21.148 ERROR 14486 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field usersRepository in service.UsersService required a bean of type 'repository.UsersRepository' that could not be found.

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


Action:

Consider defining a bean of type 'repository.UsersRepository' in your configuration.
--MainModule

------SubModule

------Main(UserApplication.java)

-----------Controller(UsersController.java)

-----------Model(Users.java and Address.java)

-----------Repository(UsersRepository.java)

-----------Service(UsersService.java)

EnableMongoRepositories takes basePackages as a parameter. EnableMongoRepositoriesbasePackages作为参数。 It lists packages to scan for repositories, try with this:它列出了要扫描存储库的包,请尝试以下操作:

@EnableMongoRepositories("repository")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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