当前位置: 首页 > 知识库问答 >
问题:

如何在Spring boot应用程序中注册所有子包中的bean?

乐正翰
2023-03-14

我正在创建一个新的spring bootjpathymeleaf应用程序,我使用了许多放置在子包中的模块。我有类似于ge的结构。我的project我的主类和@SpringBootApplication放在哪里,在这个包下我有子包ge。我的项目daoge。我的项目服务ge。我的项目控制器等等,我的豆子放在哪里@SpringBootApplication只扫描base(ge.my.project)包下的bean,但我想扫描所有子包中的bean

我尝试了许多变体来扫描子包

@ComponentScan(basePackages = {"ge.my.project.controller","ge.my.project.service","ge.my.project.configuration"})

@ComponentScan({"ge.my.project.controller","ge.my.project.service","ge.my.project.configuration"})

@ComponentScan(“ge.my.project.*)

但是什么都不管用,当我试图使用@Autowired注入bean时,我遇到了这样的错误请考虑定义一个“ge”类型的bean。我的项目服务配置中的ServiceTypeService

这是我的主要课程

package ge.my.project;

import ge.ufc.inhouseProjects.controller.ServiceTypeController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


 @SpringBootApplication
//@ComponentScan(basePackages = {"ge.my.project.controller","ge.my.project.service","ge.my.project.configuration"})
@ComponentScan({"ge.my.project.*"})
@EntityScan({"ge.my.project.entity"})
@EnableJpaRepositories("ge.my.project.dao")
public class InhouseProjectsApplication {

public static void main(String[] args) {
    /*ApplicationContext applicationContext = */SpringApplication.run(InhouseProjectsApplication.class, args);
}

}

这是我的全部项目https://github.com/JavaGeoGroup/inhouseProjects.git

在Spring启动应用程序中扫描所有项目bean的最清晰方法是什么?


共有1个答案

郤坚诚
2023-03-14

您需要注释实现,而不是接口。因此,在您的情况下,@Service注释需要在ServiceTypeDaoImpl类上设置,而不是在ServiceTypeDao接口上设置。

您需要使用@Component注释或其子注释之一(如@Repository、@Service、@Controller、@Configuration)来注释bean(接口的实现,而不是spring data repo接口)。

从@Component的javadoc:

 * Indicates that an annotated class is a "component".
 * Such classes are considered as candidates for auto-detection
 * when using annotation-based configuration and classpath scanning.
 *
 * <p>Other class-level annotations may be considered as identifying
 * a component as well, typically a special kind of component:
 * e.g. the {@link Repository @Repository} annotation or AspectJ's
 * {@link org.aspectj.lang.annotation.Aspect @Aspect} annotation.

代码的特定扩展示例:Springbeans以一个小的大小写开头,因此只要您不在“@Service”注释中设置特定的名称,您的接口“ServiceTypeDao”就会注册为“ServiceTypeDao”。

 类似资料:
  • 该应用程序在Eureka注册后以“未知”的状态启动,并且从未更改为“启动”。如果这些标志中的任何一个被设置为false,那么应用程序启动时注册为“up”。 当两个标志都启用/true时,我看到了以下一系列日志记录: 我如何让应用程序注册为启用了这两个标志/真? bootstrap.yml

  • 我正在尝试使用node.js和Microsoft图形API创建Office 365应用程序。我一直在遵循这个运行良好的示例:github.com/microsoftgraph/nodejs-connect-rest-sample 然而,我在使用微软图形应用编程接口访问Azure广告时遇到了严重问题。我正在尝试执行和,但我得到了403禁止。获取工作正常。我希望能够在Office 365租户的Azur

  • 我有Kafka Streams java应用程序启动并运行。我试图使用KSQL创建简单的查询,并使用Kafka流来实现复杂的解决方案。我希望将KSQL和Kafka流作为Java应用程序运行。 我打算通过https://github.com/confluentinc/ksql/blob/master/ksqldb-examples/src/main/java/io/confluent/ksql/em

  • 问题内容: 我想在Spring web.xml中定义两个servlet-一个用于应用程序html / jsp页面,另一个用于将由外部应用程序调用的Web服务。这是web.xml: 如果我让我的servlet单独在文件中使用DispatcherServlet,它可以正常工作。如果我的用户Web服务的配置文件(user-service- servlet.xml)具有context- param,则可以

  • 要获取请求URL,可以在堆栈溢出中找到以下方法。 第一种方法: 第二种方法: 第三种方法: 我不知道在spring boot应用程序中使用哪一个来获取请求URL。 如果我使用第三种方法,那么我是否需要在配置类中创建RequestContextListener的bean,如下所示?

  • 我正试图在Tomcat 7.0.34.0上使用Servlet3.0应用程序类开发一个RESTful webapp,但似乎没有任何工作。我对以前的应用程序使用Jersey ServletContainer方法并在web.xml中声明所有REST服务没有任何问题。 我的当前应用程序子类: 和web服务: 当我尝试部署应用程序时,Tomcat告诉我上下文已重新加载: web应用程序本身已经部署(我可以访