Spring Boot的application.properties值没有填充

我有一个非常简单的Spring Boot应用程序,我试图使用一些外部configuration。 我试图按照春季启动文档的信息,但我正在打一个路障。

当我在application.properties文件中的外部configuration下运行应用程序时,不会将其填充到bean中的variables中。 我确信我正在做一些愚蠢的,谢谢你的任何build议。

MyBean.java (位于/ src / main / java / foo / bar /)

package foo.bar; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component; @Component public class MyBean { @Value("${some.prop}") private String prop; public MyBean() { System.out.println("================== " + prop + "================== "); } } 

Application.java (位于/ src / main / java / foo /)

 package foo; import foo.bar.MyBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan @EnableAutoConfiguration public class Application { @Autowired private MyBean myBean; public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

application.properties(位于/ src / main / resources /)

 some.prop=aabbcc 

执行Spring Boot应用程序时的日志输出

 grb-macbook-pro:properties-test-app grahamrb$ java -jar ./build/libs/properties-test-app-0.1.0.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.1.5.RELEASE) 2014-09-10 21:28:42.149 INFO 16554 --- [ main] foo.Application : Starting Application on grb-macbook-pro.local with PID 16554 (/Users/grahamrb/Dropbox/dev-projects/spring-apps/properties-test-app/build/libs/properties-test-app-0.1.0.jar started by grahamrb in /Users/grahamrb/Dropbox/dev-projects/spring-apps/properties-test-app) 2014-09-10 21:28:42.196 INFO 16554 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67e38ec8: startup date [Wed Sep 10 21:28:42 EST 2014]; root of context hierarchy 2014-09-10 21:28:42.828 INFO 16554 --- [ main] osbfsDefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 2014-09-10 21:28:43.592 INFO 16554 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 2014-09-10 21:28:43.784 INFO 16554 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2014-09-10 21:28:43.785 INFO 16554 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.54 2014-09-10 21:28:43.889 INFO 16554 --- [ost-startStop-1] oaccC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2014-09-10 21:28:43.889 INFO 16554 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 1695 ms 2014-09-10 21:28:44.391 INFO 16554 --- [ost-startStop-1] osbceServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2014-09-10 21:28:44.393 INFO 16554 --- [ost-startStop-1] osbcembedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] ================== null================== 2014-09-10 21:28:44.606 INFO 16554 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-09-10 21:28:44.679 INFO 16554 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2014-09-10 21:28:44.679 INFO 16554 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 2014-09-10 21:28:44.716 INFO 16554 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-09-10 21:28:44.716 INFO 16554 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-09-10 21:28:44.902 INFO 16554 --- [ main] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup 2014-09-10 21:28:44.963 INFO 16554 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http 2014-09-10 21:28:44.965 INFO 16554 --- [ main] foo.Application : Started Application in 3.316 seconds (JVM running for 3.822) ^C2014-09-10 21:28:54.223 INFO 16554 --- [ Thread-2] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67e38ec8: startup date [Wed Sep 10 21:28:42 EST 2014]; root of context hierarchy 2014-09-10 21:28:54.225 INFO 16554 --- [ Thread-2] osjeaAnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 

执行注入属性的方式将不起作用,因为注入是在调用构造函数之后完成的。

您需要执行以下任一操作:

解决scheme更好

 @Component public class MyBean { private final String prop; @Autowired public MyBean(@Value("${some.prop}") String prop) { this.prop = prop; System.out.println("================== " + prop + "================== "); } } 

解决scheme将工作,但不易testing,可读性稍差

 @Component public class MyBean { @Value("${some.prop}") private String prop; public MyBean() { } @PostConstruct public void init() { System.out.println("================== " + prop + "================== "); } } 

另外请注意,这不是Spring Boot的具体情况,但适用于任何Spring应用程序

用户“geoand”在这里指出原因并给出解决scheme是正确的。 但更好的方法是将你的configuration封装到一个单独的类中,比如SystemContiguration java类,然后把这个类注入到你想要使用这些字段的服务中。

将configuration值直接读入服务的当前方法(@grahamrb)容易出错,如果更改了configuration设置名称,将导致重构头痛。