程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 一個最小化的SpringBoot項目,最小化springboot

一個最小化的SpringBoot項目,最小化springboot

編輯:JAVA綜合教程

一個最小化的SpringBoot項目,最小化springboot


項目結構

項目基於Maven管理,注意使用了父pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
        <relativePath /> 
    </parent>

 

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>test.demo</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot</name>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.0.RELEASE</version>
		<relativePath /> 
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

  App.class

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@EnableAutoConfiguration
@RestController
public class App
{
    public static void main(String[] args)
    {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String get()
    {
        return "get";
    }

    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public String post()
    {
        return "post";
    }
}

@EnableAutoConfiguration注解的作用在於讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置

@EnableAutoConfiguration注解上注釋的翻譯

自動配置Spring上下文,企圖猜測所需要的Bean,將classpath和你所定義的Bean加入到IOC容器,比如你有tomcat-embedded.jar在你的classpath上

那麼看起來你需要一個TomcatEmbeddedServletContainerFactory,那麼SpringBoot會給你生成一個這個類的Bean(除非你已經生成這個類的Bean)

你可以使用exclude來指定那些Bean不需要SpringBoot幫你生成Bean

public @interface EnableAutoConfiguration {

    /**
     * Exclude specific auto-configuration classes such that they will never be applied.
     * @return the classes to exclude
     */
    Class<?>[] exclude() default {};

    /**
     * Exclude specific auto-configuration class names such that they will never be
     * applied.
     * @return the class names to exclude
     * @since 1.3.0
     */
    String[] excludeName() default {};

}

在你的root package上使用這個注解,它會將這個目錄下所有的子目錄和類掃描一遍

@RestController和@RequestMapper注解由SpringMvc提供,用於創建Rest服務

關於@RequestMapper注解

value表示路徑映射,method說明請求這個路徑的Http方法

啟動日志解讀

2016-03-11 13:58:02.399  INFO 5776 --- [           main] hello.App                                : Starting App on zzzzzz with PID 5776 (E:\space\spring\springboot\target\classes started by zzzzz in E:\space\spring\springboot)
2016-03-11 13:58:02.404  INFO 5776 --- [           main] hello.App                                : No profiles are active
2016-03-11 13:58:02.508  INFO 5776 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy
2016-03-11 13:58:03.807  INFO 5776 --- [           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]]
2016-03-11 13:58:05.107  INFO 5776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-03-11 13:58:05.126  INFO 5776 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-03-11 13:58:05.128  INFO 5776 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2016-03-11 13:58:05.268  INFO 5776 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-03-11 13:58:05.268  INFO 5776 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2769 ms
2016-03-11 13:58:05.673  INFO 5776 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-03-11 13:58:05.680  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-03-11 13:58:05.681  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-03-11 13:58:05.681  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-03-11 13:58:05.682  INFO 5776 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-03-11 13:58:06.022  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@481a15ff: startup date [Fri Mar 11 13:58:02 CST 2016]; root of context hierarchy
2016-03-11 13:58:06.115  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()
2016-03-11 13:58:06.116  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post()
2016-03-11 13:58:06.121  INFO 5776 --- [           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)
2016-03-11 13:58:06.121  INFO 5776 --- [           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)
2016-03-11 13:58:06.178  INFO 5776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-11 13:58:06.178  INFO 5776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-11 13:58:06.225  INFO 5776 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-11 13:58:06.363  INFO 5776 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-03-11 13:58:06.468  INFO 5776 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-03-11 13:58:06.473  INFO 5776 --- [           main] hello.App                                : Started App in 4.736 seconds (JVM running for 5.798)

 注意查看紅色的部分,dispatcherServlet是SpringWeb的核心Servlet,所有進入SpringWeb的請求都由這個Servelt的service方法進入

2016-03-11 13:58:06.115  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String hello.App.get()
2016-03-11 13:58:06.116  INFO 5776 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public java.lang.String hello.App.post()
表示我們聲明的路徑映射

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved