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

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

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

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

我有以下代码:

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@EnableAutoConfigurationpublic 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  >        <property name="name" value="${name.property}"/>    </bean>    <!-- this property value is defined in application.properties, which Spring Boot automatically installs for us.    -->    <bean  >        <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
,和
application.properties

# application.properties some.property=Test

和..

# another.properties name.property=Another

当我运行此命令时,输出为:

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

这似乎工作。

我不太确定我是否理解该错误。您能否详细说明一下,或者也请确认这对您来说似乎令人满意?



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

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

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