gpt4 book ai didi

Spring Boot 和多模块 Maven 项目

转载 作者:行者123 更新时间:2023-12-03 14:32:58 25 4
gpt4 key购买 nike

我正在努力正确设置多模块 Spring Boot Maven 项目。有没有我可以引用的示例项目?目前我的项目结构是这样的。

数据模块:
基本上是我的数据层。是包含我的 POJO 和我的数据库 repo 接口(interface) (PagingAndSortingRepository)。该模块将是项目中其他模块的依赖项。目前我在模块中放置了一个配置类,如下所示

public class Config {

@Configuration
@Profile("cloud")
static class CloudConfiguration extends AbstractCloudConfig {
@Bean
public DataSource dataSource() {
return connectionFactory().dataSource("session-questions-sql", new DataSourceConfig(new PoolConfig(4, 4), new ConnectionConfig("")));
}

}


@Configuration
@Profile("default")
static class LocalConfiguration {
}
}

我认为这种配置在其他两个模块之间是通用的,因此它属于数据模块。

文本模块:
这是一个非常简单的模块,它包含一个 REST API Controller ,当短信发送到某个电话号码时将调用该 Controller 。它将文本消息存储在数据库中,并使用 cdata 模块中的一个 repo 接口(interface)来执行此操作。这个模块被构建为一个可执行的 jar 文件,并包含一个实现 EmbeddedServletContainerCustomizer 的类。
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
public class App implements EmbeddedServletContainerCustomizer {

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

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
//Enabled UTF-8 as the default character encoding for static HTML resources.
//If you would like to disable this comment out the 3 lines below or change
//the encoding to whatever you would like.
MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
mappings.add("html", "text/html;charset=utf-8");
container.setMimeMappings(mappings );
}
}

当我运行 jar 时,我收到错误消息,说 rest Controller 类无法从我的数据模块中 Autowiring bean。我看到一些帖子说您应该将包名称添加到 @ComponentScan 注释中。例如

@ComponentScan("com.example.data")

这样做将使嵌入式 tomcat 服务器启动,但我认为单独添加该包会导致 Spring 在我的文本模块中找不到我的 REST Controller ,因为我在访问 API 时收到 404。所以我也添加了我的文本包

@ComponentScan({"com.example.data","com.example.text"})

然而,这让我回到了同样的错误,Spring 无法从数据模块中找到我的 bean 来自动连接到我的 REST Controller 。
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'twilioController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.data.controllers.QuestionRepo com.example.questions.text.controller.TwilioController.questionRepo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.data.controllers.QuestionRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at com.example.text.App.main(App.java:34)
... 6 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.data.controllers.QuestionRepo com.example.text.controller.TwilioController.questionRepo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.data.controllers.QuestionRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.data.controllers.QuestionRepo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 24 more

有没有人对如何正确执行此操作有任何指示?

最佳答案

存储库是从您的应用程序类的包中自动扫描的(用 @EnableAutoConfiguration 注释的那个)。如果该默认设置不适合您,您可以轻松地退回到 @EnableJpaRepositories。与相关软件包。

我可以看到 com.example.datacom.example.text .我猜你可能有一个项目特定的包 com.example可能太宽泛了。因此,解决此问题的一种方法是将您的应用程序放入 com.example (或任何你的应用程序的根包)。

另请查看 the doc

关于Spring Boot 和多模块 Maven 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25071267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com