spring boot

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

上一篇:iPhone模拟定位(非越狱修改手机定位)


下一篇:iOS中容易混淆的知识点(持续更新中)