spring boot学习笔记

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:[email protected]: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>

  

时间: 2024-09-01 12:05:59

spring boot学习笔记的相关文章

Spring boot 学习笔记 (二)- 整合MyBatis

Spring boot 学习笔记 (二)- 整合MyBatis Spring Boot中整合MyBatis,并通过注解方式实现映射. 整合MyBatis 以Spring boot 学习笔记 (一)- Hello world 为基础项目,在pom.xml中添加如下依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter&l

Spring boot 学习笔记 - Hello world

Spring boot 学习笔记 - Hello world spring boot介绍: spring-boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. Hello world 通过官网https://start.spring.io/

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

前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文主要围绕以下几点进行说明: Spring Boot 简介 使用Spring Boot快速搭建一个Web应用如有不对的地方,请指正. 1. Spring Boot简介 Spring Boot是一个基于Spring的衍生框架,其主要的目的是帮助我们快速构建独立.生产级别的Spring的应用,其崇尚的理念

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

第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相关知识点时会再次理解.要运行起第一个Spring boot特别简单,用IDEA包含了Spring Boot的引导,直接新建一个spring boot项目. 注意: 1.第一次新建Spring boot项目的时候,maven会下载大量的依赖到本地,所以特别慢,耐心等待或者用国内的maven公库都行(自

spring boot学习笔记一

一直以来用java做web开发总是需要解决各种依赖和配置很是烦人spring boot在这方面做的很好.用maven管理项目只需要简单的引入几个包就可以做web开发.下面是一个例子 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2

Spring Boot 学习笔记--整合Thymeleaf

1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency> 2.添加静态文件 根据spring boot默认规则,脚本和样式默

spring boot 学习笔记(1)

@ComponentScan, @EntityScan 和@SpringBootApplication ,我们通常用它来注释启动类.spring boot的启动类也就是main类一般都放在根文件夹下.比如: package testRedis.main; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifi

spring boot整合jsp的那些坑(spring boot 学习笔记之三)

Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <depend

spring boot 学习笔记

Spring Boot系列(一):Spring Boot 入门篇 Spring Boot系列(二):web综合开发 Spring Boot系列(三):Spring Boot中Redis的使用 Spring Boot系列(四):thymeleaf使用详解 Spring Boot系列(五):spring data jpa的使用 Spring Boot系列(六):如何优雅的使用mybatis Spring Boot系列(七):springboot+mybatis多数据源最简解决方案 Spring Bo