@ConfigurationProperties注解使用

yml配置文件

nacos:
  swagger:
    swagger-name: 某某项目文档
    swagger-url: http://www.baidu.com

实体类

//可以使用yml也可以是用properties中的配置
@ConfigurationProperties(prefix = "nacos.swagger")
//为什么带上data?理解为因为@ConfigurationProperties并不像@Value,只对封装后的方法赋值
@Data
//让@Autowired注解可以找到这个类
@Component
public class swagger {
    
    

    String swaggerName;
    
	// 配置文件中是swagger-url, 转驼峰命名便可以绑定成
    String swaggerUrl;
}

这样一个注解就配置所有配置,但是需要注意下,配置文件中是swagger-url, 转驼峰命名便可以绑定成

controller中使用

@RestController
public class test{
    
    

	@Autowired
    private swagger swagger;
    
    @RequestMapping("/get")
	public String get(){
    
    
		System.out.println(swagger.getSwaggerName());
	}
}

猜你喜欢

转载自blog.csdn.net/u014641168/article/details/121849834