您的位置:首页 > 编程语言 > Java开发

springboot(一)

2016-01-10 00:00 591 查看
摘要: 最近由于项目改造,使用了学习并使用了springboot,至于为什么需要使用springboot主要有以下几个考虑

1、用户量小,并发要求不高。

2、要求稳定,不出错。

3、打包发布简单、易用,我所面临的项目可能每天要发布多个包,

3、“轻”,项目架构单薄。

最近由于项目改造,使用了学习并使用了springboot,至于为什么需要使用springboot主要有以下几个考虑

1、用户量小,并发要求不高。

2、要求稳定,不出错。

3、打包发布简单、易用,我所面临的项目可能每天要发布多个包,

3、“轻”,项目架构单薄。

因此我使用springboot来解决我的问题。

第一步,构建一个maven项目,结构如下



第二部,引入springboot依赖的jar

<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>SpringBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringBootDemo</name>
<url>http://maven.apache.org</url>

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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

第三部,创建一个启动类“Startup”,如下代码,

package com.springdemo;

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

@Configuration // 配置控制
@EnableAutoConfiguration // 启用自动配置
@ComponentScan // 组件扫描
public class Startup {

// 入口
public static void main(String[] args) {
SpringApplication.run(Startup.class, args);
}
}

第四部,创建一个“IndexController”,如下代码,

package com.springdemo.www;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {
@RequestMapping(value="/",method=RequestMethod.GET)
@ResponseBody
public String index() {
return "say hello";
}
}

第五步,启动,

启动之前首先来看一下我们目前的项目目录结构:


,这其中有一个地方需要注意,就是“startup”类一定要放在所有类的最外围,在这一点确认无误的情况下,然后右键"startup" ,选择run as java application,会在console中看到如下启动信息,

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.3.0.RELEASE)

2016-01-10 21:21:15.438  INFO 11172 --- [           main] com.springdemo.Startup                   : Starting Startup on zhaoiang-PC with PID 11172 (E:\005-Work\SpringBootDemo\target\classes started by Administrator in E:\005-Work\SpringBootDemo)
2016-01-10 21:21:15.441  INFO 11172 --- [           main] com.springdemo.Startup                   : No profiles are active
2016-01-10 21:21:15.517  INFO 11172 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@713d7dc3: startup date [Sun Jan 10 21:21:15 CST 2016]; root of context hierarchy
2016-01-10 21:21:16.552  INFO 11172 --- [           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-01-10 21:21:17.468  INFO 11172 --- [           main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-01-10 21:21:17.470  INFO 11172 --- [           main] org.eclipse.jetty.server.Server          : jetty-9.2.14.v20151106
2016-01-10 21:21:17.576  INFO 11172 --- [           main] application                              : Initializing Spring embedded WebApplicationContext
2016-01-10 21:21:17.576  INFO 11172 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2062 ms
2016-01-10 21:21:18.188  INFO 11172 --- [           main] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-10 21:21:18.191  INFO 11172 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-10 21:21:18.191  INFO 11172 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-10 21:21:18.191  INFO 11172 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-10 21:21:18.191  INFO 11172 --- [           main] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-10 21:21:18.482  INFO 11172 --- [           main] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@13f01f0{/,file:/C:/Users/Administrator/AppData/Local/Temp/jetty-docbase.3476147575260133334.8080/,AVAILABLE}
2016-01-10 21:21:18.482  INFO 11172 --- [           main] org.eclipse.jetty.server.Server          : Started @4149ms
2016-01-10 21:21:18.643  INFO 11172 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@713d7dc3: startup date [Sun Jan 10 21:21:15 CST 2016]; root of context hierarchy
2016-01-10 21:21:18.725  INFO 11172 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.springdemo.www.IndexController.index()
2016-01-10 21:21:18.730  INFO 11172 --- [           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-01-10 21:21:18.731  INFO 11172 --- [           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-01-10 21:21:18.809  INFO 11172 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-10 21:21:18.810  INFO 11172 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-10 21:21:18.870  INFO 11172 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-10 21:21:19.157  INFO 11172 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-10 21:21:19.173  INFO 11172 --- [           main] application                              : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-10 21:21:19.173  INFO 11172 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-10 21:21:19.189  INFO 11172 --- [           main] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
2016-01-10 21:21:19.212  INFO 11172 --- [           main] o.eclipse.jetty.server.ServerConnector   : Started ServerConnector@4c7051c0{HTTP/1.1}{0.0.0.0:8080}
2016-01-10 21:21:19.214  INFO 11172 --- [           main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2016-01-10 21:21:19.221  INFO 11172 --- [           main] com.springdemo.Startup                   : Started Startup in 4.567 seconds (JVM running for 4.888)

第六步,验证,

在浏览器输入http://127.0.0.1:8080/,会打印出我们输入的信息“say hello”,一个简单的springboot程序到此就完成了。

后面我会讲到如何在springboot中整合freemark以及常见配置,点击查看更多

项目代码:https://yunpan.cn/creBhskTsMGFh 访问密码 65e5

------------------------------------

群:【518397333】欢迎求职者、HR、C#,PHP,Java,C++,IOS,Android,.NET,DBA,UI加入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: