考虑在配置中定义一个'package'类型的bean [Spring-Boot]

时间:2021-03-07 05:11:42

I am getting the following error:

我收到以下错误:

***************************
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.

I have never seen this error before but it's odd that the @Autowire is not working. Here is the project structure:

我之前从未见过这个错误,但@Autowire无法正常工作很奇怪。这是项目结构:

Applicant Interface

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

Now I should be able to just Autowire Applicant and be able to access, however in this case it is not working when I call it in my @RestController:

现在我应该可以只使用Autowire Applicant并且能够访问,但是在这种情况下,当我在@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";
    }

}

------------------------UPDATE 1-----------------------

I added

@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);
    }

}

and the error went away but nothing happened. However when I commented out everything dealing with Applicant in the RestController prior to adding @ComponentScan() I was able to return a string the UI, thus meaning my RestController was working, now it is being skipped. I ugly Whitelabel Error Page now.

而错误消失但没有任何反应。然而,当我在添加@ComponentScan()之前在RestController中注释掉与Applicant有关的所有内容时,我能够返回一个字符串UI,这意味着我的RestController正在工作,现在它被跳过了。我现在丑陋的Whitelabel错误页面。

---------------------UPDATE 2------------------------------

I added the base package of the bean it was complaining about. Error reads:

我添加了它抱怨的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.

I added @ComponentScan

我添加了@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);
    }

}

----------------------------Update 3----------------------

adding:

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

still is complaining about my ApplicantImpl class which @Autowires my repo TApplicantRepository into it.

仍在抱怨我的ApplicantImpl类@Autowires我的repo TApplicantRepository。

9 个解决方案

#1


80  

It might be because the project has been broken down into different modules.

这可能是因为该项目已被分解为不同的模块。

@SpringBootApplication
@ComponentScan({"com.delivery.request"})
@EntityScan("com.delivery.domain")
@EnableJpaRepositories("com.delivery.repository")
public class WebServiceApplication extends SpringBootServletInitializer {

#2


26  

Your Applicant class is not scanned it seems. By default all packages starting with the root as the class where you have put @SpringBootApplication will be scanned.

您的申请人类似乎没有扫描。默认情况下,将扫描以root为开头的所有包作为放置@SpringBootApplication的类。

suppose your main class "WebServiceApplication" is in "com.service.something", then all components that fall under "com.service.something" is scanned, and "com.service.applicant" will not be scanned.

假设您的主类“WebServiceApplication”位于“com.service.something”中,则扫描属于“com.service.something”的所有组件,并且不扫描“com.service.applicant”。

You can either restructure your packages such that "WebServiceApplication" falls under a root package and all other components becomes part of that root package. Or you can include @SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"}) etc such that "ALL" components are scanned and initialized in the spring container.

您可以重新构建包,使“WebServiceApplication”属于根包,所有其他组件都成为该根包的一部分。或者您可以包含@SpringBootApplication(scanBasePackages = {“com.service.something”,“com.service.application”})等,以便在弹簧容器中扫描并初始化“ALL”组件。

Update based on comment

根据评论更新

If you have multiple modules that are being managed by maven/gradle, all spring needs is the package to scan. You tell spring to scan "com.module1" and you have another module which has its root package name as "com.module2", those components wont be scanned. You can even tell spring to scan "com" which will then scan all components in "com.module1." and "com.module2."

如果您有多个由maven / gradle管理的模块,则所有弹簧需求都是要扫描的包。你告诉spring扫描“com.module1”,你有另一个模块,其根包名称为“com.module2”,这些组件不会被扫描。您甚至可以告诉spring扫描“com”,然后扫描“com.module1”中的所有组件。和“com.module2。”

#3


15  

There is a chance...
You might be missing @Service, @Repository annotation on your respective implementation classes.

有可能......您可能会在各自的实现类上缺少@Service,@ Repository注释。

#4


10  

Basically this happens when you have your Class Application in "another package". For example:

基本上,当您在“另一个包”中使用类应用程序时会发生这种情况。例如:

com.server
 - Applicacion.class (<--this class have @ComponentScan)
com.server.config
 - MongoConfig.class 
com.server.repository
 - UserRepository

I solve the problem with this in the Application.class

我在Application.class中解决了这个问题

@SpringBootApplication
@ComponentScan ({"com.server", "com.server.config"})
@EnableMongoRepositories ("com.server.repository") // this fix the problem

Another less elegant way is to: put all the configuration classes in the same package.

另一种不太优雅的方法是:将所有配置类放在同一个包中。

#5


3  

I think you can make it simplified by annotated your repository with @Repository, then it will be enabled automatically by Spring Framework.

我认为您可以通过使用@Repository注释您的存储库来简化它,然后它将由Spring Framework自动启用。

#6


1  

I sought online for an answer but it seems there is no one proper solution to my case: At the very beginning, everything works well as follows:

我在网上寻求答案,但似乎没有一个适当的解决方案我的情况:在一开始,一切运作良好如下:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
}

