Spring学习之boot(1)

来源:互联网 发布:美图都用什么软件 编辑:程序博客网 时间:2024/06/06 17:24

Spring boot 可以提升开发效率简化spring.xml配置文件首先是一个helloworld 小例子:

package web.app.test.boot;import java.util.concurrent.atomic.AtomicLong;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@EnableAutoConfigurationpublic class TestController {public final String temp = "Hello,%s";public final AtomicLong a1 = new AtomicLong();@RequestMapping("/test")@ResponseBodyAnimal home(String name){return new Animal((int) a1.incrementAndGet(),String.format(temp, name));}public static void main(String[] args) {SpringApplication.run(TestController.class, args);}}
实体类就不往上写了  Animal  属性 id  name 

下面是maven pom.xml的配置

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.3.RELEASE</version></parent><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency></dependencies>
此时直接右键run as 就能跑了

启动成功可以用  http://localhost:8080/test?name=world  地址测试了;

PS:1、一定注意要是Maven的web项目(web.xml完全不用配置)

        2、修改代码后一定要先停服务在启动。(好像也可以配置成修改代码后自动重启的,可以百度下)

        3、也可参照官网http://projects.spring.io/spring-boot/


不忘初心,方得始终!

0 0
原创粉丝点击