16年开始就在写Spring Boot相关的文章了,以前一直是在本身猿天地的博客上发布,今年开始维护公众号,大部分时间都在写新的文章。java
一周能保持一篇原创的文章就已经很不错了,毕竟精力有限,在没有出新文章的时候就想着把以前写的文章分享出来,给正在入门学习Spring Boot的朋友。web
Spring Boot做为微服务框架,从最根本上来说,Spring Boot就是一些库的集合,集成了各类Spring的子项目,它可以被任意项目的构建系统所使用。面试
解决了平时开发中搭建项目框架的难度,很是方便。spring
搭建的步骤也是很简单的哈,首先建立个maven工程,而后配置pom文件数据库
文件中配置Spring Boot版本信息以及成jar包的插件。浏览器
properties中的java.version是指定jdk的版本。tomcat
关于Spring Boot的版本你们能够用最新的,目前最新的版本已经到了2.0了。微信
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</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> <properties> <java.version>1.8</java.version> </properties>
还须要别的什么配置吗,之前都要配置一些什么junit啊,各类spring的包啊,commons包啊之类的。多线程
如今彻底不须要了,是否是很简单,很方便。app
贴一个官网给的列子:
@RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
咱们能够看到全部的配置都被注解代替了,启动程序也能够直接用main方法启动了,框架里有内置的web容器,咱们不在须要把程序丢到tomcat里面去部署了,否则怎么体现为服务框架的魅力。
接下来咱们运行这个main方法能够看到输出了一大片日志信息。最有趣的还要属这个图形了。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.1.RELEASE)
咱们在浏览器中访问http://localhost:8080就能看到输出的Hello World!
下面讲下配置信息
配置的话默认的配置信息都在application.properties中,本身建个application.properties文件放在classpath中
好比这边默认启动的端口是8080, 咱们改为80的配置以下:
#tomcat port server.port=80
还有就是项目的context path默认是/, 也是能够改的
#the context path, defaults to '/' server.context-path=/spring-boot/
改为上面的配置信息后咱们在访问刚刚的地址就变成了http://localhost/spring-boot
上面官方给的列子咱们能够看到只是启动Example这一个类而已,每每开发中咱们确定加载的是多个类
下面给出一个简单的列子,以学生来讲明
咱们先按照这个层次建下包
com +- example +- spring-boot +- Application.java | +- domain | +- Student.java | +- service | +- StudentService.java | +- StudentServiceImpl.java | +- web +- StudentController.java
咱们的Application类是程序的入口,负责加载全部信息
经过ComponentScan扫描类,最简单的是只加@SpringBootApplication注解就能够了,@SpringBootApplication中已经包含了下面的注解。
@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
业务类,这边先不涉及数据库,就在业务层模拟了一条数据返回。
public interface StudentService { List<Student> queryStudents(); } @Service public class StudentServiceImpl implements StudentService { @Override public List<Student> queryStudents() { return Arrays.asList(new Student("1001", "yinjihuan")); } }
实体类
public class Student { private String id; private String name; public Student() { super(); } public Student(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制类
@RestController @EnableAutoConfiguration public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/students") Object queryStudents() { return studentService.queryStudents(); } }
咱们在Application中执行main方法启动程序,而后访问咱们的students资源
http://localhost/spring-boot/students
能够看到输出的结果
[{"id":"1001","name":"yinjihuan"}]
推荐相关阅读: