用 @configurationproperties 代替 @value
使用方法
定义对应字段的实体
@data// 指定前缀@configurationproperties(prefix = "developer")@componentpublic class developerproperty { private string name; private string website; private string qq; private string phonenumber;}
使用时注入这个bean
@restcontroller@requiredargsconstructorpublic class propertycontroller { final developerproperty developerproperty; @getmapping("/property") public object index() { return developerproperty.getname(); }}
我们都知道注入一个 bean 有三种方式哦(set 注入, 构造器注入, 注解注入),spring 推荐我们使用构造器的方式注入 bean
我们来看看上段代码编译完之后的样子
以上就是如何使用@requiredargsconstructor来替代@autowired在java中?的详细内容。