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. 比较下OceanBase的选举协议和Raft的选举协议的区别

    阿里技术大讲堂OceanBase专场中曾有专门一场讲座介绍OB自己实现的分布式选举算法:<分布式选举-破解数据库高可用性难题> 这里简单列一下这个选举算法和raft论文中提到的选举算法的区 ...

  2. 图解 & 深入浅出 JavaWeb:Servlet 再说几句

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  3. FFmpeg编译出错_img_convert 找不到

    问题出现在下载的ffmpeg的版本不一样,在0.4.8以前的版本中还有img_convert这个函数,新版本中用sws_getContext和sws_scale代替了.简单说明如下: 新版本的ffmp ...

  4. SkipList 跳表

    1.定义描述      跳跃列表(也称跳表)是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间).      基本上,跳跃列表是对有序的链表增加 ...

  5. iTunes Connect突然登录不了的原因

    突然使用开发者账号登录不了iTunes Connect了,提示:Your Apple ID or password was entered incorrectly. 这是由于iTunes Connec ...

  6. c/c++: c++继承 内存分布 虚表 虚指针 (转)

    http://www.cnblogs.com/DylanWind/archive/2009/01/12/1373919.html 前部分原创,转载请注明出处,谢谢! class Base  {  pu ...

  7. 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:

    php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql extens ...

  8. Java知多少(107)几个重要的java数据库访问类和接口

    编写访问数据库的Java程序还需要几个重要的类和接口. DriverManager类 DriverManager类处理驱动程序的加载和建立新数据库连接.DriverManager是java.sql包中 ...

  9. 代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用

    代码如下: 1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  10. ActivityGroup里面装的子Activity不执行OnActivityResult()的解决办法

    转自http://blog.csdn.net/snakewarhead/article/details/7976831 tab activity 中调用startActivityForResult 以 ...