Spring-Boot:如何在@ImportResource中引用application.properties

我的Spring Boot应用程序中有一个applicationContext.xml文件。 在此文件中,它有一个属性占位符 – $ {profile.services.url} – 用于配置 bean的“address”属性。

在我的Application.java类中,我导入了这个文件。

@ImportResource("classpath:applicationContext.xml") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

我在application.properties中定义了“profile.services.url”。 但是,在我的XML文件中构建bean时无法识别它。 我尝试添加以下内容,但它似乎不起作用。

  

关于如何让@ImportResource识别Spring Boot的属性支持的任何建议?

我有以下代码:

 package demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import java.util.Collection; @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(Application.class, args); Collection shouldBeConfigured = applicationContext.getBeansOfType(Foo.class).values(); System.out.println(shouldBeConfigured.toString()); } } @Configuration @ImportResource("/another.xml") class XmlImportingConfiguration { } class Foo { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Foo{" + "name='" + name + '\'' + '}'; } } 

我有一个Spring XML配置文件,另一个:

             

我有以下pom.xml

   4.0.0 org.demo demo 0.0.1-SNAPSHOT demo Demo project  org.springframework.boot spring-boot-starter-parent 1.0.0.RC1    org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-test test    demo.Application 1.7     org.springframework.boot spring-boot-maven-plugin      spring-snapshots Spring Snapshots http://repo.spring.io/snapshot  true    spring-milestones Spring Milestones http://repo.spring.io/milestone  false      spring-snapshots Spring Snapshots http://repo.spring.io/snapshot  true    spring-milestones Spring Milestones http://repo.spring.io/milestone  false     

最后,我有两个.properties文件, another.properties .propertiesapplication.properties

 # application.properties some.property=Test 

和..

 # another.properties name.property=Another 

当我运行它时,输出是:

[Foo {name =’Another’},Foo {name =’Test’}]

这似乎有效。

我不太确定我理解错误。 你能详细说明,或确认这似乎是令人满意的行为吗?

通过在JavaConfig中配置我的Soap服务而不是XML,我能够解决我的问题:

 @Value("${profile.services.url}") private String profileServiceUrl; @Bean public ProfileSoapService profileSoapService() { final JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setServiceClass(ProfileSoapService.class); jaxWsProxyFactoryBean.setAddress(profileServiceUrl); jaxWsProxyFactoryBean.getOutInterceptors().add(getSecurityInterceptor()); return (ProfileSoapService) jaxWsProxyFactoryBean.create(); } private WSS4JOutInterceptor getSecurityInterceptor() { ... }