Springboot配置文件之properties
来源:互联网 发布:工作流数据库设计思路 编辑:程序博客网 时间:2024/05/21 06:25
打开IDEA,创建Springboot项目。
其中pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhenqi</groupId> <artifactId>study-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_property</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
然后在创建一个Java bean对象。User.java如下:
package com.zhenqi.study.entity;import java.util.Date;/** * Created by wuming on 2017/7/14. */public class User { private int id; private String name; private int age; private Date birDate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirDate() { return birDate; } public void setBirDate(Date birDate) { this.birDate = birDate; }}
最后创建Controller层,IndexController.java如下:
package com.zhenqi.study.controller;import com.zhenqi.study.entity.User;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.Date;import java.util.HashMap;/** * Created by wuming on 2017/7/14. */@RestController@RequestMapping(value = "/index")public class IndexController { @Value(value = "${study.secret}") private String secret; @Value(value = "${study.number}") private int id; @Value(value = "${study.number}") private int age; @Value(value = "${study.desc}") private String desc; @RequestMapping public String index() { return "hello springboot!"; } // @RequestParam 简单类型的绑定,可以出来get和post @RequestMapping(value = "/get") public HashMap<String, Object> get(@RequestParam String name) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("title", "hello zhenqi"); map.put("name", name); map.put("age",age); map.put("secret", secret); map.put("id", id); map.put("desc", desc); return map; } // @PathVariable 获得请求url中的动态参数 @RequestMapping(value = "/get/{id}/{name}/{age}") public User getUser(@PathVariable int id, @PathVariable String name,@PathVariable int age) { User user = new User(); user.setId(id); user.setName(name); user.setAge(age); user.setBirDate(new Date()); return user; }}
其配置文件 application.properties
#32位随机字符串study.secret=${random.value}#int类型的随机数字study.number=${random.int}study.name=zhenqi-study#属性占位符属性study.desc=${study.name} is a domain name#应用端口server.port=8090#时间格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss#时区设置spring.jackson.time-zone=Asia/Shanghai
run 这个类。
浏览器访问效果如下:
阅读全文
0 0
- Springboot配置文件之properties
- springBoot配置文件application.properties
- SpringBoot 配置文件 application.properties
- Springboot的配置文件Application.properties
- application.properties详解 --springBoot配置文件
- springboot之properties
- 【SpringBoot】SpringBoot之入门配置文件
- 【springboot】Spring boot配置文件 application.properties
- springboot配置文件application.properties中的配置项
- SpringBoot配置文件application.yml (application.properties)
- Springboot 之 多配置文件
- Springboot配置文件之yaml
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IntelliJ IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- Springboot 之 解决IDEA读取properties配置文件的中文乱码问题
- 笨方法学python(本文为阅读时从此书摘录的笔记) 第二天
- bzoj3594 方伯伯的玉米田 树状数组优化dp
- JS编程训练 | 题12:求二次方
- 服务器和客户端之间采用sendfile高效传送图片
- 关于垂直切分Vertical Sharding的粒度
- Springboot配置文件之properties
- hdu 1512 Monkey King (左偏树可并堆 并查集)
- 文件上传(Linux服务器)
- C++宏实现的好的代码
- 【反射】PHP的反射机制【原创】
- Android Studio通过cmake创建FFmpeg项目
- [BZOJ]2809 左偏树
- 截取list集合中任意几条数据
- NYOJ1239 引水工程(最小生成树,Prim)