码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot

时间:2014-07-09 17:20:57      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:style   http   使用   art   cti   io   

Spring-boot 项目提供了快速启动一个Spring项目

好处:

1.使用mvn spring:boot直接启动一个Web应用。

2.提供缺省的依赖。

环境: jdk1.7   eclipse

使用maven导入spring-boot 包:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
</dependencies>

两种简单注入实例:

实体类:

package com.boful.domin;

public class Consumer {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Action类:

第一种、

@Controller表示这个一个controller类(from spring mvc);

@EnableAutoConfiguration声明让spring boot自动给程序进行必要的配置(from spring boot);

@RequestMapping("/sayHello")表示通过/sayHello可以访问的方法(from spring mvc);

@ResponseBody 表示将结果直接返回给调用者(from spring mvc).

 

 

package com.boful.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boful.domin.Consumer;
//开启自动配置
@EnableAutoConfiguration
@RestController
@RequestMapping("/com/boful/domin/Consumer")
public class TestController {
@RequestMapping("/{id}")
public Consumer list(@PathVariable("id") Long id){
Consumer consumer = new Consumer();
consumer.setId(id);
consumer.setName("zs");
return consumer;

}

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestController.class);
TestController testController = (TestController)context.getBean("testController");
Consumer consumer = testController.list((long)1);
System.out.println(consumer.getName());
}
}

第二种、

通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean,也可以启动添加 Consumer 实体

package com.boful.controller;

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

import com.boful.domin.Consumer;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class AppController {
public Consumer list(Long id){
Consumer consumer = new Consumer();
consumer.setId(id);
consumer.setName("zs");
return consumer;

}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(TestController.class);
TestController testController = (TestController)context.getBean("testController");
Consumer consumer = testController.list((long)1);
System.out.println(consumer.getName());
}
}

 

 

 

spring boot,布布扣,bubuko.com

spring boot

标签:style   http   使用   art   cti   io   

原文地址:http://www.cnblogs.com/lvy6/p/3831993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!