Then I am trying to add a map to cache something and it becomes this:

然后我试图添加一个地图来缓存一些东西,它变成了这个:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
    Map<String, String> testMap;
}

Boom!

Description:

Parameter 4 of constructor in *.GroupService required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

I removed the @AllArgsConstructor(onConstructor = @__(@Autowired)) and add @Autowired for each repository and service except the Map<String, String>. It just works as before.

我删除了@AllArgsConstructor(onConstructor = @__(@ Autowired))并为每个存储库和服务添加@Autowired,但Map 除外。它像以前一样工作。 ,string>

@Slf4j
@Service
public class SecurityGroupService {
    @Autowired
    private Repository repository;
    @Autowired
    private Service service;
    Map<String, String> testMap;
}

Hope this might be helpful.

希望这可能会有所帮助。

#7


1  

This can also happen if you are using Lombok and you add the @RequiredArgsConstructor and @NonNull for fields but some of your fields are not to be injected in the constructor. This is only one of the possibilities to get the the same error.

如果您使用Lombok并且为字段添加@RequiredArgsConstructor和@NonNull,但是您的某些字段不会在构造函数中注入,也会发生这种情况。这只是获得相同错误的可能性之一。

parameter 0 required a bean of type MissingBeanName that could not be found

参数0需要找不到找不到的MissingBeanName类型的bean

In my case the error told me what Controller the problem was in, after removing @NonNull the application started fine

在我的情况下,错误告诉我问题所在的控制器,删除@NonNull后,应用程序启动正常

#8


0  

This can happen if the @Service class is marked abstract.

如果@Service类标记为abstract,则会发生这种情况。

#9


-2  

@SpringBootApplication
@MapperScan("com.developer.project.mapper")

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

#1


80  

It might be because the project has been broken down into different modules.

这可能是因为该项目已被分解为不同的模块。

