spring boot 是什么

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。 
spring boot采用了“约定优于配置” 的理念,减少了spring繁琐的配置,方便快速搭建应用 
spring boot官网:http://projects.spring.io/spring-boot/

maven配置:

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

  

配置解读:

  • 继承spring-boot-starter-parent后我们可以继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化
  • spring-boot-starter-web提供了对web的支持,提供了嵌入式Tomcat容器以及端点信息:如服务器信息、应用指标(metrics)以及环境详情

hello world

官网中的hello-world,示例如下:

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*; @Controller // 标识是web的controller类
@EnableAutoConfiguration // 允许自动加载配置,Boot要采用一种特定的方式来对应用进行配置
public class SampleController {
@RequestMapping("/") // 对应restful url的相对目录
@ResponseBody // 将返回直接作为response的body
String home() {
return "Hello World!";
} // 启动应用程序
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
 

main分离

当如果有多个controller的时候,直接把main放在一个controller里面是不合适的,那么就把main单独拆分出来

UserController.class:

package sailorxiao.SpringBootSample.controller; // 约定的controller都放在controller目录下

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*; @Controller
@RequestMapping("/user")
@EnableAutoConfiguration
public class UserController {
public String helloUser() {
return "Hello user!";
}
}
main: package sailorxiao.SpringBootSample; // 默认的main对应的class应该放在项目src代码的一级目录下,默认使用Application import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan; @EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

  

如图所示,对于main函数,没法放到单独controller里面,因此:

  • @EnableAutoConfiguration 标志在对应的主要class上
  • @ComponentScan 允许boot依据约定的规则对项目中的bean进行扫描
 

增加@Autowired自动装配

通常在controller中会有一些逻辑交互,需要做一些处理(比如读写db,进行一些业务逻辑等),一般会把相关的操作放到对应的service下,如下,对UserContrller进行处理,增加UserService 
UserController:

package sailorxiao.SpringBootSample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import sailorxiao.SpringBootSample.entity.User;
import sailorxiao.SpringBootSample.service.IHelloService;
import sailorxiao.SpringBootSample.service.IUserService; @RestController
public class UserController {
@Autowired
private IUserService userService; //使用了自动装备,boot会自动装配对应的service对象 @RequestMapping("/hello/{name}") // rest path中带着name,完整的url就是 http://xxxx/user/{name}
public String helloUser(@PathVariable("name") String name) {
return userService.helloUser(name);
}
}

  

service目录下

IUserService:

package sailorxiao.SpringBootSample.service; // 所有的service约定都放在service目录下

public interface IUserService { // 对于每个service需要定义好一个interface,直接放在service目录下,名字为IXXXService
public String helloUser(String user);
}

  

IUserServiceImpl:

package sailorxiao.SpringBootSample.service.impl;

import org.springframework.stereotype.Component;
import sailorxiao.SpringBootSample.service.IUserService; @Component // 需要加上这个标签,这样main的ComponentScan才能扫描到对应的实现
public class UserServiceImpl implements IUserService {
@Override
public String helloUser(String user) {
return "hello " + user;
}
}

http body 约束

很多时候,会希望传入的是一些特定规则的参数,比如我们希望在UserController中传入User对象{name:xxx,age:xxx} 
controller:

package sailorxiao.SpringBootSample.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import sailorxiao.SpringBootSample.entity.User;
import sailorxiao.SpringBootSample.service.IUserService; @RestController
public class UserController {
@Autowired
private IUserService userService; @RequestMapping("/hello/{name}")
public String helloUser(@PathVariable("name") String name) {
return userService.helloUser(name);
} @RequestMapping(value = "/create", method = RequestMethod.POST)
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
}

  

User对象:

package sailorxiao.SpringBootSample.entity; // 一般而言与用户相关的对象都放在entity目录下

public class User { // User对象是个bean,有默认的set/get方法
private String name;
private int age; public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
}
}

  

注意,在client使用json作为body传递给server的controller的时候,需要标志contentType是"application/json",如使用以下方式:

HttpClient httpClient = new HttpClient();

StringRequestEntity requestEntity = new StringRequestEntity(
jsonStr, // json的序列化后的string,比如这里应该就是USER对象序列化成json的str
"application/json",
"UTF-8"); PostMethod postMethod = new PostMethod(url);
postMethod.setRequestEntity(requestEntity); httpClient.executeMethod(postMethod); String resStr = postMethod.getResponseBodyAsString();
System.out.println("post res: " + resStr);

resource

有时候需要使用配置,那么在boot里面使用配置非常简单,如下,在定义配置后,只需要在程序中声明

application.properties:

host="192.168.137.10"
port=34001
properties类: @ConfigurationProperties // 声明这个是properties的bean
public class BootMongoProperties {
private String host;
private int port;
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return this.port;
}
public void setPort(int port) {
this.port = port;
}
}

  

