简体   繁体   English

自动连线导致Spring Boot应用程序出现异常

[英]Autowired causing exceptions in spring boot application

I am having trouble getting the Autowired annotation to work in my spring boot application. 我在使Springwire应用程序中的Autowired注释无法正常工作时遇到麻烦。

The error that I am receiving is "Could not autowire field. No qualifying bean of type" 我收到的错误是“无法自动装配字段。没有合格的类型的bean”

I have verified that the code from the Controller on down are not POJO and have Spring annotations. 我已经验证了从Controller向下的代码不是POJO并具有Spring注释。

I am also unable to run my main method outside of a package. 我也无法在包外运行我的main方法。 Any suggestions? 有什么建议么?

package com.xxx.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class SampleWebJspApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SampleWebJspApplication.class);
    }

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

}

UserService class UserService类

package com.xxx.service;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import com.xxx.entity.User;
    import com.xxx.repository.UserRepository;

    @Service
    public class UserService {

        @Autowired
        private UserRepository userRepository;

        public List<User> findAll() {
            return userRepository.findAll();
        }

    }

UserRepository class UserRepository类

package com.xxx.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.xxx.entity.User;

public interface UserRepository extends JpaRepository<User, Integer> {

}

There could be two possible reasons for this: 可能有两个原因:

  • Package of component is not included in @ComponentScan packages. @ComponentScan程序包中不包含组件程序包。
  • Package of repository is not included in @EnableJPARepositories packages. @EnableJPARepositories软件包中不包含存储库软件包。

Your @SpringBootApplication annotation is in the package "com.xxx.controller", so Spring will be able to scan components from this package to packages inside it. 您的@SpringBootApplication批注位于包“ com.xxx.controller”中,因此Spring可以将组件从该包扫描到其中的包中。

Docs: If specific packages are not defined, scanning will occur from the package of the class that declares this annotation. Docs:如果未定义特定的程序包,则将从声明此注释的类的程序包中进行扫描。

Try: 尝试:

  • Add @ComponentScan({"com.xxx"}) under @SpringBootApplication or modify to @SpringBootApplication(scanBasePackages = {"com.xxx"}). 在@SpringBootApplication下添加@ComponentScan({“ com.xxx”})或修改为@SpringBootApplication(scanBasePackages = {“ com.xxx”})。

or 要么

  • Just move your class "SampleWebJspApplication" to the package "com.xxx". 只需将您的类“ SampleWebJspApplication”移动到包“ com.xxx”。 This is probably the best solution, once this class of yours doesn't look like a controller. 一旦您的此类不像控制器,这可能是最好的解决方案。

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

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