您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Spring Boot-无法从application.properties的xml中解析属性

Spring Boot-无法从application.properties的xml中解析属性

您可以通过在XML中添加context:property-placeholder来解决该问题。这样,您将告诉Spring加载您的特定属性文件

但是,另一个与Spring Boot解决方案保持一致的方法是,仅将application.properties用作属性文件名称,并将其放在预期的位置之一,并使用@EnableAutoconfiguration批注。

Spring Boot希望在以下位置按优先顺序排列application.properties。

我已经尝试过了,并且有效。

pom.xml

<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>sample</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Sample</name>
    <description>Spring Boot sample</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>

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

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Sample.java

package sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:beans.xml")
public class Sample implements CommandLineRunner {

    @Value("${sample}")
    private String sample;

    @Autowired
    SampleService service;

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

    public void run(String... args) {
        System.out.println(service.greetings());
    }
}

SampleService.java

package sample;


public class SampleService {

    private String field;

    public String greetings() {
        return field;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }
}

beans.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"
  xmlns:p="http://www.springframework.org/schema/p"
  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-4.0.xsd">

    <bean class="sample.SampleService">
        <property name="field" value="${sample}-world"></property>
    </bean>
</beans>

application.properties

sample=hello

如果您运行该程序,则在输出中将显示 hello world@H_403_50@ 。

请注意,您正在使用Spring Boot,因此鼓励您避免XML配置。完全没有beans.xml的情况下,您可以获得相同的结果。尽管,如果仍然需要,可以将XML与注释混合使用。

我已经将两个示例项目都上传到了GitHub,请检查。

https://github.com/plopcas/example-spring-boot/tree/master/spring-boot- xml

https://github.com/plopcas/example-spring-boot/tree/master/spring- boot

希望这可以帮助。

Java 2022/1/1 18:14:42 有514人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