- 添加组件
org.springframework.boot spring-boot-configuration-processor true - 添加注解
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "app") @Data public class CustomProperties { private String appid; private String appkey; private String secretkey; }
import com.ciih.authcenter.util.CustomProperties; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @MapperScan(basePackages = "XXX.dao") @EnableConfigurationProperties(CustomProperties.class) public class AuthcenterApplication { public static void main(String[] args) { SpringApplication.run(AuthcenterApplication.class, args); } }
- 配置文件已经有提示了

- 使用配置: 大功告成了
@RestController @RequestMapping("user") public class UserController { @Resource private CustomProperties customProperties; @PostMapping("information") public String information() { return customProperties.getAppid(); } } - 同是天涯码砖人,相逢何必曾相识;如果还有问题请加V:sunziwen3366
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200042.html原文链接:https://javaforall.net
