`
kinfer
  • 浏览: 7599 次
社区版块
存档分类
最新评论

spring boot 起步-原型实例

 
阅读更多

自从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!"出现啦...

 

下篇待续...

  • 大小: 77 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics