我使用Spring Boot。
我想使用YAML而不是属性来编写配置。
由于我使用spring-boot-starter
,SnakeYAML库已经在类路径中,并且SpringApplication应该自动使用YAML版本。
SpringApplication类将自动支持YAML作为
每当启用SnakeYAML库时,就可以选择属性
您的课程路径。
问题是应用程序继续使用application.properties文件,如果我删除它,则根本不会加载任何配置。
有人能帮我吗?这是我的主文件
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(App.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
这是我的pom.xml
....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml文件只是
tasks: 231232
我尝试使用注入的环境读取属性
@Autowired
private Environment environment;
....
log.info(environment.getProperty("tasks"));
我的错误在哪里?
参考方案
我解决了我的问题
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.16</version>
到我的pom.xml文件。注意1.16,spring-boot-starter-parent导入1.17。
我打开一个问题https://github.com/spring-projects/spring-boot/issues/6878
Heroku Spring Boot Web服务始终返回404,但不在本地主机上 - java我已经开发了一个Web服务,正在尝试将其部署到Heroku。看来成功了,尽管每当我尝试调用我的URL之一时,我都会得到一个404。我知道这意味着资源不可用,但在localhost上运行良好。Heroku部署的输出是标准的Spring引导,但缺少以下内容:2016-05-23 22:29:22.145 INFO 15785 --- [ main] s.w.s.…
Spring Boot-使用上下文路径时在根级别的静态内容 - java假设我有一个application.yml内容server: port: 8000 context-path: /rest 因此,将像这样访问所有控制器和htmlhttp://server:8000/rest/controller因此,具有此配置...有可能在不更改其上下文路径的情况下将一些静态html元素添加到其根级别? (我已经将index.html添加…
Spring Boot:java.time.Duration的默认序列化从字符串更改为数字 - java我们最近从Spring Boot 2.1.9升级到2.2.1,这导致我们的测试失败。调查导致结果,默认情况下java.time.Duration类型现在序列化为不同的序列。现在,我们将得到"PT15M",而不是在JSON消息中包含字符串"900.0"。 POJO定义如下所示@JsonProperty(required …
无法将应用程序日志保存到文件 - java我正在创建一个简单的Spring Boot应用程序,该应用程序会在API调用上产生结果。该应用程序按预期工作,但我无法将日志打印到文件中。日志仅在控制台中显示。这是我的pom文件:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="h…
Spring Boot删除Web应用程序的Whitelabel错误页面 - java我有一个spring boot Web应用程序,正在将它作为war文件部署在tomcat中。我不希望用户显示白色标签错误页面。我对此有所进步,但需要将其重新定向到错误页面。下面的代码是/ error白色标签错误页面是自定义错误消息。但是我希望将其重定向到我的Web应用程序资源中模板文件夹下的error.jsp或error.html。我尝试将@RestCont…