我得到以下错误:

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

Description:

Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.


Action:

Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.

我以前从未见过这个错误,但奇怪的是@Autowire不工作。项目结构如下:

申请人接口

public interface Applicant {

    TApplicant findBySSN(String ssn) throws ServletException;

    void deleteByssn(String ssn) throws ServletException;

    void createApplicant(TApplicant tApplicant) throws ServletException;

    void updateApplicant(TApplicant tApplicant) throws ServletException;

    List<TApplicant> getAllApplicants() throws ServletException;
}

ApplicantImpl

@Service
@Transactional
public class ApplicantImpl implements Applicant {

private static Log log = LogFactory.getLog(ApplicantImpl.class);

    private TApplicantRepository applicantRepo;

@Override
    public List<TApplicant> getAllApplicants() throws ServletException {

        List<TApplicant> applicantList = applicantRepo.findAll();

        return applicantList;
    }
}

现在我应该能够自动连线申请人,并能够访问,但在这种情况下,当我在@RestController中调用它时,它是不工作的:

@RestController
public class RequestController extends LoggingAware {

    private Applicant applicant;

    @Autowired
    public void setApplicant(Applicant applicant){
        this.applicant = applicant;
    }

    @RequestMapping(value="/", method = RequestMethod.GET)
    public String helloWorld() {

        try {
            List<TApplicant> applicantList = applicant.getAllApplicants();

            for (TApplicant tApplicant : applicantList){
                System.out.println("Name: "+tApplicant.getIndivName()+" SSN "+tApplicant.getIndSsn());
            }

            return "home";
        }
        catch (ServletException e) {
            e.printStackTrace();
        }

        return "error";
    }

}

------------------------ 更新1 -----------------------

我添加了

@SpringBootApplication
@ComponentScan("module-service")
public class WebServiceApplication extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebServiceApplication.class);
    }

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

}

错误消失了,但什么也没发生。然而,当我在添加@ComponentScan()之前注释掉所有与RestController中的申请人相关的内容时,我能够返回一个字符串的UI,从而意味着我的RestController正在工作,现在它被跳过了。我丑陋的白色标签错误页现在。

--------------------- 更新2 ------------------------------

我添加了它所抱怨的bean的基本包。错误读取:

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

Description:

Parameter 0 of method setApplicantRepo in com.service.applicant.ApplicantImpl required a bean of type 'com.delivery.service.request.repository.TApplicantRepository' that could not be found.


Action:

Consider defining a bean of type 'com.delivery.request.request.repository.TApplicantRepository' in your configuration.

我添加了@ComponentScan

@SpringBootApplication
@ComponentScan({"com.delivery.service","com.delivery.request"})
public class WebServiceApplication extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WebServiceApplication.class);
    }

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

}

---------------------------- 更新3 ----------------------

添加:

@SpringBootApplication
@ComponentScan("com")
public class WebServiceApplication extends SpringBootServletInitializer {

仍然在抱怨我的ApplicantImpl类@自动连接我的repo TApplicantRepository到它。


当前回答

在我的例子中,这个问题是由错误的spring.autoconfigure.exclude属性值引起的。我有这个属性是因为我的Spring Boot项目最初没有连接到数据库,但后来我添加了我的第一个JpaRepository类,并试图在@RestController类中使用@Autowired注释声明它的一个实例。

线条在应用中。导致问题的属性:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

一旦我删除或注释掉这一行,我的Spring Boot服务就可以正常运行了。

其他回答

如果您的类依赖是由Spring管理的,那么如果我们忘记在POJO类中添加默认/空参数构造函数,则可能会发生此问题。

@Service应该从org.springframework.stereotype.Service导入

您可能是从org.jvnet.hk2.annotations.Service导入它

在CrudRepository中,检查实体类@Id是否使用import javax.persistence.Id;,而不是import org.springframework.data.annotation.Id;

我也遇到过同样的问题。Mongo DB存储库是由Spring引导标识的,但它并没有为扩展Mongo存储库的存储库接口创建Bean。

在我的案例中,问题是maven pom中“spring + mango”的版本规范不正确。我已经改变了神器的组id,这一切都像魔法一样有效。不需要注释,因为spring boot会处理所有事情。

在我解决问题的过程中,我在网上到处搜索解决方案,并意识到这个问题实际上是与项目配置相关的,任何面临这个问题的人都应该首先检查他们的项目设置,并从spring中启用调试,以获得更多关于失败的详细信息,并密切关注在这个过程中的确切位置,创建失败了。

在我的案例中,这两种选择都有效。

in //@ComponentScan ({"myapp", "myapp.resources","myapp.services"}) 还包括在列表中包含Application.class的包,或者 简单地添加@EnableAutoConfiguration;它会自动识别所有的春豆。

如果您意外地在两个不同的类中定义了相同的bean,也会得到这个错误。我就是这样。错误信息具有误导性。当我去掉多余的豆子时,问题就解决了。