从入门到入门-Spring Boot-属性配置2
来源:互联网 发布:怎么用vs2015写c语言 编辑:程序博客网 时间:2024/06/02 02:55
在yml文件中添加name 、age
在Java中获取
浏览器访问http://localhost:8082/user/hello
正常获取到属性中的值!!!!!!!!
再往下看
添加李四
添加User类
package com.demo;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created by chenjianfei on 2017-7-11. */@Component@ConfigurationProperties(prefix = "user")public class User { private String userName ; private Integer age; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
Controller引入User
package com.demo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by chenjianfei on 2017-7-11. */@RestControllerpublic class UserController { @Value("${name}") private String name; @Value("${age}") private Integer age; @Autowired private User user; @RequestMapping(value="/hello",method = RequestMethod.GET) public String say(){ return user.getUserName()+" " +user.getAge(); }}
如果发生如下错误,请修改pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional></dependency>
最后很重要一步:
配置完还需要在spring Boot入口类加上@EnableConfigurationProperties并指明要加载哪个bean,如果不写ConfigBean.class,在bean类那边添加
重启Spring Boot,浏览器访问http://localhost:8082/user/hello
推荐一篇很好的文章 http://tengj.top/2017/02/28/springboot2/
阅读全文
0 0
- 从入门到入门-Spring Boot-属性配置2
- 从入门到入门-Spring Boot-属性配置
- 从入门到入门-Spring Boot-属性配置3
- 从入门到入门-Spring Boot-常用属性
- 从入门到入门-Spring Boot-入门
- 从入门到入门-Spring Boot-Controller
- spring boot从入门到精通
- Spring Boot从入门到精通课程
- 【Spring Boot】 从入门到熟练,从简介到集成
- 从入门到入门-Spring Boot-CRUD+Transactional
- 从入门到入门-Spring Boot-第一个Spring Boot应用
- Docker 从入门到放弃(二)之Spring Boot
- Spring boot 系列 入门--配置
- spring boot 入门 (2)
- spring boot入门-2
- spring 从入门到总结
- spring security 从入门到精通二 基础配置
- Spring 入门之属性配置
- nodejs远程调试
- 等价二叉树
- Java 用Arrays.fill() 初始化 二维数组
- (转载) JSP内置对象request的方法
- 相册/拍照或者微信支付/支付宝支付底部弹出框
- 从入门到入门-Spring Boot-属性配置2
- 1About This Guide
- R中的自定义函数function
- SQL error 0x800a0cc1
- 才知道java竟然没有运算符重载
- docker总结
- OpenSource官方下载地址大全
- 实践项目五:python调用百度API实现自动检测翻译
- (3)详解ServerSocket类