spring boot 入门示例代码
来源:互联网 发布:上海it helpdesk招聘 编辑:程序博客网 时间:2024/04/30 14:17
新建一个maven工程,pom文件:
<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>maxwoods.sample</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version></parent><properties><jdk.version>1.8</jdk.version><maven.compiler.source>${jdk.version}</maven.compiler.source><maven.compiler.target>${jdk.version}</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--发布到tomcat取消该注释<exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions>--></dependency><!--发布到tomcat取消该注释<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency>--></dependencies><build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><port>8011</port><uriEncoding>UTF-8</uriEncoding></configuration></plugin></plugins></build></project>
springboot.java
package springboot;import org.apache.catalina.connector.Connector;import org.apache.coyote.http11.Http11NioProtocol;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.boot.web.support.SpringBootServletInitializer;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@SpringBootApplication@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {@Overridepublic void customize(Connector connector) {Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();//设置最大连接数protocol.setMaxConnections(2000);//设置最大线程数protocol.setMaxThreads(2000);protocol.setConnectionTimeout(30000);}}@Overridepublic void customize(ConfigurableEmbeddedServletContainer container){container.setPort(8081);((TomcatEmbeddedServletContainerFactory)container).addConnectorCustomizers(new MyTomcatConnectorCustomizer());}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(Application.class); }public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
可以直接运行main方法,或将pom中的注释去掉后,发布到tomcat中,或者直接使用maven tomcat:run运行该示例,以下为maven tomcat:run运行日志:
D:\Develop\Oracle\JDK8\WIN64\bin\java "-Dmaven.multiModuleProjectDirectory=D:\My Code\SpringBoot" -Dmaven.home=D:\Develop\Maven\apache-maven-3.3.9 -Dclassworlds.conf=D:\Develop\Maven\apache-maven-3.3.9\bin\m2.conf -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\Develop\JetBrains\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath "D:\Develop\Maven\apache-maven-3.3.9\boot\plexus-classworlds-2.5.2.jar;D:\Develop\JetBrains\IntelliJ IDEA\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.3.4 -s D:\Develop\Maven\apache-maven-3.3.9\conf\settings.xml org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:run[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building springboot 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] >>> tomcat7-maven-plugin:2.1:run (default-cli) > process-classes @ springboot >>>[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 1 resource[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] <<< tomcat7-maven-plugin:2.1:run (default-cli) < process-classes @ springboot <<<[INFO] [INFO] --- tomcat7-maven-plugin:2.1:run (default-cli) @ springboot ---[INFO] Running war on http://localhost:8011/springboot[INFO] Using existing Tomcat server configuration at D:\My Code\SpringBoot\target\tomcat[INFO] create webapp with contextPath: /springboot二月 10, 2017 11:56:28 下午 org.apache.coyote.AbstractProtocol init信息: Initializing ProtocolHandler ["http-bio-8011"]二月 10, 2017 11:56:28 下午 org.apache.catalina.core.StandardService startInternal信息: Starting service Tomcat二月 10, 2017 11:56:28 下午 org.apache.catalina.core.StandardEngine startInternal信息: Starting Servlet Engine: Apache Tomcat/7.0.37二月 10, 2017 11:56:30 下午 org.apache.catalina.core.ApplicationContext log信息: 2 Spring WebApplicationInitializers detected on classpath . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.1.RELEASE)2017-02-10 23:56:30.646 INFO 7224 --- [ost-startStop-1] springboot.Application : Starting Application on MaxWoods with PID 7224 (started by Max Woods in D:\My Code\SpringBoot)2017-02-10 23:56:30.650 INFO 7224 --- [ost-startStop-1] springboot.Application : No active profile set, falling back to default profiles: default2017-02-10 23:56:30.692 INFO 7224 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@697c4256: startup date [Fri Feb 10 23:56:30 CST 2017]; root of context hierarchy2017-02-10 23:56:31.478 INFO 7224 --- [ost-startStop-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring2017-02-10 23:56:31.513 INFO 7224 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2017-02-10 23:56:31.585 INFO 7224 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2017-02-10 23:56:31.616 INFO 7224 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/springboot] : Initializing Spring embedded WebApplicationContext2017-02-10 23:56:31.616 INFO 7224 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 924 ms2017-02-10 23:56:32.117 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2017-02-10 23:56:32.120 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*]2017-02-10 23:56:32.121 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2017-02-10 23:56:32.121 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-02-10 23:56:32.121 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2017-02-10 23:56:32.121 INFO 7224 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2017-02-10 23:56:32.552 INFO 7224 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@697c4256: startup date [Fri Feb 10 23:56:30 CST 2017]; root of context hierarchy2017-02-10 23:56:32.626 INFO 7224 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/{id}]}" onto java.lang.String springboot.Home.test(java.lang.String)2017-02-10 23:56:32.627 INFO 7224 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String springboot.Home.home()2017-02-10 23:56:32.631 INFO 7224 --- [ost-startStop-1] 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-02-10 23:56:32.631 INFO 7224 --- [ost-startStop-1] 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-02-10 23:56:32.680 INFO 7224 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-02-10 23:56:32.680 INFO 7224 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-02-10 23:56:32.747 INFO 7224 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2017-02-10 23:56:32.956 INFO 7224 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2017-02-10 23:56:32.974 INFO 7224 --- [ost-startStop-1] springboot.Application : Started Application in 2.74 seconds (JVM running for 7.812)2017-02-10 23:56:32.999 INFO 7224 --- [ main] org.apache.coyote.http11.Http11Protocol : Starting ProtocolHandler ["http-bio-8011"]
0 0
- spring boot 入门示例代码
- Spring Boot入门示例
- spring-boot快速入门实践代码
- 【Spring Boot】Spring Boot 入门
- spring boot示例学习
- Spring Boot 示例节选
- Spring Boot AJAX 示例
- Spring Boot 快速入门
- spring boot 入门
- Spring Boot 入门
- Spring Boot 入门
- spring boot简易入门
- Spring boot入门
- Spring Boot 快速入门
- Spring boot快速入门
- Spring boot 入门 实例
- Spring Boot 快速入门
- spring boot入门(一)
- Sql_Server编程 简明教程
- Android的四层结构
- ,有一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger UI,具体配置过程可移步《Spring Boot 利用 Swagger 实现restful测试》
- JSON.NET VS BinaryFormatter 性能
- 蓝桥杯 2012 3 拼音字母
- spring boot 入门示例代码
- Groovy批处理命令
- 初学Android:数据存储技术小结
- Objective-C 数据持久化
- C++拷贝构造函数大全
- UVa 213 信息解码Message Decoding
- JavaScript面试大全1
- 设备管理命令
- AngularJS 国际化 支持记录语言刷新后语言不变