springboot简化之前的很多xml文件,不需要大量配置xml文件,没有很多xml文件是多么的愉快
下面进入正题,演示简单的入门程序,虽然简单,但足以振奋初学者们
我新建的工程名叫hello,大家可以参考
一、建立一个war工程,导入正确的依赖
<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.springboot.demo</groupId> <artifactId>hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
二、建包建java文件
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class HelloController { @RequestMapping("/hello") @ResponseBody String home() { return "Hello,spring boot!"; } public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } }
三、在HelloController里面执行main方法
出现如下图所示,表示启动成功
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.4.RELEASE) 2017-12-24 18:45:26.059 INFO 13340 --- [ main] com.example.HelloController : Starting HelloController on LAPTOP-AP8KJEQL with PID 13340 (E:\Demo\workspace\hello\target\classes started by eluzhu in E:\Demo\workspace\hello) 2017-12-24 18:45:26.061 INFO 13340 --- [ main] com.example.HelloController : No active profile set, falling back to default profiles: default 2017-12-24 18:45:26.086 INFO 13340 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24b1d79b: startup date [Sun Dec 24 18:45:26 CST 2017]; root of context hierarchy 2017-12-24 18:45:27.110 INFO 13340 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-12-24 18:45:27.121 INFO 13340 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2017-12-24 18:45:27.122 INFO 13340 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33 2017-12-24 18:45:27.241 INFO 13340 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-12-24 18:45:27.242 INFO 13340 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1158 ms 2017-12-24 18:45:27.486 INFO 13340 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-12-24 18:45:27.490 INFO 13340 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-12-24 18:45:27.679 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@24b1d79b: startup date [Sun Dec 24 18:45:26 CST 2017]; root of context hierarchy 2017-12-24 18:45:27.728 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.example.HelloController.home() 2017-12-24 18:45:27.731 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-12-24 18:45:27.731 INFO 13340 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-12-24 18:45:27.751 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-24 18:45:27.751 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-24 18:45:27.775 INFO 13340 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-12-24 18:45:27.839 INFO 13340 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-12-24 18:45:27.901 INFO 13340 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-12-24 18:45:27.904 INFO 13340 --- [ main] com.example.HelloController : Started HelloController in 2.094 seconds (JVM running for 2.468)
四、第三步执行成功后,在浏览器输入localhost:8080/hello就可以看到hello,springboot这句话了,就表示入门程序成功了
最后说明:springboot远远不止这么点功能,还有很多了,一起探索吧