目前,我有一个很好的工作解决方案:
从我的pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
resources / 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添加属性,然后从我的服务中获取它,但这根本不起作用。在classpath中看不到它。仅供参考:它在$ {baseFolder} / target / {app} .war下生成。
我需要类似的东西,但是我必须设置maven构建属性,我尝试的所有方法都失败了:
<bean id="buildInfoProvider" class="service.impl.BuildInfoProviderImpl"/>
<bean id="healthEndpoint" class="endpoint.HealthEndpoint">
<constructor-arg ref="buildInfoProvider"/>
</bean>
答案 0 :(得分:0)
好吧,由于我在这里实际上没有正确的答案,因此我自己提供:
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"/>
答案 1 :(得分:-1)
您需要在应用程序上下文xml中定义PropertyPlaceholderConfigurer
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:mvn.build.properties</value>
</property>