自从struts2出现上次的漏洞以后 对spring的关注度开始越来越浓;
以前也那spring做过几个项目 感觉是xml配置文件需要写一大堆,后来spring加入了annotation,使得xml配置简化了很多,当然还是有些配置需要使用xml,比如申明component scan等。
前段时间发现了spring开了一个新的model spring boot,主要思想就是降低spring的入门,使得新手可以以最快的速度让程序在spring框架下跑起来。
公司以后的项目打算都使用spring boot为基础进行开发,所以打算将spring boot所有的特性都记录在博文中,也就是说些一个spring boot的系列吧,废话少说,先上第一步,看看spring boot怎么以最简单的方式让程序跑起来的 而且还是一个web项目。
首先 使用eclipse jee创建一个maven工程,一般的工程就可以(我用的是quickstart),工程就叫spring-boot-sample;
第二步 在pom.xml中引入spring-boot-start-parent,spring官方的解释叫什么stater POMs,它可以提供dependency management,也就是说依赖管理,应该是说工程需要依赖的jar包的管理,引入以后在申明其他dependency的时候就不需要version了,后面可以看到。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.1.RELEASE</version> </parent>
第三步 因为我们开发的是web工程,所以需要在pom.xml中引入spring-boot-starter-web,spring官方解释说spring-boot-starter-web包含了spring webmvc和tomcat等web开发的特性。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
第四步 真正的程序开始啦,我们需要一个启动类 然后在这个启动类中申明让spring boot自动给我们配置spring需要的配置,比如component scan等;为了可以尽快让程序跑起来,我们使用spring官方的实例:
@Controller @EnableAutoConfiguration public class App { @RequestMapping("/") @ResponseBody public String hello(){ return "Hello world!"; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }
其中@EnableAutoConfiguration申明让spring boot自动给程序进行必要的配置;
@Controller表示这个一个controller类;
@RequestMapping("/") 表示通过/可以访问的方法;
@ResponseBody 表示将结果直接返回给调用着。
第五步 右键pom.xml -> debug configuration,在Goals里输入 spring-boot:run,然后点击Apply,最后点击Debug。
最后会出现以下log:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.1.RELEASE)
2014-04-13 00:06:16.992 INFO 3472 --- [mple.App.main()] com.sample.App : Starting App on XUMAM4F7Q5P41FE with PID 3472 (E:\jee_workspace\spring-boot-sampe\target\classes started by Administrator)
2014-04-13 00:06:17.254 INFO 3472 --- [mple.App.main()] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1c239aa: startup date [Sun Apr 13 00:06:17 GMT+08:00 2014]; root of context hierarchy
2014-04-13 00:06:19.630 INFO 3472 --- [mple.App.main()] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-04-13 00:06:20.086 INFO 3472 --- [mple.App.main()] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-04-13 00:06:20.086 INFO 3472 --- [mple.App.main()] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-04-13 00:06:20.568 INFO 3472 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-04-13 00:06:20.578 INFO 3472 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3334 ms
2014-04-13 00:06:22.134 INFO 3472 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-04-13 00:06:22.154 INFO 3472 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-04-13 00:06:22.787 INFO 3472 --- [mple.App.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-13 00:06:23.061 INFO 3472 --- [mple.App.main()] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.sample.App.hello()
2014-04-13 00:06:23.130 INFO 3472 --- [mple.App.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-13 00:06:23.130 INFO 3472 --- [mple.App.main()] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-13 00:06:23.623 INFO 3472 --- [mple.App.main()] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-04-13 00:06:23.724 INFO 3472 --- [mple.App.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-04-13 00:06:23.725 INFO 3472 --- [mple.App.main()] com.sample.App : Started App in 7.867 seconds (JVM running for 18.295)
这个时候在浏览器中访问http://localhost:8080/ ,你会发现"Hello world!"出现啦...
下篇待续...
相关推荐
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...
赠送jar包:spring-boot-configuration-processor-2.6.3.jar; 赠送原API文档:spring-boot-configuration-processor-2.6.3-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.6.3-sources.jar; ...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:spring-boot-configuration-processor-2.5.6.jar; 赠送原API文档:spring-boot-configuration-processor-2.5.6-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.5.6-sources.jar; ...
赠送jar包:spring-boot-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-autoconfigure-2.3.12.RELEASE-sources.jar; 赠送...
赠送jar包:druid-spring-boot-starter-1.1.10.jar; 赠送原API文档:druid-spring-boot-starter-1.1.10-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.10-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:aliyun-sms-spring-boot-starter-2.0.2.jar 赠送原API文档:aliyun-sms-spring-boot-starter-2.0.2-javadoc.jar 赠送源代码:aliyun-sms-spring-boot-starter-2.0.2-sources.jar 包含翻译后的API文档...
赠送jar包:oss-spring-boot-starter-1.0.3.jar; 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar; 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar; 赠送Maven依赖信息文件:oss-spring-boot...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:nacos-config-spring-boot-autoconfigure-0.2.7.jar; 赠送原API文档:nacos-config-spring-boot-autoconfigure-0.2.7-javadoc.jar; 赠送源代码:nacos-config-spring-boot-autoconfigure-0.2.7-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:druid-spring-boot-starter-1.2.8.jar; 赠送原API文档:druid-spring-boot-starter-1.2.8-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.2.8-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:spring-boot-admin-server-ui-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-ui-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-ui-2.5.2-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:oss-spring-boot-starter-1.0.3.jar 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar 包含翻译后的API文档:oss-spring-boot-...
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom
赠送jar包:aliyun-sms-spring-boot-starter-2.0.2.jar; 赠送原API文档:aliyun-sms-spring-boot-starter-2.0.2-javadoc.jar; 赠送源代码:aliyun-sms-spring-boot-starter-2.0.2-sources.jar; 赠送Maven依赖信息...