Spring boot -- 入门
2017-02-04 10:46
274 查看
看到spring,没错它是spring家族的一员新将,有过开发经验的程序员应该知道spring、spring mvc为我们带来的便捷的地方。
并且目前很多主流的框架都与spring有对接如hibernate、struts2、mybatis等。
越来越多的框架集成在一起,那就涉及到了配置,有过框架搭建经验的人应该知道,这个配置还是比较繁琐的 。要进行jar包版本的兼容,要使用特定的标签进行相应的注入、关联等。
现在Spring boot 为我们解决的就是这个繁琐的过程,我们不需要关注任何配置、任何依赖,只需要引入我们想要的技术就可以。
目前较为流行的2中构建方式maven、gradle。
我这个例子使用的是maven构建的工程,如图
附上pom.xml
代码19行
spring-boot-starter-web
核心的配置,这里指定使用spring boot来构建web工程。
代码23行
spring-boot-starter-data-jpa
这里是引用jpa进行数据的持久化。
代码27行
spring-boot-starter-thymeleaf
引入thymeleaf视图模板。
更多参考,可以下载《Spring Boot实战 ,丁雪丰 (译者) .pdf》在附录B中有全部的依赖关系记载。
下面我们在来看下Example这个类
这里需要提的是@EnableAutoConfiguration注解
这个是spring boot中引入的,含义是自动化配置,大白话就是什么都不用配置了,只要有@EnableAutoConfiguration(很牛的注解,其实也就是你想要的配置spring boot都帮你干了)。
第17行代码
SpringApplication.run(Example.class, args);
启动服务,你会有疑问,不需要容器吗,如tomcat。
答案是no,spring boot自己集成了tomcat。你只需要运行main方法中的
SpringApplication.run(Example.class, args);一个web服务就起来了。
我们运行下试试
启动起来了,访问URL:http://localhost:8080/
你可以自己复制我上面的代码:
1、maven构建一个新工程
2、引入pom.xml,我上面的代码。
3、创建一个Example类,我上面的代码
4、运行main方法。
5、见证奇迹的时刻。
并且目前很多主流的框架都与spring有对接如hibernate、struts2、mybatis等。
越来越多的框架集成在一起,那就涉及到了配置,有过框架搭建经验的人应该知道,这个配置还是比较繁琐的 。要进行jar包版本的兼容,要使用特定的标签进行相应的注入、关联等。
现在Spring boot 为我们解决的就是这个繁琐的过程,我们不需要关注任何配置、任何依赖,只需要引入我们想要的技术就可以。
目前较为流行的2中构建方式maven、gradle。
我这个例子使用的是maven构建的工程,如图
附上pom.xml
<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>demo1</groupId> <artifactId>demo1</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.3.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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
代码19行
spring-boot-starter-web
核心的配置,这里指定使用spring boot来构建web工程。
代码23行
spring-boot-starter-data-jpa
这里是引用jpa进行数据的持久化。
代码27行
spring-boot-starter-thymeleaf
引入thymeleaf视图模板。
更多参考,可以下载《Spring Boot实战 ,丁雪丰 (译者) .pdf》在附录B中有全部的依赖关系记载。
下面我们在来看下Example这个类
package demo1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
这里需要提的是@EnableAutoConfiguration注解
这个是spring boot中引入的,含义是自动化配置,大白话就是什么都不用配置了,只要有@EnableAutoConfiguration(很牛的注解,其实也就是你想要的配置spring boot都帮你干了)。
第17行代码
SpringApplication.run(Example.class, args);
启动服务,你会有疑问,不需要容器吗,如tomcat。
答案是no,spring boot自己集成了tomcat。你只需要运行main方法中的
SpringApplication.run(Example.class, args);一个web服务就起来了。
我们运行下试试
启动起来了,访问URL:http://localhost:8080/
你可以自己复制我上面的代码:
1、maven构建一个新工程
2、引入pom.xml,我上面的代码。
3、创建一个Example类,我上面的代码
4、运行main方法。
5、见证奇迹的时刻。
相关文章推荐
- springboot 开发入门,及问题汇总
- Spring Boot 快速入门
- [Spring入门学习笔记][Spring Boot]
- spring-boot之入门实践
- Spring Boot 快速入门
- 微框架 - spring boot快速入门
- spring boot入门例
- Spring boot 入门 实例
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- spring boot 入门
- SpringBoot入门
- Spring Boot 入门
- 基于Spring Boot构建的Spring MVC快速入门
- Spring Boot 快速入门
- springboot 开发入门,及问题汇总
- Spring Boot 入门
- Spring Boot 入门
- Spring Boot入门之Hello world!