Spring Boot学习笔记(一)
来源:互联网 发布:大众网络报17年还有吗 编辑:程序博客网 时间:2024/05/29 16:52
一个简单的HelloWorld
1. 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.github.abel533</groupId> <artifactId>spring-boot</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build></project>
- 增加父pom,spring-boot-starter-parent包含了常用的依赖管理,自己添加这些依赖时不需要添加版本号,但是本身已经有父依赖的时候可以用一下的代码实现
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.2.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
- java version属性,Spring默认使用JDK 1.6,当规定使用其他版本的时候使用本标签
<properties> <java.version>1.8</java.version></properties>
- 添加spring-boot-starter-*这样的依赖去具体整合某个功能,完整功能列表详见完整的spring-boot-starter-功能 这里使用了spring-boot-starter-web去整合web功能
- 使用spring-boot-maven-plugin插件给项目打包,为了避免项目频繁启动,在插件中添加依赖实现热部署
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency></dependencies>
2. 创建一个应用类
这个HelloWorld的应用类代码如下(Spring Boot建议将我们main方法所在的这个主要的配置类(入口类)配置在根包名下。)
@Controller@EnableAutoConfigurationpublic class Application { @RequestMapping("/") String home(){ return "Hello world!"; } @RequestMapping("/now") String hehe(){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); return "right now:"+format.format((new Date())); } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
在这个过程中我出现了一个问题,8080端口被Oracle占用,这个时候可以直接使用Spring自动配置功能中修改其基本配置文件的方式来修改自动创建的Tomcat的端口号。这个时候只需要直接创建一个名为application.properties或者application.yml 的文件直接去修改端口号,代码如下(注意:key后面的冒号,后面一定要跟一个空格):
server: port: 8888
关于一些注解的解释
1. @RestController 这个注解相当于 @Controller+@ResponseBody
2. @EnableAutoConfiguration 这个注解SpringBoot只建议存在一个,这个注解用来实现自动根据pom.xml中的jar包依赖来自动配置项目。例如当项目下有jdbc的依赖的时候会自动去创建jdbc的数据源DataSource,但是如果自己创建了的话SpringBoot就不再自动创建,而且可以通过如下的代码手动取消自动创建数据源。
@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class MyConfiguration {}
- @SpringBootApplication 相当于(注解的继承实现)@Configuration+@EnableAutoConfiguration+@ComponentScan
- 项目启动使用SpringApplication.run()方法,但是也可以使用如下的方法去启动项目,下面的方法可以包含其他配置方法
SpringApplication application = new SpringApplication(Application.class);application.run(args);
- 除了上面的启动方法,还可以使用SpringApplicationBuilder
new SpringApplicationBuilder() .showBanner(false) .sources(Application.class) .run(args);
在SpringMVC中还可以使用该类的child(xxx…)方法可以添加子容器
阅读全文
0 0
- spring boot 学习笔记(一)
- spring boot学习笔记(一)
- spring boot学习笔记(一)
- Spring Boot学习笔记(一)
- Spring Boot入门学习笔记(一)
- spring boot学习笔记(一)
- Spring Boot 学习笔记(一)
- Spring Boot 学习笔记(一)
- Spring Boot学习笔记-从Hello World开始 (一)
- Spring Boot 学习笔记(一)——入门
- Spring Boot 学习笔记一(SpringBoot启动过程)
- Spring-Boot学习笔记(一) 项目搭建和HelloWold
- Spring Boot学习笔记----mybatis注解(一)
- Spring Boot学习笔记:(一)简介与HelloWorld搭建
- Spring-Boot学习笔记一:Hello World
- 学习笔记一:Spring Boot快速入门
- Spring Boot学习(一)
- Spring Boot学习(一)
- GLES20.glTexImage2D、GLUtils.texImage2D 使用区别
- CentOs7安装及配置网卡
- C++ 内存泄露的几种情况
- Java设计模式_(行为型)_中介者模式
- 2.1.3多个对象锁(一个线程一个对象锁)
- Spring Boot学习笔记(一)
- FTP文件上传
- HTTP-POST数据
- windows10下编译ffmpeg for android----编译成功
- 删除链表中重复的节点
- android日历多选 日历插件
- spring mvc 4 + swagger2
- JS前端基础-数据类型
- GPU和CUDA基础介绍