版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39053211/article/details/77967145
SpringBoot并不是对Spring功能上的一种增强,而是提供了一种快速使用spring的一种方式。开箱即用,没有代码生成,也无需 XML 配置
Maven + Spring Web 项目结构在 Eclipse 下面的实现
在 POM.XML中添加,父工程依赖,springWeb工程依赖,springBoot Maven运行编译环境
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itxyl.springboot</groupId>
<artifactId>SpringBoot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 必须应用springBoot 父工程依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<!-- springWeb工程依赖 默认集成SpringMvc -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- springBoot Maven运行编译环境 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
添加后在编写JAVA代码
package com.itxyl.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @RestController标注该类的所有请求方法为 返回json字符串 不做试图解析
* @author xuyalin
*
*/
@RequestMapping("/springboot")
@RestController
public class IndexController {
@RequestMapping("/index")
public String index(){
return "success";
}
@RequestMapping("/index1")
public String index1(){
return "success1";
}
@RequestMapping("/listindex")
public List<String> listIndex(){
List<String> list=new ArrayList<String>();
list.add("index");
list.add("index1");
list.add("index5");
System.out.println(list);
return list;
}
}
package com.itxyl.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import com.itxyl.controller.IndexController;
/**
* springBoot启动类
* @author xuyalin
*
*/
@ComponentScan(basePackages={"com.itxyl.controller"})//标示为springmvc注入类
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试就直接main方法启动,如下就启动成功了,在浏览器输入测试的话就是:http://localhost:8080/springboot/listindex。根据你自己写的路径就好了。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2017-09-13 15:46:16.700 INFO 8932 --- [ main] com.itxyl.service.Application : Starting Application on DESKTOP-J74FCO4 with PID 8932 (D:\Java\workspace\SpringBoot\target\classes started by xuyalin in D:\Java\workspace\SpringBoot)
2017-09-13 15:46:16.703 INFO 8932 --- [ main] com.itxyl.service.Application : No active profile set, falling back to default profiles: default
2017-09-13 15:46:16.761 INFO 8932 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16958cd8: startup date [Wed Sep 13 15:46:16 CST 2017]; root of context hierarchy
2017-09-13 15:46:17.399 INFO 8932 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2017-09-13 15:46:18.009 INFO 8932 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-13 15:46:18.023 INFO 8932 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-09-13 15:46:18.024 INFO 8932 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.32
2017-09-13 15:46:18.120 INFO 8932 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-13 15:46:18.120 INFO 8932 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1362 ms
2017-09-13 15:46:18.389 INFO 8932 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-13 15:46:18.391 INFO 8932 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-13 15:46:18.392 INFO 8932 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-13 15:46:18.392 INFO 8932 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-13 15:46:18.392 INFO 8932 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-13 15:46:18.536 INFO 8932 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@16958cd8: startup date [Wed Sep 13 15:46:16 CST 2017]; root of context hierarchy
2017-09-13 15:46:18.587 INFO 8932 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/springboot/index]}" onto public java.lang.String com.itxyl.controller.IndexController.index()
2017-09-13 15:46:18.587 INFO 8932 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/springboot/index1]}" onto public java.lang.String com.itxyl.controller.IndexController.index1()
2017-09-13 15:46:18.587 INFO 8932 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/springboot/listindex]}" onto public java.util.List<java.lang.String> com.itxyl.controller.IndexController.listIndex()
2017-09-13 15:46:18.589 INFO 8932 --- [ 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-09-13 15:46:18.589 INFO 8932 --- [ 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-09-13 15:46:18.623 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-13 15:46:18.623 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-13 15:46:18.669 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-13 15:46:18.799 INFO 8932 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-09-13 15:46:18.849 INFO 8932 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-09-13 15:46:18.853 INFO 8932 --- [ main] com.itxyl.service.Application : Started Application in 2.509 seconds (JVM running for 2.734)
@EnableAutoConfiguration为springboot程序入口,一个程序就一个入口,所以可以把他单独的拿出来。