第一个demo
摘要: 先了解下springboot到底是个什么东西,能用来干什么?有什么好处?也就是为什么要学习他用他。然后给个网上到处都能找到的demo。
1、什么是springboot?
简单的理解就是一个快速开发框架集合,大家都叫他快速开发脚手架。
2、springboot有什么特征?
它遵循“习惯由于配置”原则,使用springboot只需要少量配置,我们大部分时候可以使用他的默认配置;
它可以让我们搭建项目更快速,可无配置整合第三方框架;
完全不使用xml配置,只是用自动配置或是Java config
它内嵌了servelt容器(tomcat、jetty),应用可以用jar的方式运行
集成了应用运行状态的监控
3、学习springboot需要什么知识储备?
java这个不用说了,哈哈
maven这个必须会用啦
spring 、spring MVC 这两个是最基本的,这里的spring 必须是 4.x版本,因为springboot使用了spring4.x 的@Conditional注解。
想要学好它你还必须对设计模式有一定理解,还有core java中的 反射、注解、泛型 这些必不可少了,这部分有助于我们去理解源码。
1、 新建一个maven工程,大家可以直接到spring官网上去新建一个,下载下来,或是自己建一个。我提倡手动。
2、引入springboot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
完整的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.pxk</groupId>
<artifactId>springbootdemo </artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories> </project>
当然这是一个web工程,项目结构如下图
Application.java
package com.pxk;import org.apache.log4j.Logger;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { private static Logger logger = Logger.getLogger(Application.class); /**
* Main Start
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("============= SpringBoot Start Success =============");
}
}
HelloController1.java
package com.pxk;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController public class HelloController1 { @RequestMapping("/")
String home() {
return "Hello World pxk!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello "+myName+"!!!";
}
}
然后直接run Application,springboot就会启动内嵌的tomcat,默认是不带项目名,且端口为8080
访问http://localhost:8080/ 如下图
访问:http://localhost:8080/hello/springboot,使用的是spring MVC的mapping方式,生成rest风格的访问路径