SpringBoot学习笔记(1) Spirng boot 初探

本文介绍SpringBoot项目的快速启动方法及核心特点,包括如何创建独立的Java应用和Web应用,使用默认配置实现快速开发,以及非功能性特性的集成。文中详细展示了如何搭建SpringBoot环境,并通过实例说明了配置文件的使用。

Spring Boot 项目旨在简化创建产品级的 Spring应用和服务。你可通过它来选择不同的Spring平台。可创建独立的Java应用和Web应用,同时提供了命令行工具来允许'spring scripts'.

 

该项目主要的目的是:

 

l 为 Spring 的开发提供了更快更广泛的快速上手

l 使用默认方式实现快速开发

l 提供大多数项目所需的非功能特性,诸如:嵌入式服务器、安全、心跳检查、外部配置等

Spring Boot 不生成代码,完全无需 XML配置。

 

Spirng boot可以以jar包的形式独立运行,运行一个Spring boot项目只需通过java -jar xx.jar来运行,Spring boot还内嵌了servlet容器,提供一系列的starter pom 来简化maven的依赖加载,Spirng boot还会自动配置spring ,提供基于httpsshtelnet对运行的项目进行监控


Quick Start

http://start.spring.io/

可以通过spring官网来生成


上面那里 项目中使用的技术 那里是搜索的

具体有哪些可以查看下面


然后直接将生成的项目导入到ide中就OK

 

 

除了自动生成的,也可以自己创建一个maven项目

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.ibigsea</groupId>
	<artifactId>bootdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>bootdemo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<boot.version>1.3.1.RELEASE</boot.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>${boot.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<version>${boot.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

然后创建一个普通的类

package com.ibigsea.bootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 是Spring Boot项目的核心注解,主要是开启自动配置
 */
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
@RestController
public class App {
	
	@RequestMapping("/")
	public String index(){
		return "Hello Spring Boot";
	}
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

运行就可以访问到了



基本配置

Spirng boot通常都有一个入口类,入口类一个main方法.

@SpringBootApplication Spring Boot的核心注解,并且是一个组合注解

这个注解相当于@Configuration@EnableAutoConfiguration@ComponentScan


Spirng boot 一些注解解释

http://dailycode.daoapp.io/spring-boot-annotations/

 

@EnableAutoConfiguration 注解会根据类路径中的jar包依赖为当前项目进行自动配置

For example:

添加spring-boot-starter-web依赖,会自动添加tomcatspring mvc的依赖,那么Spring Boot会对TomcatSpirngMVC进行自动配置

 

修改默认启动图案

resources下面放一个banner.txt文件





也可以关闭这个图案


Spring Boot配置文件

Spring Boot使用一个全局配置文件,application.propertiesapplication.yml放置在src/main/resources下面或者类路径的/config下面



在默认情况下spring boot 使用/为访问路径 使用8080为 默认端口

新增application.properties

server.port=8081
server.context-path=/demo

Starter pom

Starter pom是可以包含应用中的一个方便的依赖关系描述集合,例如,如果你想使用SpringJPA进行数据库访问,只需要在你的项目中包含spring-boot-starter-data-jpa依赖,然后你就可以开始了。

 

具体资料:

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-starters

 

Spirng Boot 通过 @ImportResource注解来加载xml配置

例如

@ImportResource({"classpath*:spring-*.xml", "classpath*:*-context.xml"})

除了在application.properties 文件中添加配置信息以外,还可以通过命令行来进行配置

java -jar bootdemo.jar --server.port=8081 

Spring Boot 基础教程(基于1.3.x-1.5.x) 快速入门 chapter1基本项目构建(可作为工程脚手架),引入web模块,完成一个简单的RESTful API 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程 工程配置 chapter2-1-1配置文件详解:自定义属性、随机数、多环境配置等 chapter2-1-2:2.0 新特性(一):配置绑定全解析 chapter2-2-1:2.0 新特性(二):新增事件ApplicationStartedEvent Web开发 chapter3-1-1:构建一个较为复杂的RESTful API以及单元测试 chapter3-1-2:使用Thymeleaf模板引擎渲染web视图 chapter3-1-3:使用Freemarker模板引擎渲染web视图 chapter3-1-4:使用Velocity模板引擎渲染web视图 chapter3-1-5:使用Swagger2构建RESTful API chapter3-1-6:统一异常处理 chapter3-1-7:使用Java 8中LocalDate等时间日期类的问题解决 chapter3-1-8:扩展XML请求和响应的支持 数据访问 chapter3-2-1:使用JdbcTemplate chapter3-2-2:使用Spring-data-jpa简化数据访问层(推荐) chapter3-2-3:多数据源配置(一):JdbcTemplate chapter3-2-4:多数据源配置(二):Spring-data-jpa chapter3-2-5:使用NoSQL数据库(一):Redis chapter3-2-6:使用NoSQL数据库(二):MongoDB chapter3-2-7:整合MyBatis chapter3-2-8:MyBatis注解配置详解 chapter3-2-9:使用Flyway来管理数据库版本 chapter3-2-10:使用LDAP来统一管理用户信息 chapter3-2-11Spring Boot中增强对MongoDB的配置(连接池等) 事务管理 chapter3-3-1:使用事务管理 chapter3-3-2:[分布式事务(未完成)] 其他内容 chapter4-1-1:使用@Scheduled创建定时任务 chapter4-1-2:使用@Async实现异步调用 chapter4-1-3:使用@Async实现异步调用:自定义线程池 chapter4-1-4:使用@Async实现异步调用:资源优雅关闭 chapter4-1-5:使用@Async实现异步调用:使用Future以及定义超时 日志管理 chapter4-2-1:默认日志的配置 chapter4-2-2:使用log4j记录日志 chapter4-2-3:对log4j进行多环境不同日志级别的控制 chapter4-2-4:使用AOP统一处理Web请求日志 chapter4-2-5:使用log4j记录日志到MongoDB chapter4-2-6:Spring Boot 1.5.x新特性:动态修改日志级别] 安全管理 chapter4-3-1:使用Spring Security chapter4-3-2:[使用Spring Session(未完成)] 缓存支持 chapter4-4-1:注解配置与EhCache使用 chapter4-4-2:使用Redis做集中式缓存 邮件发送 chapter4-5-1:实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件 消息服务 chapter5-1-1:[JMS(未完成)] chapter5-2-1Spring Boot中使用RabbitMQ 其他功能 chapter6-1-1:使用Spring StateMachine框架实现状态机 Spring Boot Actuator监控端点小结 在传统Spring应用中使用spring-boot-actuator模块提供监控端点 Spring Boot应用的后台运行配置 Spring Boot自定义Banner Dubbo进行服务治理 chapter9-2-1Spring Boot中使用Dubbo进行服务治理 chapter9-2-2:Spring Boot与Dubbo中管理服务依赖
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值