快速入门1springboot
来源:互联网 发布:网络语挖煤是什么意思 编辑:程序博客网 时间:2024/06/03 06:43
快速入门1springboot
一、第一个springBoot程序
1.创建项目:File-New Project:
选择 Spring Initializr:
推荐使用阿里云的maven镜像:
修改maven中的配置文件settings.xml:
<mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror></mirrors>
2.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.hcx</groupId> <artifactId>girl</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>girl</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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.8</java.version> </properties> <dependencies> <!--web项目必须引入的依赖--> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
3.项目结构:
GirlApplication:启动文件,必须有@SpringBootApplication注解
package com.hcx;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class GirlApplication { public static void main(String[] args) { SpringApplication.run(GirlApplication.class, args); }}
application.properties:配置文件
项目启动的三种方法:
1、直接在IDEA中Application.class中右击 run; 2、命令行在项目根目录下运行 mvn spring-boot:run (第一次运行时会现在支持文 件); 3、命令行在项目根目录下运行 mvn install进行编译,会产生一个target临时文件夹,里面有一个本项目编译的jar包,通过 java -jar jar包名 的命令可以直接运行;
4.编写类HelloController:
@RestControllerpublic class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return "Hello Spring Boot"; }}
启动程序访问:http://127.0.0.1:8080/hello
二、属性配置
项目的配置文件application.properties:
server.port=8081server.context-path=/girl
访问:http://127.0.0.1:8081/girl/hello
默认配置文件为.properties,单推荐使用.yml文件:
server: port: 8081 context-path: /girl
注意:.yml的语法:每个属性后面必须有空格
例:选出cupSize为B,age为18的:
application.yml:
server: port: 8081cupSize: Bage: 18
HelloController:
@RestControllerpublic class HelloController { @Value("${cupSize}") private String cupSize; @Value("${age}") private Integer age; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return cupSize + age; }}
注:在配置文件中不需要区分类型,只有在注入的时候标明类型即可
还可在配置中再使用配置:
application.yml:
server: port: 8081cupSize: Bage: 18content: "cupSize: ${cupSize}, age: ${age}"
HelloController:
@RestControllerpublic class HelloController { @Value("${cupSize}") private String cupSize; @Value("${age}") private Integer age; @Value("${content}") private String content; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return content; }}
当属性较多时:
新建一个属性类:
@Component //如果该类需要被注入则添加该注解@ConfigurationProperties(prefix = "girl")//获取前置为girl的配置public class GirlProperties { private String cupSize; private Integer age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
HelloController:
@RestControllerpublic class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return girlProperties.getCupSize(); }}
当生产环境与开发环境的配置不同时:可建多个配置文件:
application-dev.yml:
server: port: 8080girl: cupSize: B age: 18
application-prod.yml:
server: port: 8081girl: cupSize: F age: 18
application.yml:使用dev中的配置
spring: profiles: active: dev
也可用不同的启动方式同时启动两种配置
阅读全文
0 0
- 快速入门1springboot
- SpringBoot学习1之快速入门
- SpringBoot(1) Spring Boot快速入门
- SpringBoot 快速入门
- SpringBoot快速入门
- springboot快速入门
- SpringBoot快速入门
- SpringBoot 快速入门
- SpringBoot快速入门
- springboot的快速入门
- springboot快速入门
- springboot快速入门
- SpringBoot+IDEA+Maven快速入门
- SpringBoot快速入门(一)
- SpringBoot入门-快速整合Mybatis
- SpringBoot系列<一>快速入门
- SpringData 基于SpringBoot快速入门
- SpringBoot快速入门,不继承SpringBoot父依赖项目
- 微服务架构设计
- PHP 时间/日期/星期的方法及解决....
- 线程的状态
- 数塔 (dp)
- cocos 安卓打包问题疑难杂症汇总
- 快速入门1springboot
- 简单实用的OkHttp3入门精简教程
- 六个强大的创建RESTful服务的Java框架
- 算法导论 所有节点对的最短路径问题 FloydWarshall
- reactor模式 proator模式
- B树、B-树、B+树与红黑树
- Nginx相关
- 通过CSS的细节处理实现窗口适应
- 飞行员配对