第一章 初识springBoot
最近工作不是很忙!可以闲下来写点东西了。先说优点吧!
1: Spring Boot使编码变简单
2: Spring Boot使配置变简单
3: Spring Boot使部署变简单
4: Spring Boot使监控变简单
具体好与坏!等我们掀起她的盖头之后再做评价吧。。
springboot由于自身嵌入tomcat-embed-el等原因使其可以不用依赖于外部的tomcat去单独启动服务。
以下是简单示例实现RESTAPI访问
步骤一
官网下载相应jar: http://projects.spring.io/spring-boot/ 目前本人使用版本为1.5.4.RELEASE
步骤二
直接上图吧
具体代码如下
1:testController.java
package controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/springboot")
public class testController {
@RequestMapping(value = "/rest")
public String getData() {
return "恭喜恭喜! 你已初步使用springboot的RestAPI访问!";
}
}
2:start.java
package applicationStart;
import java.net.UnknownHostException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
@Configuration
@ComponentScan(basePackages = "controller")
@SpringBootApplication
public class Start {
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(Start.class);
ConfigurableApplicationContext ConfigurableApplicationContext= app.run(args);
String beanNames[]=ConfigurableApplicationContext.getBeanFactory().getBeanNamesForAnnotation(RestController.class);
System.out.println("所以beanNames个数:"+beanNames.length);
for(String bn:beanNames){
System.out.println(bn);
}
}
}