SpringBoot自学笔记(一)
特此声明:本自学笔记,主要是参照《从零开始学Spring Boot》(作者: 林祥纤)一书,并对部分例子进行了改动,便于理解,非以盈利为目的,仅供学习交流,如有侵权,立即撤下!
(一)Srping Boot——入门程序HelloWord
1.使用Eclispe创建一个Maven工程(jar)。这里以“spring-boot-hello1”为例命名。
2.修改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>yan.li</groupId>
<artifactId>spring-boot-hello1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入spring-boot-start-parent 依赖管理,
引入以后在申明其它dependency的时候就不需要version了-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<!-- 引入spring-boot-starter-web 包含了
springwebmvc和tomcat等web开发的特性-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,
否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
3.编写启动类,博主放在“yan.li.test”包下。
package yan.li.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//返回json字符串的数据,直接可以编写RESTFul的接口;
@SpringBootApplication//让spring boot自动给程序进行必要的配置;
public class HelloWord {
@RequestMapping("/")
public String hello() {
return "Hello Word!";
}
public static void main(String[] args) {
SpringApplication.run(HelloWord.class, args);
}
}
4.右键Run As → Java Application运行(或是使用Maven的spring-boot:run)。打开浏览器,访问:http://localhost:8080,看到"Hello Word!",表示已经成功。
运行后你会在控制台看到如下标示:
:: Spring Boot :: (v1.3.3.RELEASE)
...(部分省略)...
2016-11-21 10:31:22.033 INFO 5888 --- [main]:
Tomcat started on port(s): 8080 (http)
2016-11-21 10:31:22.037 INFO 5888 --- [main]:
Started MainApp in 1.725 seconds (JVM running for 2.008)
(二)Srping Boot——返回Json格式数据
SpringBoot返回Json格式,主要是依赖@RestController这个注解。
【延伸】@Controller和@RestController的区别?
官方文档:@RestController is a stereotype annotation that combines @ResponseBody and @Controller
意思是:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
1.仍在上一个工程中,删除HelloWord程序,创建新包“yan.li”,编写启动类
package yan.li;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //申明让spring boot自动给程序进行必要的配置
public class MainApp {
public static void main(String[]args) {
SpringApplication.run(MainApp.class,args);
}
}
2.创建一个PO类,放在“yan.li.jsonPojo”包下
package yan.li.jsonPojo;
public class Student {
private Longid;//主键
private Stringname;//测试名称
public Long getId() {
return id;
}
public void setId(Longid) {
this.id =id;
}
public String getName() {
return name;
}
public void setName(Stringname) {
this.name =name;
}
}
3.编写Web端接口,放在“yan.li.jsonWeb”包下
package yan.li.jsonWeb;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yan.li.jsonPojo.Student;
@RestController
@RequestMapping("/demo")
public class StudentController {
@RequestMapping("/getDemo")
public Student getDemo() {
Student student=new Student();
student.setId(1l);
student.setName("张三");
return student;
}
}
4.MainApp 启动类上右键Run As → Java Application启动(或是使用Maven的spring-boot:run),浏览器访问http://localhost:8080/demo/getDemo,即可看到Json格式数据:
{"id":1,"name":"张三"}
(三)Srping Boot——热部署
所谓热部署,可简单理解为:在应用正在运行的时候,修改代码(原意指升级软件),却不需要重新启动应用。
如果使用spring-boot:run启动的话,只需修改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>yan.li</groupId>
<artifactId>spring-boot-hello1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入spring-boot-start-parent 依赖管理,
引入以后在申明其它dependency的时候就不需要version了-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<!-- 引入spring-boot-starter-web 包含了
springwebmvc和tomcat等web开发的特性-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,
否则是无法启动的。如果使用maven 的spring-boot:run的话是不需要此配置的-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
<dependencies>
<!--springloaded hot deploy热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugins>
</build>
</project>
如果使用Run As → Java Application启动的话,那么还需要做如下处理:
下载spring-loader-1.2.4.RELEASE.jar,放到项目的lib目录中,然后把在Eclipse的run参数里,在VM里加入如下参数重启即可:
-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
最后,重启即可。可以通过student.setName()方法,在运行过程中不断名字,看页面输出结果,来判定是否热部署成功。