栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

  1. 当前目录的/ config子目录。
  2. 当前目录
  3. 类路径/ config包
  4. 类路径根

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

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 >        <property name="field" value="${sample}-world"></property>    </bean></beans>

application.properties

sample=hello

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

确保已启用自动配置。 如果您没有这样做,它将无法按预期工作。为此,请像示例中一样添加@EnableAutoconfiguration批注。

请注意,您正在使用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

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417102.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号