springBoot项目是spring的一个子项目,使用约定由于配置的思想省去了以往在开发过程中许多的配置工作(其实使用springBoot并不是零配置,只是使用了注解完全省去了XML文件的配置),达到了开箱即用的目的使我们专注于业务逻辑的快速开发
一下的demo是我总结网上的一些配置自己实践过得来的:
我们使用maven来作为依赖管理的工具,首先,我们使用maven-archtype-quickstart模板建立一个普通java项目 以下是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>com.jiaoyiping</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 根据自己的需要决定是打jar包还是war包-->
<packaging>war</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
以rest方式的接口开发为例,如果我们不需要用打war包的方式进行部署(pom中打包方式要改成jar) 可以通过main()方法来运行:
package com.jiaoyiping.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.jiaoyiping.springboot.service.MailService; /** * Hello world! * */ @Configuration @ComponentScan @EnableAutoConfiguration @RequestMapping(value = "/hello") public class App{ @RequestMapping(value = "/hello", method = RequestMethod.GET) @ResponseBody public String hello() { return "hello world"; }public static void main(String[] args) { SpringApplication.run(App.class, args); } }
启动项目、访问http://localhost:8080/hello/hello 就可以访问我们定义的接口
如果需要通过打包的方式在web容器中进行部署,则需要继承 SpringBootServletInitializer 覆盖configure(SpringApplicationBuilder)方法
代码如下:
package com.jiaoyiping.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.jiaoyiping.springboot.service.MailService; /** * Hello world! * */ @Configuration @ComponentScan @EnableAutoConfiguration @RequestMapping(value = "/hello") public class App extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(App.class); } @RequestMapping(value = "/hello", method = RequestMethod.GET) @ResponseBody public String hello() { return "hello world"; } }
需要注意一下几点:
1.jar包中的打包方式根据自己的需要进行修改
2.若打包成war包,则需要继承 org.springframework.boot.context.web.SpringBootServletInitializer类,覆盖其config(SpringApplicationBuilder)方法
3.打包成war的话,如果打包之后的文件中没有web.xml文件的话自己可以加进去一个最简单的web.xml(只有根节点的定义,而没有子元素),防止因缺乏web.xml文件而部署失败