天天看點

Springboot入門程式

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遠遠不止這麼點功能,還有很多了,一起探索吧