spring boot使用

首先spring-boot是个服务框架,更加准确来讲是个微服务框架,实际上来说”微“并不“微”,spring-boot包含很多可嵌入的组件,通过这些组件可以来完成我们的服务,

以往我们使用Spring的时候,我们需要首先在pom文件中增加对相关的的依赖(使用gradle也是类似)然后新建Spring相关的配置文件而通常是xml,然后后面加入jetty\tomcat,而现在我们通过spring-boot就可以完成我们这些繁杂的事情,下面来介绍下如何使用spring-boot:

1)首先是我们的pom.xml依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>sping_boot_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>
View Code

2)然后就可以写我们的代码了:

启动类:

package Example;

import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
View Code

controler类:

package Example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="users")
public class MyRestController {
    @RequestMapping("/")
    String home(){
        return "Hello World!";
    }
    
    @RequestMapping("/test")
    String test(){
        return "Hello test!";
    }
}
View Code
package Example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Example {
    
    @RequestMapping("/")
    String home(){
        return "Hello World!";
    }
    
    @RequestMapping("/test")
    String test(){
        return "Hello test!";
    }
    
}
View Code

3)启动与运行:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.4.0.BUILD-SNAPSHOT)

2016-06-04 00:06:11.194  INFO 91890 --- [           main] Example.Application                      : Starting Application on adeMacBook-Pro.local with PID 91890 (/Users/apple/Documents/JavaDemoWorkSpace/spring_boot_test/target/classes started by apple in /Users/apple/Documents/JavaDemoWorkSpace/spring_boot_test)
2016-06-04 00:06:11.199  INFO 91890 --- [           main] Example.Application                      : No active profile set, falling back to default profiles: default
2016-06-04 00:06:11.312  INFO 91890 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3b1dd4fb: startup date [Sat Jun 04 00:06:11 CST 2016]; root of context hierarchy
2016-06-04 00:06:13.117  INFO 91890 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-06-04 00:06:13.140  INFO 91890 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-06-04 00:06:13.142  INFO 91890 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-06-04 00:06:13.316  INFO 91890 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-06-04 00:06:13.316  INFO 91890 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2008 ms
2016-06-04 00:06:13.743  INFO 91890 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-06-04 00:06:13.748  INFO 91890 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-06-04 00:06:13.748  INFO 91890 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-06-04 00:06:13.748  INFO 91890 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-06-04 00:06:13.749  INFO 91890 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2016-06-04 00:06:14.000  INFO 91890 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3b1dd4fb: startup date [Sat Jun 04 00:06:11 CST 2016]; root of context hierarchy
2016-06-04 00:06:14.155  INFO 91890 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String Example.Example.home()
2016-06-04 00:06:14.157  INFO 91890 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto java.lang.String Example.Example.test()
2016-06-04 00:06:14.160  INFO 91890 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/]}" onto java.lang.String Example.MyRestController.home()
2016-06-04 00:06:14.161  INFO 91890 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/users/test]}" onto java.lang.String Example.MyRestController.test()
2016-06-04 00:06:14.165  INFO 91890 --- [           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-06-04 00:06:14.165  INFO 91890 --- [           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)
2016-06-04 00:06:14.299  INFO 91890 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-04 00:06:14.299  INFO 91890 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-04 00:06:14.387  INFO 91890 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-04 00:06:14.969  INFO 91890 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-06-04 00:06:15.070  INFO 91890 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-06-04 00:06:15.077  INFO 91890 --- [           main] Example.Application                      : Started Application in 4.612 seconds (JVM running for 5.156)

访问应用:

http://127.0.0.1:8080/users/test

 

参考:

http://docs.spring.io/

https://github.com/spring-projects/spring-boot

 

你可能感兴趣的:(spring boot使用)