如果配置都是以一定名称打头的,可以加上prefix字段,如:

@ConfigurationProperties(prefix = "spring.data.mongodb")
public class BootMongoProperties {
private String host; //表示配置项为spring.data.mongodb.host
private int port;
private String uri = "mongodb://localhost/test"; // 如果配置中没有配置spring.data.mongodb.uri的话,默认为该值
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return this.port;
}
public void setPort(int port) {
this.port = port;
}
}

mongodb支持

一般而言web服务中需要使用到db,那么在spring boot里面是如何进行db相关操作的呢?以mongodb为例只需要三部

第一步,配置db相关配置,如图在application.properties中配置mongodb的uri

spring.data.mongodb.uri=mongodb://sailor:sailor@192.168.137.10:34001/sailor?autoConnectRetry=true&connectTimeout=60000
spring.data.mongodb.repositories.enabled=true

第二步,声明mongoTemplate,mongodb的driver中,相关操作都是通过mongoTemplate进行的

@Resource  // @Resource表示mongoTemplate直接基于配置生成对应的mongoTemplate操作实例
private MongoTemplate mongoTemplate;

第三步,使用mongoTemplate

User user = new User(name, age); // 定义一个collection相关的bean
mongoTemplate.insert(user); // 从mongo中插入该数据,插入到user对应的类名的集合中(这里就是user) User类:
public class User {
private String name;
private int age; public User() {
// do nothing
} public User(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
}
}

  

通过以上三步,就能很简单得实现boot操作对应的db

 

问题

通过java -jar运行时,提示找不到main函数

解决方案,在pom.xml中加入mainClass选项,指明对应的mainClass

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>SailorXiao.spring.main.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

  

spring boot学习笔记的更多相关文章

  1. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  2. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  3. Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  4. Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  5. Spring Boot 学习笔记--整合Thymeleaf

    1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...

  6. 我的第一个spring boot程序(spring boot 学习笔记之二)

    第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...

  7. Java框架spring Boot学习笔记(六):Spring Boot事务管理

    SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.

  8. Spring Boot学习笔记---Spring Boot 基础及使用idea搭建项目

    最近一段时间一直在学习Spring Boot,刚进的一家公司也正好有用到这个技术.虽然一直在学习,但是还没有好好的总结,今天周末先简单总结一下基础知识,等有时间再慢慢学习总结吧. Spring Boo ...

  9. spring boot学习笔记2

    开场知识: spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间先后顺序: 1)BeanFactoryPostProcessor 容器初始化的回调方法 * BeanFactoryP ...

  10. Spring Boot学习笔记二

    Spring Boot入门第二篇 第一天的详见:https://www.cnblogs.com/LBJLAKERS/p/12001253.html 同样是新建一个pring Initializer快速 ...

随机推荐

  1. python 跳出嵌套循环方法

    class LoopError(Exception):pass rs = '' try: for i in range(1, 3): print i rs = 'one ' if i == 1: fo ...

  2. Java8简明教程(转载)

    ImportNew注:有兴趣第一时间学习Java 8的Java开发者,欢迎围观<征集参与Java 8原创系列文章作者>. 以下是<Java 8简明教程>的正文. “Java并没 ...

  3. LiveWriter Test

    From LiveWriter.

  4. c/c++:重载 覆盖 隐藏 overload override overwrite

    http://www.cnblogs.com/qlee/archive/2011/07/04/2097055.html 成员函数的重载.覆盖与隐藏成员函数的重载.覆盖(override)与隐藏很容易混 ...

  5. [JAVA词形还原工具]Snowball

    demo:http://snowball.tartarus.org/demo.php jar download:http://snowball.tartarus.org/download.php (J ...

  6. 导出excel和PDF小结 vba

    最近接触了一个关于Access工具的项目,所以整理下需要使用的方法. 功能要求简介: 1.将数据表中的数据导出到excel和PDF 2.并根据某个字段名称分sheet输出. 3.无模板方式 方案简介: ...

  7. joomla3.1安装不通过Magic Quotes GPC解决方法

    测试安装下joomla 3.1稳定版,但是不能成功,Magic Quotes GPC始终显示 否红色,这样就安装不了了! 要解决这个很简单,开启Magic Quotes GPC就行了,于是找到php. ...

  8. Linux高级编程--08.线程概述

    线程 有的时候,我们需要在一个基础中同时运行多个控制流程.例如:一个图形界面的下载软件,在处理下载任务的同时,还必须响应界面的对任务的停止,删除等控制操作.这个时候就需要用到线程来实现并发操作. 和信 ...

  9. ruby -- 进阶学习(六) devise修改邮件发送者邮箱

    在config/environment.rb/development.rb或者config/environment/production.rb中, 简单示范例子: Text03::Applicatio ...

  10. 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现

    概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...