簡體   English   中英

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

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

我的Spring Boot應用程序中有一個applicationContext.xml文件。 在這個文件中,它有一個屬性占位符 - $ {profile.services.url} - 用於配置<jaxws:client> 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時無法識別它。 我嘗試添加以下內容,但它似乎不起作用。

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

關於如何讓@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<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 + '\'' +
                '}';
    }

}

我有一個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>

我有以下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>

最后,我有兩個.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() { ... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM