spring - 将 spring-batch-admin 集成到现有的 spring boot 后无法导入属性

标签 spring spring-batch spring-batch-admin

我曾参与过一个使用 spring-batch 和 spring-boot 的项目。

我遵循了如何集成它的确切规则: 1.删​​除所有@EnableBatchProcessing 2.添加ServletConfiguration和WebappConfiguration(并使用导入它们

@Import({ ServletConfiguration.class, WebappConfiguration.class })
  • 添加 Prop :

    batch-mysql.properties

    业务架构-mysql

  • 并修改了 application.properties:

    server.servletPath=/*
    spring.freemarker.checkTemplateLocation=false
    ENVIRONMENT=mysql
    

    现在这是副作用。除了 java 配置之外,我的应用程序还使用 applicationContext .xml。

    applicationContext 有一些占位符:

      <context:property-placeholder
                location="file:///etc/location/services/myapp.properties"/>
    
    
        <bean name="configuration" class="com.mycompany.commons.configuration.factory.BeanAwareConfigurationFactory">
    
            <property name="serviceId" value="${serviceId}"/>
           ...
        </bean>
    

    集成 spring-batch-admin 后,我收到此错误:

    Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'serviceId' in string value "${serviceId}"
        at 
    ...
    

    我尝试使用@PropertySource导入它,但没有成功:

      @PropertySource("file:///etc/location/services/myapp.properties")
        public class Application extends SpringBootServletInitializer {
    
            public static void main(String[] args) {
                SpringApplication.run(Application.class, args);
                System.out.printf("Started processor service app");
            }
    

    当我从 spring-boot 项目中删除 spring-batch-admin 后,我就设法附加这些 Prop 。

    知道如何克服这个问题吗?

    最佳答案

    您可以覆盖spring-batch-admin默认上下文加载配置。在 src/main/resources/META-INF/spring/batch/override/manager/中,您可以放置​​ env-context.xml 文件,其中包含需要配置的资源已加载。

    这里是spring batch admin可以用作起点,这样您就可以执行以下操作:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--  Use this to set additional properties on beans at run time -->
        <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                    <value>classpath:batch-default.properties</value>
                    <value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
                    <!-- this line you can add-->
                    <value>file:///etc/location/services/myapp.properties</value>  
                </list>
            </property>
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            <property name="ignoreResourceNotFound" value="true" />
            <property name="ignoreUnresolvablePlaceholders" value="false" />
            <property name="order" value="1" />
        </bean>
    
    </beans>
    

    关于spring - 将 spring-batch-admin 集成到现有的 spring boot 后无法导入属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29195981/

    相关文章:

    spring - 将 jndi 数据源与 Spring Batch 管理一起使用

    Spring Batch - 未定义名为 'job-configurations' 的 bean

    spring - 如何在 Spring Batch Admin UI 中创建和部署 Spring Batch 作业

    java - 依赖注入(inject) EJB 3 - 太多选择?

    java - 将自定义端点添加到 Spring Data REST 存储库并在 Swagger 中显示

    java - Spring Boot 多模块 servletDispatchers

    spring - 如何更改 Spring 批处理(作业存储库)oracle 数据源架构

    java - 使用 JobStoreTX 为 quartz 集群配置 CronTriggerFactoryBean

    java - 如何从 spring 批处理器 process() 方法向 Spring 批处理作业启动方法抛出异常?

    spring - Spring batch中的抽象路由数据源