gpt4 book ai didi

java - 使用 Spring xml 配置从 Rest 端点获取 Maven 构建信息

转载 作者:行者123 更新时间:2023-12-01 06:03:18 25 4
gpt4 key购买 nike

目前我有一个很好的工作解决方案:

来 self 的 pom.xml:

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

资源/mvn.build.properties:

version=${pom.version}
build.timestamp=${timestamp}

BuildInfoProviderImpl.java:

@Service
@PropertySource("classpath:mvn.build.properties")
public class BuildInfoProviderImpl implements BuildInfoProvider {

@Value("${build.timestamp}")
private String dateTime;
@Value("${version}")
private String version;

private BuildInfo buildInfo;

@Override
public BuildInfo getBuildInfo() {
return buildInfo;
}

@PostConstruct
public void activate() {
buildInfo = new BuildInfo(
LocalDateTime.parse(dateTime, Constants.DATE_TIME_FORMATTER),
version);
}
}

HealthEndpoint.java:

@Service
@Path("_health")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class HealthEndpoint {

private static final Logger LOGGER = LoggerFactory.getLogger(
HealthEndpoint.class);

private BuildInfoProvider buildInfoProvider;

public HealthEndpoint(BuildInfoProvider buildInfoProvider) {
this.buildInfoProvider = buildInfoProvider;
}

@GET
public BuildInfo getBuildInfo() {
LOGGER.debug("Build info has been requested");
return buildInfoProvider.getBuildInfo();
}
}

问题是:如何使用 xml 配置文件配置完全相同的东西?

我尝试使用 maven-war-plugin 添加属性,然后从我的服务中获取它,但这根本不起作用。在类路径中看不到它。仅供引用:它在 ${baseFolder}/target/{app}.war 下生成。

我需要这样的东西,但不知何故我必须设置maven构建属性,我尝试的一切都失败了:

<bean id="buildInfoProvider" class="service.impl.BuildInfoProviderImpl"/>

<bean id="healthEndpoint" class="endpoint.HealthEndpoint">
<constructor-arg ref="buildInfoProvider"/>
</bean>

最佳答案

好吧,由于我实际上没有正确的答案,所以我自己提供:

public class HealthEndpoint {

private static final Logger LOGGER = LoggerFactory.getLogger(HealthEndpoint.class);

@Context
private ServletContext context;

private BuildInfo buildInfo;

@GET
public BuildInfo getBuildInfo() {
LOGGER.debug("Build info has been requested");
setBuildInfo();
return buildInfo;
}

private void setBuildInfo() {
if (buildInfo != null) {
return;
}
// some logic of setting build info with help of manifest file here
}
}

在我的 xml 配置中,我只是创建这个 bean,如下所示:

<bean id="healthEndpoint" class="endpoint.HealthEndpoint"/>

关于java - 使用 Spring xml 配置从 Rest 端点获取 Maven 构建信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com