Spring Boot 多模块项目跨包自动注入的方法,解决SpringBoot引用别的模块无法注入的问题。
在使用 Maven 多模块开发的时候,A模块引入B模块,却无法注入B模块中被@Service、@Mapper、@Compoment、@Configuration 等注解修饰的类。
SpringBootApplication启动类默认会扫描该启动类所在的包及其子包。
Spring Boot启动类通常使用@SpringBootApplication注解,该注解是一个组合注解,包含了@ComponentScan,@EnableAutoConfiguration和@Configuration等。@ComponentScan会扫描该类所在的包及其子包中的Spring组件(如@Component, @Service, @Repository等),如果不指定basePackages,则默认会扫描该启动类所在的包及其子包。
解决方法1【推荐】:确保两个模块的启动类包路径一致性(com.es)
解决方法2: 利用@SpringBootApplication的scanBasePackages 属性指定包的所有扫描路径,例如"com"。
@SpringBootApplication(scanBasePackages = {"com.zkn","com.st."})
或者使用@ComponentScan的value属性指定包的扫描路径
@ComponentScan(value = {"com.zkn","com.st"})
@ComponentScan(basePackages = {"com.example.*"})
其他解决方案:编写自己的starter项目
子模块
<modules>
<module>Common</module>
<module>Mybatis</module>
<module>ES</module>
<module>push_http</module>
</modules>
A模块引入B模块
<dependency>
<groupId>org.example</groupId>
<artifactId>Common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Starter开发步骤:
在Spring Boot3中,传统的spring.factories不生效。
Spring Boot3多模块项目跨包自动注入的方法,快速编写自己的starter项目。
package com.commons.spring;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.commons")
@SpringBootConfiguration
public class BeanConfigScanConfig{
}
META-INF/spring/
,并放入org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件,然后把第二步写的Configuration类路径放进去即可。com.commons.spring.BeanConfigScanConfig
自动连接数据源/ES、自动建立定时任务、关闭的时候自动清理外部缓存
package com.commons.starter;
import jakarta.annotation.Resource;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
@Configuration
public class CommonsStarter implements ApplicationListener {
@Resource
ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ApplicationStartedEvent) {
// 这里编写你启动时要初始化的代码
}
// 还可以接管其他的生命周期:详细可以搜百度,这里不做赘述
// ApplicationContextInitializedEvent
// ApplicationEnvironmentPreparedEvent
// ApplicationFailedEvent
// ApplicationPreparedEvent
// ApplicationReadyEvent
// ApplicationStartedEvent
// ApplicationStartingEvent
// EventPublishingRunListener
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]”。
原因:Java新版本禁用了些老的加密算法引起的
解决方案1:兼容SQL Server 2005,在连接数据库时,url后面加上一个encrypt=false或者encrypt=true;trustServerCertificate=true
String dbURL="jdbc:sqlserver://localhost:1433;databaseName=TestDB;encrypt=false";
解决方案2:找到jdk的安装目录/Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home/jre/lib/security
下的java.security改安全协议的配置即可。删掉:TLSv1、TLSv1.1
。
jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
include jdk.disabled.namedCurves
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有