第一步:新建配置文件实体类
package com.newcapec.swaggerdemo.configuration;
import lombok.Data;
@Data
public class MyConfig {
private String username;
private String password;
private Integer age;
}
第二步:新建配置文件类
package com.newcapec.swaggerdemo.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@Component
public class MyConfiguration {
@Bean
@ConfigurationProperties(prefix = "pss")
public MyConfig myConfig(){
return new MyConfig();
}
}
第三步:编写配置文件
server:
port: 8077
pss:
username: pss
password: 123456
age: 25
第四步:使用
package com.newcapec.swaggerdemo.controller;
import java.util.ArrayList;
import java.util.List;
import com.newcapec.swaggerdemo.configuration.MyConfig;
import com.newcapec.swaggerdemo.model.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "测试模块")
public class Hello {
@Autowired
private MyConfig myConfig;
@ApiOperation(value = "我是hello方法的value",notes ="我是hello方法的notes",response = String.class)
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
System.out.println(myConfig.getUsername() + ":" + myConfig.getPassword() + ":" + myConfig.getAge());
return "hello";
}
}
读取的结果展示:
pss:123456:25