2014-01-31 10 views
10

Ich habe eine applicationContext.xml Datei in meinem Frühjahr Boot-Anwendung. In dieser Datei hat es eine Eigenschaft Platzhalter - $ {profile.services.url} - dass die „Adresse“ Eigenschaft einer < jaxws verwendet wird zu konfigurieren: client> Bean.Frühlings-Boot: Wie verweisen ich application.properties in einem @ImportResource

In meiner Application.java Klasse, importiere ich diese Datei.

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

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

Ich habe "profile.services.url" in application.properties definiert. Es wird jedoch nicht erkannt, wenn die Bean in meiner XML-Datei erstellt wird. Ich habe versucht, Folgendes hinzuzufügen, aber es scheint nicht zu funktionieren.

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

Haben Sie Vorschläge, wie Sie @ImportResource dazu bringen können, die Eigenschaft von Spring Boot zu erkennen?

Antwort

17

Ich habe den folgenden Code bekam:

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

} 

ich eine Spring-XML-Konfigurationsdatei haben, 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> 

ich folgendes pom.xml haben:

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

Schließlich ich habe zwei .properties Dateien, another.properties und application.properties:

# application.properties 
some.property=Test 

und ..

# another.properties 
name.property=Another 

Als ich dies ausführen, wird die Ausgabe lautet:

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

Was scheint zu funktionieren.

Ich bin nicht ganz sicher, ob ich den Fehler bin zu verstehen. Können Sie das bitte auch für Sie ausarbeiten oder bestätigen?

+0

Dank Josh - ich war in der Lage, dies zu verwenden, um nachzuweisen, dass die Eigenschaft gesetzt zu werden (und liest) aus application.properties. Das Problem, das ich jetzt habe, ist ähnlich http://stackoverflow.com/questions/19748499/jaxwsclient-address-property-not-resolving-placeholder#comment32408122_19748499. Mit anderen Worten, das „Adresse“ Attribut scheint an diesem Punkt zur Laufzeit und es gibt keine Eigenschaft Substitution neu bewertet werden. –

2

konnte ich mein Problem umgehen, indem Sie meinen Soap-Dienst in JavaConfig statt XML konfigurieren:

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