java – 自从迁移到Spring Boot 1.1.4.RELEASE之后,发生@Value和
发布时间:2020-12-14 05:42:09 所属栏目:Java 来源:网络整理
导读:我有一个问题,因为我移动到版本1.1.4.RELEASE的春天启动. 使用@Value注释的变量目前尚未填入值,尽管它们存在于application.properties中.在此之前,我使用的是Spring Boot @ version 1.0.2,而且工作正常. 这一切都是从升级开始的,我没有改变代码. SampleAppli
我有一个问题,因为我移动到版本1.1.4.RELEASE的春天启动.
使用@Value注释的变量目前尚未填入值,尽管它们存在于application.properties中.在此之前,我使用的是Spring Boot @ version 1.0.2,而且工作正常. 这一切都是从升级开始的,我没有改变代码. SampleApplication.java package org.sample; import org.springframework.beans.factory.annotation.Value; 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.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @ComponentScan @EnableAutoConfiguration @PropertySource(value = "classpath:application.properties") public class SampleApplication { private static Logger logger = LoggerFactory .getLogger(TaskManagerApplication.class); @Value("${org.sample.sampleProperty}") private static String sampleProperty; public static void main(String[] args) { SpringApplication.run(SampleApplication.class,args); System.out.print("SampleApplication started: " + sampleProperty); } @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } } application.properties spring.datasource.url: jdbc:mysql://127.0.0.1:3306/mydb spring.datasource.username: root spring.datasource.password: root spring.datasource.driverClassName: com.mysql.jdbc.Driver spring.jpa.show-sql: true #Disable the ddl-auto:create once tables have been created #spring.jpa.hibernate.ddl-auto: create org.sample.sampleProperty=This is a sample property photos.upload.dir=C:/temp/UserPhotos/ # Server port server.port=8081 我尝试添加一个PropertySourcesPlaceholderConfigurer bean,甚至PropertySourcesPlaceholderConfigurer,但仍然存在问题. 有人经历过这个吗?还是有一种新的方式来加载属性文件? 请注意,我的数据库连接和服务器端口正在正确读取,因为我的应用程序可以连接到数据库,我必须通过指定的端口访问它. 解决方法
> @Value不适用于静态字段
> application.properties的属性可以自动使用,而不需要为其指定@PropertySource. >而不是在main()方法中打印出属性,您应该在构建bean之后执行,例如使用@PostConstruct 充分的工作实例: package demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; @Configuration @ComponentScan @EnableAutoConfiguration public class Application { @Value("${org.sample.sampleProperty}") private String sampleProperty; public static void main(String[] args) { SpringApplication.run(Application.class,args); } @PostConstruct public void postConstruct() { System.out.print("SampleApplication started: " + sampleProperty); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |