使用gradle和springboot进行多模块开发,只有主模块有Application.class的main方法,其他模块没有main方法,那么其他模块的bean是怎么加载的呢,其他模块的application.yml中的配置会加载吗?
时间: 2024-04-24 09:22:39 浏览: 16
在使用gradle和springboot进行多模块开发时,其他模块的bean是通过主模块中的扫描机制进行加载的。可以通过在主模块的Application类上使用`@ComponentScan`注解,指定其他模块中需要扫描的包路径,来实现其他模块bean的加载。同时,其他模块的application.yml中的配置也会被加载。可以通过在主模块的application.yml中使用`spring.config.import`属性,指定其他模块的配置文件路径,来实现其他模块的配置加载。需要注意的是,其他模块中的配置文件需要命名为`application-{module}.yml`的格式,其中`{module}`为模块名称。
相关问题
使用gradle和springboot进行多模块开发,只有主模块有Application.class的main方法,且主模块有2个DataSource数据源,每个模块都有各自的mybatis的mapper文件,怎么指定各模块的mapper文件与数据源的对应关系呢?
可以使用SpringBoot的多数据源配置,同时在每个模块的配置文件中指定对应的数据源和mapper文件位置。
首先,在主模块的配置文件中定义两个数据源,例如:
```
spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
```
接着,在主模块中定义两个数据源的bean:
```java
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
然后,在每个模块的配置文件中指定对应的数据源和mapper文件位置,例如:
```
mybatis.mapper-locations=classpath:mapper/module1/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.name=primaryDataSource
```
```
mybatis.mapper-locations=classpath:mapper/module2/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.name=secondaryDataSource
```
最后,在每个模块中定义对应的SqlSessionFactoryBean和MapperScannerConfigurer:
```java
@Configuration
public class Module1Config {
@Autowired
@Qualifier("primaryDataSource")
private DataSource dataSource;
@Bean(name = "module1SqlSessionFactory")
public SqlSessionFactoryBean module1SqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/module1/*.xml"));
return sessionFactory;
}
@Bean(name = "module1MapperScannerConfigurer")
public MapperScannerConfigurer module1MapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.module1.mapper");
scannerConfigurer.setSqlSessionFactoryBeanName("module1SqlSessionFactory");
return scannerConfigurer;
}
}
```
```java
@Configuration
public class Module2Config {
@Autowired
@Qualifier("secondaryDataSource")
private DataSource dataSource;
@Bean(name = "module2SqlSessionFactory")
public SqlSessionFactoryBean module2SqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/module2/*.xml"));
return sessionFactory;
}
@Bean(name = "module2MapperScannerConfigurer")
public MapperScannerConfigurer module2MapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.module2.mapper");
scannerConfigurer.setSqlSessionFactoryBeanName("module2SqlSessionFactory");
return scannerConfigurer;
}
}
```
这样就可以实现多数据源和多模块的mybatis配置了。
使用gradle和springboot进行多模块开发,如果每个模块都有各自的mybatis的mapper、
entity、service、controller等组件,如何进行统一管理和依赖注入?
可以通过使用Spring Boot的自动化配置(auto-configuration)和自动化装配(auto-wiring)功能来实现统一管理和依赖注入。
首先,在每个模块的build.gradle文件中,需要添加对mybatis的依赖:
```groovy
dependencies {
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
//其他依赖
}
```
然后,在每个模块中创建mapper、entity、service、controller等组件,可以使用Spring的注解来标记它们,例如:
```java
@Repository
public interface UserMapper {
//查询用户
User selectUserById(Integer id);
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
//查询用户
public User getUserById(Integer id) {
return userMapper.selectUserById(id);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
//查询用户
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id) {
return userService.getUserById(id);
}
}
```
最后,在每个模块的启动类中,需要添加@EnableAutoConfiguration注解来启用Spring Boot的自动化配置功能:
```java
@SpringBootApplication
@EnableAutoConfiguration
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
这样,每个模块的组件就可以通过自动化装配的方式进行依赖注入,而且所有模块的mybatis的mapper、entity、service、controller等组件也都可以进行统一管理。