关于@value的springapplication容器的问题
1.在src/main/resources下创建stu.properties文件
## student . name = Tom student . age = 22 student . birthday = 1996 / 01 / 10 student . sex = true student . hobbies [ 0 ]= swimming student . hobbies [ 1 ]= basketball student . skills [ 0 ]= programming student . skills [ 1 ]= test student . address . province = henan student . address . city = zhengzhou2.创建实体类
package com . fcy . entity ; import java . util . Arrays ; import java . util . Date ; import java . util . List ; import java . util . Map ; import org . springframework . beans . factory . annotation . Value ; import org . springframework . context . annotation . PropertySource ; import org . springframework . stereotype . Component ; @Component @PropertySource ({ "classpath:stu.properties" }) public class Student { @Value ( "${student.name}" ) private String name ; @Value ( "${student.age}" ) private int age ; @Value ( "${student.sex}" ) private boolean sex ; @Value ( "${student.birthday}" ) private Date birthday ; private String [] hobbies ; private List < String > skills ; private Map < String , Object > address ; //省略getter和setter方法 }3.错误的方法
@RestController public class StudentController { @RequestMapping ( "/getStu" ) public Student getStu () { Student stu = new Student (); System . out . println ( stu ); return stu ; } }4. 效果如图
5.正确的方法为
总结:
从上面方法得知,第一种方法没有获取到值是因为没有在springapplication容器里获取student的bean,因为在实体类加上了@comment注解,这个注解就是把student变成一个bean,才能读取到,不能new 一个对象调用
SpringBoot @Value注解设置默认值
默认值的设置:
符合SpEL表达式
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
原文链接:https://blog.csdn.net/qq_42274641/article/details/83107807
查看更多关于解决SpringBoot @value注解取不到值的问题的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did214496