Springboot入门(二)项目配置
2017-08-10 18:08
316 查看
pom.xml文件相关信息
<!-- 项目的相关信息--> <groupId>com.example</groupId> <artifactId>Test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Test</name> <description>Demo project for Spring Boot</description> <!--SpringBoot的parent配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--Springboot web配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Springboot的单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--SpringBoot 构建项目时使用的插件配置 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
TestApplication.java
//要启动springboot项目必须添加该注解 @SpringBootApplication public class TestApplication { public static void main(String[] args) { //springboot的启动 SpringApplication.run(TestApplication.class, args); } }
HelloWorld.java
1、在类上面条件声明@RestController
2、增加方法@RequestMapping(“/hello”)
@RestController public class HelloWorld { @RequestMapping(value="/hello",method=RequestMethod.GET) public String say() { return "Hello Springboot"; } }
application.properties
server.port=8081 server.context-path=/Test
server.context-path=/Test为浏览网址添加了一个路径
访问地址:http://127.0.0.1:8081/Test/hello
这里推荐使用application.yml文件,application.yml文件写法上更加简单。
server: port: 8082 context-path: /Test
1、注意port:跟8082之间有空格,context-path也是一样有空格。
2、使用yml文件必须进行配置
<!-- 支持 @ConfigurationProperties 注解 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
例如:
@RestController public class HelloController { @Value("${age}") private Integer age ; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return age+""; } }
application.yml
server: port: 8081 age: 18
Demo
HelloController.java@RestController public class HelloController { @Value("${age}") private Integer age ; @Value("${name}") private String name; @Value("${content}") private String content; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return content; } }
application.yml
server: port: 8081 age: 18
name: 小芳
content: "name:${name} age:${age} "
简化书写方式:
PersonProperties.java//添加组件 @Component //设置前缀是person @ConfigurationProperties(prefix="person") public class PersonProperties { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
application.yml
server: port: 8081 person: age: 19 name: 小芳
调用
@RestController public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge(); } }
结果图省略
开发环境与测试环境配置
相关文章推荐
- SpringBoot入门(一)--基于IDEA编译器的项目构建、启动、配置
- Myeclipse服务器项目配置时与jdk路径不符合
- 分享知识-快乐自己:IDEA下maven编译打包Java项目成jar包但是resource下配置文件无法编译
- VC项目配置基础
- maven搭建项目,settings.xml配置文件,以及parent的pom.xml
- apache添加 扩展php自定义的项目配置方法
- 在java web项目中配置freemarker
- 在java web项目中配置freemarker
- Disconf介绍,源码下载,环境准备,安装,disconf-web使用和配置介绍,项目中进行配置,项目案例运行
- centos7 apache httpd安装和配置django项目
- 【图文讲解】eclipse中配置maven环境+导入maven项目
- 配置Source Insight阅读PHP项目的代码
- PHPStorm 配置链接Linux服务器上的项目
- debug,trace,release项目配置区别
- 关于Cordova项目的打包配置
- 服装ERP研究(九):DRP项目结束后如何配置支持小组
- nginx在一个服务器上配置两个项目,并通过两个不同的域名访问
- 大型项目使用Automake/Autoconf完成编译配置
- VS加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS
- 公司项目nephelefli配置记录