Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

@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.

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

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

like image 860
Matt Raible Avatar asked Jan 31 '14 00:01

Matt Raible


People also ask

How do I pass application property in spring boot?

Spring Boot application converts the command line properties into Spring Boot Environment properties. Command line properties take precedence over the other property sources. By default, Spring Boot uses the 8080 port number to start the Tomcat. Let us learn how change the port number by using command line properties.

Where do I put spring boot application properties?

You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.

How do you call a properties file in spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.

How do you call external properties from spring boot?

properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring. config. location environment property (comma-separated list of directory locations, or file paths).


2 Answers

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:

<?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:

<?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 .properties files, another.properties, and 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'}]

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?

like image 91
Josh Long Avatar answered Oct 21 '22 09:10

Josh Long


I was able to workaround my issue by configuring my Soap service in JavaConfig instead of 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() { ... }
like image 29
Matt Raible Avatar answered Oct 21 '22 09:10

Matt Raible