Spring Boot和多模块Maven项目

8

我正在努力正确设置一个多模块Spring Boot Maven项目。 有没有示例项目可以参考? 目前我将我的项目结构化如下。

Data模块: 基本上是我的数据层。 包括我的POJO和数据库repo接口(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控制器,当发送短信到特定的电话号码时将被调用。它将短信存储在数据库中,并使用cdata模块中的repo接口之一来实现。该模块正在构建为可执行的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控制器类无法自动装配来自我的数据模块的bean。我看到一些帖子说你应该将包名添加到@ComponentScan注释中。例如:
@ComponentScan("com.example.data")
这样做可以让嵌入式Tomcat服务器启动,但是我认为仅添加该包会导致Spring无法在我的文本模块中找到REST控制器,因为当我访问API时,我得到404错误。所以我也添加了我的文本包:
@ComponentScan({"com.example.data","com.example.text"})
然而,这又使我回到了同样的错误,Spring无法从数据模块中找到我的bean以将其自动装配到我的REST控制器中。
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

有没有人能提供如何正确地完成这个任务的指南?
3个回答

5
仓库会自动从应用程序类的包中进行扫描(即那些被注释为@EnableAutoConfiguration的类)。如果这个默认值不适合您,您可以轻松地回退到使用关联包的@EnableJpaRepositories
我看到了com.example.datacom.example.text。我猜您可能有一个特定于项目的包,因为com.example可能太广泛了。所以修复它的一种方法是将您的应用程序放在com.example(或者您的应用程序的根包)中。
还请查看文档

0

验证Beans注解,要求在Application类中具有父注解,例如@Transactional - 父级:@EnableTransactionManagement。

其中一些提供AOP和代理对象的注解会引起Bean注入生命周期失败,如果它们错误或缺失。我曾经在JPA和REST多模块项目中遇到过这种情况(例如认为是多模块组件扫描失败)。

Beans解析层次结构

@Controller <- @Service <- @Component(Repository/Aggregation Facade)<- @Repository


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接