前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 多模块项目跨包自动注入的方法

Spring Boot 多模块项目跨包自动注入的方法

作者头像
公众号iOS逆向
发布2024-03-27 15:35:54
1.2K0
发布2024-03-27 15:35:54
举报
文章被收录于专栏:iOS逆向与安全iOS逆向与安全

引言

Spring Boot 多模块项目跨包自动注入的方法,解决SpringBoot引用别的模块无法注入的问题。

I Spring Boot 多模块项目跨包自动注入的方法

1.1 问题描述

在使用 Maven 多模块开发的时候,A模块引入B模块,却无法注入B模块中被@Service、@Mapper、@Compoment、@Configuration 等注解修饰的类。

1.2 原因

SpringBootApplication启动类默认会扫描该启动类所在的包及其子包。

Spring Boot启动类通常使用@SpringBootApplication注解,该注解是一个组合注解,包含了@ComponentScan,@EnableAutoConfiguration和@Configuration等。@ComponentScan会扫描该类所在的包及其子包中的Spring组件(如@Component, @Service, @Repository等),如果不指定basePackages,则默认会扫描该启动类所在的包及其子包。

1.3 解决方案

解决方法1【推荐】:确保两个模块的启动类包路径一致性(com.es)

解决方法2: 利用@SpringBootApplication的scanBasePackages 属性指定包的所有扫描路径,例如"com"。

@SpringBootApplication(scanBasePackages = {"com.zkn","com.st."})

或者使用@ComponentScan的value属性指定包的扫描路径

@ComponentScan(value = {"com.zkn","com.st"})

代码语言:javascript
复制
@ComponentScan(basePackages = {"com.example.*"})

其他解决方案:编写自己的starter项目

1.4 模块结构

子模块

代码语言:javascript
复制
<modules>
        <module>Common</module>
        <module>Mybatis</module>
        <module>ES</module>
        <module>push_http</module>
    </modules>

A模块引入B模块

代码语言:javascript
复制
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

II Starter项目

Starter开发步骤:

  • 新建Maven项目,在项目的POM文件中定义使用的依赖;
  • 新建配置类,写好配置项和默认的配置值,指明配置项前缀;
  • 新建自动装配类,使用@Configuration和@Bean来进行自动装配;
  • 新建spring.factories文件,指定Starter的自动装配类;

在Spring Boot3中,传统的spring.factories不生效。

2.1 多模块项目跨包自动注入

Spring Boot3多模块项目跨包自动注入的方法,快速编写自己的starter项目。

  1. 确保pom.xml里声明的打包类型是jar
  2. 写一个Configuration类
代码语言:javascript
复制
package com.commons.spring;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.commons")
@SpringBootConfiguration
public class BeanConfigScanConfig{
}

  1. 在资源目录下建立目录META-INF/spring/,并放入org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,然后把第二步写的Configuration类路径放进去即可。
代码语言:javascript
复制
com.commons.spring.BeanConfigScanConfig

2.2 接管生命周期,成为starter

自动连接数据源/ES、自动建立定时任务、关闭的时候自动清理外部缓存

代码语言:javascript
复制
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
    }
}

2.3 预备知识:自动装配

III 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOS逆向 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引言
  • I Spring Boot 多模块项目跨包自动注入的方法
    • 1.1 问题描述
      • 1.2 原因
        • 1.3 解决方案
          • 1.4 模块结构
          • II Starter项目
            • 2.1 多模块项目跨包自动注入
              • 2.2 接管生命周期,成为starter
                • 2.3 预备知识:自动装配
                • III 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
                相关产品与服务
                云数据库 SQL Server
                腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档