You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Spring Boot属性Bean传递至其他项目的类中?

跨项目复用Spring Boot配置Bean的几种方案

针对你想要在多个项目中复用APPStaticProperties的需求,我整理了几种实用的方案,适配不同场景:

方案一:抽成独立的公共Jar包(最推荐的生产级方案)

这是跨Spring Boot项目复用配置类的标准做法,把公共配置逻辑独立成模块,所有需要的项目直接依赖即可:

步骤1:创建独立的Maven/Gradle模块

新建一个专门存放公共配置、工具类的模块(比如命名为common-config),在这个模块里重构你的配置类,用更贴合Spring Boot规范的写法:

import javax.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

// 用prefix绑定配置前缀,替代@Value更简洁
@Validated
@ConfigurationProperties(prefix = "dblog.system")
public class APPStaticProperties {
    @NotNull
    private String name; // 对应配置项dblog.system.name

    // Getter & Setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

然后在模块的配置类或启动类中开启配置属性支持:

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(APPStaticProperties.class)
public class CommonConfig {
}

别忘了在模块的pom.xml(如果是Maven)中添加必要依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>

步骤2:打包并发布模块

把这个模块打包成Jar,上传到公司的私有仓库(如Nexus),或者本地安装到Maven仓库(执行mvn install)。

步骤3:在目标项目中引入依赖

在需要使用APPStaticProperties的项目的pom.xml中添加依赖:

<dependency>
    <groupId>你的groupId</groupId>
    <artifactId>common-config</artifactId>
    <version>对应版本号</version>
</dependency>

然后在该项目的application.properties中配置对应的属性值:

dblog.system.name=你的项目专属值

之后就可以像原项目一样,用@Autowired注入APPStaticProperties使用了。

方案二:通过REST接口暴露配置(适合非Spring项目或松耦合场景)

如果有些目标项目不是Spring技术栈,或者不想引入Jar依赖,可以在原项目中写一个接口暴露配置:

@RestController
@RequestMapping("/public/config")
public class ConfigExportController {
    @Autowired
    private APPStaticProperties appStaticProperties;

    @GetMapping("/dblog-system-name")
    public String getDbLogSystemName() {
        return appStaticProperties.getDb_logsystem_name();
    }

    // 也可以返回整个配置对象的JSON
    @GetMapping("/full-config")
    public APPStaticProperties getFullConfig() {
        return appStaticProperties;
    }
}

其他项目通过HTTP请求调用这些接口获取配置值即可,不过这种方式依赖原项目的可用性,适合非核心配置的场景。

方案三:JSP中传递配置(适配你的控制器→JSP→其他项目类流程)

如果要把配置传递到JSP并供其他项目的类使用,可以按以下步骤操作:

1. 控制器中把配置放入Model

@Controller
public class YourController {
    @Autowired
    private APPStaticProperties appStaticProperties;

    @GetMapping("/your-page")
    public String showPage(Model model) {
        // 把配置对象放到Model中,供JSP访问
        model.addAttribute("appConfig", appStaticProperties);
        return "your-page";
    }
}

2. JSP中直接使用或传递给外部类

在JSP页面中,你可以直接用EL表达式访问配置:

<div>DB日志系统名称:${appConfig.db_logsystem_name}</div>

如果需要调用其他项目的类,直接把配置值作为参数传入即可:

<%
    // 假设其他项目的类是com.otherproject.service.ConfigConsumer
    com.otherproject.service.ConfigConsumer consumer = new com.otherproject.service.ConfigConsumer();
    // 传递配置值
    consumer.processConfig(appConfig.getDb_logsystem_name());
%>

这种方式适合简单场景,但如果多个JSP或类都需要使用,还是推荐方案一的Jar包复用,更易维护。


内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:06:29