【问题标题】:Spring property placeholders in application-context.xml inside dependency jar依赖项 jar 中 application-context.xml 中的 Spring 属性占位符
【发布时间】:2017-11-08 21:28:32
【问题描述】:

对于我的 Spring Boot 应用程序,我使用基于注释的配置和 WebApplicationInitalizer

我的一个依赖项在 xml 中提供了 spring 配置,包含在 jar 中。我使用 @ImportResource 加载上下文 xml。这似乎可行,除了在这个 xml 中,有属性占位符,例如 ${poolsize:10}

显然,spring 不会自动替换这些占位符(我得到一个NumberFormatException)。我需要添加一些额外的配置吗?

我们的创业班:

public class Application implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        // config
        rootContext.register(JmsConfiguration.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));
    }

}

以及配置类(我们使用spring-jms):

@Configuration
@EnableJms
@ComponentScan(basePackages = { "..." })
public class JmsConfiguration implements JmsListenerConfigurer {
       // config for jms listener and jaxb, nothing to do with property handling
}

也许我错误地认为使用WebapplicationInitializer就是如何使用spring boot。也许我们甚至不需要弹簧靴?我们使用的唯一与spring boot相关的依赖是:

<dependency>
    <!-- Import dependency management from Spring Boot -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.5.3.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

我们使用的 Spring 依赖项:

org.springframework:spring-context:jar:4.3.8.RELEASE:compile
org.springframework:spring-jms:jar:4.3.8.RELEASE:compile
org.springframework:spring-oxm:jar:4.3.8.RELEASE:compile
org.springframework:spring-context:jar:4.3.8.RELEASE:compile
org.springframework:spring-beans:jar:4.3.8.RELEASE:compile

【问题讨论】:

  • 这应该是开箱即用的。如果没有,您可能已经添加了太多。您正在使用 Spring Boot,那么为什么需要 WebApplicationInitializer?看起来您正在尝试使用 Spring Boot,但不是以预期的方式。
  • 嘿马丁,感谢您的回复。我现在实际上正在处理您的一个旧应用程序(WSB):) 这个问题是关于我们要在 jboss EAP 6 上部署的一个新应用程序。我将用更多代码更新这个问题。

标签: spring spring-boot property-placeholder


【解决方案1】:

我明白了,谢谢@M。迪南

我们不需要spring boot来使用WebApplicationInitializer,但是(至少没有spring boot)我们必须声明我们自己的PropertySourcesPlaceholderConfigurer

@Configuration
@ImportResource(locations = {"classpath*:/library-context.xml"})
public class MyConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

注意。由于 @EnableAutoConfiguration@PropertyPlaceholderAutoConfiguration 包含完全相同的 PropertySourcesPlaceholderConfigurer bean,这在 Spring Boot 中开箱即用。

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 2017-03-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2012-11-01
    • 2021-01-15
    相关资源
    最近更新 更多