方法1:绑定对象bean调用
1、在application.properties或者application.yml里面添加我们的配置项:
2、创建一个配置类(该类和我们上面配置项的属性一一对应):
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "upload") public class PropertiesConfig { private int port; public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
3、在需要调用的地方自动装配上我们上面的类并按照下面的方式获得配置项的值:
@Controller @RequestMapping("/update/send") public class UpdateSendController extends BaseController { @Autowired private PropertiesConfig propertiesconfig; int port = propertiesconfig.getPort(); }