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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21470409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:52:18  来源:igfitidea点击:

Spring-Boot: How do I reference application.properties in an @ImportResource

javaspringspring-boot

提问by Matt Raible

I have an applicationContext.xml file in my Spring Boot application. In this file, it has a property placeholder - ${profile.services.url} - that's used to configure the "address" property of a <jaxws:client> bean.

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

In my Application.java class, I import this file.

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

@ImportResource("classpath:applicationContext.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I have "profile.services.url" defined in application.properties. However, it's not recognized when building the bean in my XML file. I've tried adding the following, but it doesn't seem to work.

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

<context:property-placeholder location="classpath:application.properties"/>

Any suggestions on how to get @ImportResource to recognize Spring Boot's property support?

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

采纳答案by Josh Long

I've got the following code:

我有以下代码:

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<Foo> 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 + '\'' +
                '}';
    }

}

I have a Spring XML configuration file, another.xml:

我有一个 Spring XML 配置文件another.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="another.properties" />

    <!-- this property value is defined in another.properties, which we install in this XML file
    -->
    <bean class="demo.Foo" >
        <property name="name" value="${name.property}"/>
    </bean>

    <!-- this property value is defined in application.properties, which Spring Boot automatically installs for us.
    -->
    <bean class="demo.Foo" >
        <property name="name" value="${some.property}"/>
    </bean>

</beans>

I have the following pom.xml:

我有以下几点pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.0.RC1</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <start-class>demo.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

Finally, I have two .propertiesfiles, another.properties, and application.properties:

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

# application.properties 
some.property=Test

and..

和..

# another.properties 
name.property=Another

When I run this, the output is:

当我运行它时,输出是:

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

[Foo{name='另一个'}, Foo{name='Test'}]

Which would seem to work.

这似乎有效。

I'm not quite sure I am understanding the error. Can you elaborate, or confirm this seems satisfactory behavior for you, too please?

我不太确定我是否理解错误。您能否详细说明或确认这对您来说似乎也令人满意?

回答by Matt Raible

I was able to workaround my issue by configuring my Soap service in JavaConfig instead of XML:

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

@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() { ... }