欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot有多个模块(子工程),主工程调用需要模块的3处扫描手动加载

程序员文章站 2022-04-22 13:38:58
...

主工程调用需要模块的3处扫描手动加载

第一处,@ComponentScan:该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <context:component-scan>。

第二处,@MapperScan:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类。

第三处,mapper-locations:指定sql所在包。

主工程启动类BackOfficeApplication.java

"jp.co.pia.ticket.sports.hawks.seasonseat.backoffice":主工程组件扫描地址。

"jp.co.pia.ticket.sports.hawks.seasonseat.common":模块组件扫描地址。

"jp.co.pia.ticket.sports.hawks.seasonseat.backoffice.mapper":主工程mapper扫描地址。

"jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper":模块mapper扫描地址。

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@ComponentScan(basePackages = {"jp.co.pia.ticket.sports.hawks.seasonseat.backoffice","jp.co.pia.ticket.sports.hawks.seasonseat.common"})
@SpringBootApplication
@MapperScan({"jp.co.pia.ticket.sports.hawks.seasonseat.backoffice.mapper","jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper"})
public class BackOfficeApplication {

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

	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:messages_backend");
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}
}

工程配置文件

application.yml

classpath:/jp/co/pia/ticket/sports/hawks/seasonseat/backoffice/sql/*.xml:主工程sql扫描地址。

classpath:/jp/co/pia/ticket/sports/hawks/seasonseat/common/util/sql/*.xml:子工程sql扫描地址。

server:
  servlet:
    session:
      timeout: 1800

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/iij
    username: postgres
    password: 11111111
  jpa:
    open-in-view: false
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false
        dialect: org.hibernate.dialect.PostgreSQLDialect
  main:
    allow-bean-definition-overriding: true

mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:/jp/co/pia/ticket/sports/hawks/seasonseat/backoffice/sql/*.xml,classpath:/jp/co/pia/ticket/sports/hawks/seasonseat/common/util/sql/*.xml