spring boot 1.5入门示例代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MaxWoods/article/details/54976357

新建一个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
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer
 {


	class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {

			@Override
		public void customize(Connector connector) {
			Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
			//设置最大连接数
			protocol.setMaxConnections(2000);
			//设置最大线程数
			protocol.setMaxThreads(2000);
			protocol.setConnectionTimeout(30000);
		}
	}

	@Override
	public void customize(ConfigurableEmbeddedServletContainer container)
	{
		container.setPort(8081);
		((TomcatEmbeddedServletContainerFactory)container).addConnectorCustomizers(new MyTomcatConnectorCustomizer());
	}

	@Override
	protected 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: default
2017-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 hierarchy
2017-02-10 23:56:31.478  INFO 7224 --- [ost-startStop-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-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 WebApplicationContext
2017-02-10 23:56:31.616  INFO 7224 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 924 ms
2017-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 hierarchy
2017-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 startup
2017-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"]

猜你喜欢

转载自blog.csdn.net/MaxWoods/article/details/54976357