Bean的Spring Boot自动装配在Maven多模块项目中不起作用 [英] Spring Boot Autowiring of beans is not working in maven multi module project

查看:883
本文介绍了Bean的Spring Boot自动装配在Maven多模块项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Spring Boot和Maven将我们的项目模块化为不同的独立Maven项目时,我们遇到了一个问题,即多模块Maven项目中的bean自动装配无法正常工作.

While modularising our project into different independent maven projects using spring boot and maven, we have came across a issue where autowiring of beans in multi module maven project is not working.

仅给您一个概述,下面是到目前为止开发的独立Maven项目

Just to give you an overview of the issue, below are the independent maven projects developed so far

  1. Coreservices –包含整个应用程序的spring boot域对象:输出JAR
  2. DBservices1-包含用于引导数据库的Spring Boot存储库和服务(数据库服务):输出JAR
  3. 奖励-包含与奖励模块相关的文件(控制器,服务(业务逻辑服务),视图):输出JAR
  4. RewardsApp-独立的可部署Maven项目:输出WAR

下面是依赖项结构 RewardsApp->奖励-> DBservices1-> Coreservices

Below is the dependency structure RewardsApp-> Rewards -> DBservices1 -> Coreservices

问题是Rewards中使用了@Autowired批注,并且RewardsApp Project中没有提供DBservices1来获取使用@Service/@Repository批注的映射服务.

The problem is @Autowired annotation used in Rewards and DBservices1 to fetch the mapped services annotated with @Service/@Repository are not available in RewardsApp Project.

作为一种解决方法,我们在RewardsApp中使用@Bean批注配置了bean,然后服务器可以使用这些服务以成功启动. 使用这种方法,我们需要手动配置相关项目中使用的RewardsApp中的所有bean. 我们的应用程序中有很多服务和存储库,我们认为像这样创建bean并不是一种正确的方法,因为需要创建许多bean.

As a workaround we have configured the beans in RewardsApp with @Bean annotation, then the services are available to the server to start successfully. With this approach we need to manually configure all the beans in RewardsApp used in dependent projects. We have many services and repositories in our application and we think creating beans like this not a proper way as many beans need to be created.

请注意,我们已经在以下所有项目中创建了所有spring boot控制器,服务,存储库 包com.company.application

Please note that we have created all the spring boot controllers,services,repositorys across all projects under package com.company.application

下面是主要课程的摘要:

Below is the snippet of main class:

@SpringBootApplication
@ComponentScan(basePackages = {"com.company.application"})
public class RewardsApp extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(RewardsApp.class);
    }
}

/**
*Manual beans in  RewardsApp
**/
@Bean
public SomeService someService()
{
    return new SomeService();
}

推荐答案

通过在RewardsApp.java中添加以下注释,对我来说是成功的诀窍,现在自动装配正在罐中的类中工作

By adding below annotation in RewardsApp.java did the trick for me, now autowiring was working for the classes inside the jars

@ComponentScan(basePackages = {"com.company"})
@EntityScan(basePackages = {"com.company"})
@EnableJpaRepositories(basePackages = {"com.company"})

我猜上面是关于服务,实体(域),存储库的

I guess above are for Services,Entities(Domains),Repositories

这篇关于Bean的Spring Boot自动装配在Maven多模块项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