@SpringBootApplication
@ComponentScan({"com.delivery.request"})
@EntityScan("com.delivery.domain")
@EnableJpaRepositories("com.delivery.repository")
public class WebServiceApplication extends SpringBootServletInitializer {

#2


26  

Your Applicant class is not scanned it seems. By default all packages starting with the root as the class where you have put @SpringBootApplication will be scanned.

您的申请人类似乎没有扫描。默认情况下,将扫描以root为开头的所有包作为放置@SpringBootApplication的类。

suppose your main class "WebServiceApplication" is in "com.service.something", then all components that fall under "com.service.something" is scanned, and "com.service.applicant" will not be scanned.

假设您的主类“WebServiceApplication”位于“com.service.something”中,则扫描属于“com.service.something”的所有组件,并且不扫描“com.service.applicant”。

You can either restructure your packages such that "WebServiceApplication" falls under a root package and all other components becomes part of that root package. Or you can include @SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"}) etc such that "ALL" components are scanned and initialized in the spring container.

您可以重新构建包,使“WebServiceApplication”属于根包,所有其他组件都成为该根包的一部分。或者您可以包含@SpringBootApplication(scanBasePackages = {“com.service.something”,“com.service.application”})等,以便在弹簧容器中扫描并初始化“ALL”组件。

Update based on comment

根据评论更新

If you have multiple modules that are being managed by maven/gradle, all spring needs is the package to scan. You tell spring to scan "com.module1" and you have another module which has its root package name as "com.module2", those components wont be scanned. You can even tell spring to scan "com" which will then scan all components in "com.module1." and "com.module2."

如果您有多个由maven / gradle管理的模块,则所有弹簧需求都是要扫描的包。你告诉spring扫描“com.module1”,你有另一个模块,其根包名称为“com.module2”,这些组件不会被扫描。您甚至可以告诉spring扫描“com”,然后扫描“com.module1”中的所有组件。和“com.module2。”

#3


15  

There is a chance...
You might be missing @Service, @Repository annotation on your respective implementation classes.

有可能......您可能会在各自的实现类上缺少@Service,@ Repository注释。

#4


10  

Basically this happens when you have your Class Application in "another package". For example:

基本上,当您在“另一个包”中使用类应用程序时会发生这种情况。例如:

com.server
 - Applicacion.class (<--this class have @ComponentScan)
com.server.config
 - MongoConfig.class 
com.server.repository
 - UserRepository

I solve the problem with this in the Application.class

我在Application.class中解决了这个问题

@SpringBootApplication
@ComponentScan ({"com.server", "com.server.config"})
@EnableMongoRepositories ("com.server.repository") // this fix the problem

Another less elegant way is to: put all the configuration classes in the same package.

另一种不太优雅的方法是:将所有配置类放在同一个包中。

#5


3  

I think you can make it simplified by annotated your repository with @Repository, then it will be enabled automatically by Spring Framework.

我认为您可以通过使用@Repository注释您的存储库来简化它,然后它将由Spring Framework自动启用。

#6


1  

I sought online for an answer but it seems there is no one proper solution to my case: At the very beginning, everything works well as follows:

我在网上寻求答案,但似乎没有一个适当的解决方案我的情况:在一开始,一切运作良好如下:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
}

Then I am trying to add a map to cache something and it becomes this:

然后我试图添加一个地图来缓存一些东西,它变成了这个:

@Slf4j
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GroupService {
    private Repository repository;
    private Service service;
    Map<String, String> testMap;
}

Boom!

Description:

Parameter 4 of constructor in *.GroupService required a bean of type 'java.lang.String' that could not be found.


Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

I removed the @AllArgsConstructor(onConstructor = @__(@Autowired)) and add @Autowired for each repository and service except the Map<String, String>. It just works as before.

我删除了@AllArgsConstructor(onConstructor = @__(@ Autowired))并为每个存储库和服务添加@Autowired,但Map 除外。它像以前一样工作。 ,string>

@Slf4j
@Service
public class SecurityGroupService {
    @Autowired
    private Repository repository;
    @Autowired
    private Service service;
    Map<String, String> testMap;
}

Hope this might be helpful.

希望这可能会有所帮助。

#7


1  

This can also happen if you are using Lombok and you add the @RequiredArgsConstructor and @NonNull for fields but some of your fields are not to be injected in the constructor. This is only one of the possibilities to get the the same error.

如果您使用Lombok并且为字段添加@RequiredArgsConstructor和@NonNull,但是您的某些字段不会在构造函数中注入,也会发生这种情况。这只是获得相同错误的可能性之一。

parameter 0 required a bean of type MissingBeanName that could not be found

参数0需要找不到找不到的MissingBeanName类型的bean

In my case the error told me what Controller the problem was in, after removing @NonNull the application started fine

在我的情况下,错误告诉我问题所在的控制器,删除@NonNull后,应用程序启动正常

#8


0  

This can happen if the @Service class is marked abstract.

如果@Service类标记为abstract,则会发生这种情况。

#9


-2  

@SpringBootApplication
@MapperScan("com.developer.project.mapper")

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