Spring Boot知识点总结

本文介绍了SpringBoot框架的基本概念,包括其设计目的、核心注解的使用方法,以及如何搭建SpringBoot项目。详细讲解了@configuration、@bean等注解的作用,配置文件的读取方式,自定义拦截器和消息转换器的实现,以及SpringBoot与MyBatis、Redis的整合方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot

一、Spring Boot简介
Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。

设计目的:用来简化新 Spring 应用的初始搭建以及开发过程。

发现pom.xml中多了

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>

有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它之后,常用的包依赖就可以省去 version 标签。(必须,该parent包含了大量默认的配置,大大简化了我们的开发。)

关于具体 Spring Boot 提供了哪些 jar 包的依赖,我们可以查看本地 Maven 仓库下:\repository\org\springframework\boot\spring-boot-dependencies\2.1.0.RELEASE\spring-boot-dependencies-2.1.0.RELEASE.pom 文件来查看,挺长的…

从jdk1.5支持注解以来开始,Spring boot 渐渐开始推荐使用Java配置方式来取代xml配置,
到Spring4.x,java配置方式已经可以完全替代xml配置

二、初识注解
核心注解@Configuration和 @Bean
Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的:
1、@Configuration 作用于类上,使用该注解的类相当于一个xml配置文件;
2、@Bean 作用于方法上,使用该注解的方法相当于xml配置中的;

@Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件

@ComponentScan(basePackages = "com.xxx.xxx") //配置扫描包
public class SpringConfig {
    
    @Bean // 通过该注解来表明是一个Bean对象,相当于xml中的<bean>
    public UserDAO getUserDAO(){
        return new UserDAO(); // 直接new对象做演示,对象被注入到ioc容器中
    }
}

通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法:

@PropertySource(value = { "classpath:database.properties" })
多个注解用,号隔开例如:
@PropertySource(value={"classpath:database.properties", "classpath:database.properties"})

如果没有找到配置文件会怎样?
可通过这个参数忽略没有找到的配置文件

@PropertySource(value = { "classpath:database.properties" },ignoreResourceNotFound = true)

配置数据源:

@Value(value = "jdbc.mysql.url")
	private String jdbcUrl;
	@Value("${jdbc.mysql.driver}")
	private String jdbcDriver;
	@Value(value = "jdbc.mysql.username")
	private String jdbcUserName;
	@Value("${jdbc.mysql.password}")
	private String jdbcPassword;

	@Bean // 配置数据源(bean的名字会默认是方法名,可通过name属性设置)
	public DataSource dataSource() {
		BoneCPDataSource dataSource = new BoneCPDataSource();
		dataSource.setJdbcUrl(jdbcUrl);
		dataSource.setDriverClass(jdbcDriver);
		dataSource.setUsername(jdbcUserName);
		dataSource.setPassword(jdbcUserName);
		return dataSource;
	}

代码说明:
1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2、@Configuration:这是一个配置Spring的配置类;
3、@Controller:标明这是一个SpringMVC的Controller控制器;
4、main方法:在main方法中启动一个应用,即:这个应用的入口;

三、开始Spring Boot的学习
3.1 Spring Boot搭建
3.1.1、设置spring boot的parent

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
</parent>

说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。

3.1.2、导入spring boot的web支持

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.1.3、添加Spring boot的插件

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

3.1.4、编写第一个Spring Boot的应用

@Controller
@SpringBootApplication
@Configuration
public class HelloApplication {
    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello world!";
    }
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}
  1. 2、入口类和@SpringBootApplication
    Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法。

@SpringBootApplication注解是Spring Boot的核心注解,它其实是一个组合注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
@ConfigurationPropertiesScan
public @interface SpringBootApplication

对上面注解的介绍:
a、@SpringBootConfiguration注解(该注解也是个组合注解)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration(proxyBeanMethods = false)
public @interface SpringBootConfiguration

推荐使用该注解替代@Configuration注解

b、@EnableAutoConfiguration:启动注解该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项:

c、@ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。

3.3、关闭自动配置
Spring boot会根据项目中的jar包依赖,自动做出配置,Spring boot支持的自动配置在
spring-boot-autoconfigure-2.2.0.RELEASE.jar下可查看(非常多)
如果我们不需要Spring Boot自动配置,想要关闭某一项自动配置,该如何设置呢?

比如:不想自动配置redis,想手动配置。
只需要在启动类上的@SpringBootApplication注解加上属性
exclude = {RedisAutoConfiguration.class}即可

3.4、自定义banner

  1. 拷贝生成的字符到一个文本文件中,并且将该文件命名为banner.txt
  2. 将banner.txt拷贝到项目的resources目录中:

如果不想看到任何的banner,也是可以将其关闭的:

public static void main(String[] args) {
		SpringApplication app = new SpringApplication(HelloApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);
		/* SpringApplication.run(HelloApplication.class, args); */
}

3.5、全局配置文件
Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。
例如:

1、修改tomcat的端口为9900
server.port=9900

2、修改进入DispatcherServlet的规则为:.html
server.servlet-path=
.html

更多的配置:可百度查询文档

3.6、Starter pom

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.6Xml的配置文件
在这里插入图片描述

3.7日志(在全局配置文件配置)
Spring Boot对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置:

#设置日志级别
logging.level.org.springframework=DEBUG
格式:
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`

四、Spring Boot的自动配置的原理
Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器。
该文件在: pring-boot-2.2.0.RELEASE.jar下META-INF/中

在org.springframework.boot.autoconfigure.web.servlet包中有个类WebMvcAutoConfiguration配置了自动配置WebMvc不同版本可能有所不同,可能在其他包中自行研究。

说明该过滤器不存在时自动创建

@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
	return new OrderedHiddenHttpMethodFilter();
}

配置视图解析器

@Bean
@ConditionalOnMissingBean //默认以方法名为bean的名字
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	resolver.setPrefix(this.mvcProperties.getView().getPrefix());
	resolver.setSuffix(this.mvcProperties.getView().getSuffix());
	return resolver;
}

org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.class 在该类中定义了一个内部类View

源码如下:

public static class View {
		/**
		 * Spring MVC view prefix.
		 */
		private String prefix;
		/**
		 * Spring MVC view suffix.
		 */
		private String suffix;
		public String getPrefix() {
			return this.prefix;
		}
		public void setPrefix(String prefix) {
			this.prefix = prefix;
		}
		public String getSuffix() {
			return this.suffix;
		}
		public void setSuffix(String suffix) {
			this.suffix = suffix;
		}
	}
并没有给默认值,可在全局配置中配置
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.

4.1、静态资源配置
如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

但是不配置也可以直接访问,如果配置了,则根据配置的为准,未配置的目录下的静态资源不可以访问

4.2、自定义消息转化器

以前的配置方式:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="utf-8"/>
        </bean>
        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

现在只需要在被标记为配置文件的类中定义一个方法,并且用@Bean注解标识
也可以加上注解@ConditionalOnMissingBean,标识如果有设置,则不加载成bean

@Bean
public StringHttpMessageConverter stringHttpMessageConverter() {
	StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		return converter;
}

4.3、自定义Spring mvc拦截器
第一种方式:通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。(该类已被标记为过时的)

import java.nio.charset.Charset;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration // 申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter {
	// 自定义拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
			@Override
			public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
					throws Exception {
				System.out.println("自定义拦截器............");
				return true;
			}
			@Override
			public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
					ModelAndView modelAndView) throws Exception {
			}
			@Override
			public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
					Exception ex) throws Exception {
			}
		};
		registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
	}
	// 自定义消息转化器的第二种方法
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
		converters.add(converter);
	}
}

第二种方式:实现HandlerInterceptor接口

@Configuration // 申明这是一个配置
public class TestInterceptor implements HandlerInterceptor {
    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        if(true){
            System.out.println("已经进行拦截了。。。。。。。。");
            return false;
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object
            handler, ModelAndView modelAndView) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
    }
}

4.4 Spring boot + mybatis 整合
Mybatis和Spring Boot的整合有两种方式:
第一种:使用mybatis官方提供的Spring Boot整合包实现,地址:https://github.com/mybatis/spring-boot-starter
第二种:使用mybatis-spring整合的方式,也就是我们传统的方式
这里我们推荐使用第二种,因为这样我们可以很方便的控制Mybatis的各种配置。
首先,创建一个Mybatis的配置类:

import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration // 申明这是个配置类
public class MyBatisConfig {
	@Bean
	@ConditionalOnMissingBean // 当容器里没有指定的Bean的情况下创建该对象
	public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		// 设置数据源
		sqlSessionFactoryBean.setDataSource(dataSource);
		// 设置mybatis的主配置文件
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
		sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
		// 设置别名包
		sqlSessionFactoryBean.setTypeAliasesPackage("com.taotao.cart.pojo");
		return sqlSessionFactoryBean;
	}
}

然后,创建Mapper接口的扫描类MapperScannerConfig:

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@AutoConfigureAfter(MyBatisConfig.class) // 保证在MyBatisConfig实例化之后再实例化该类
public class MapperScannerConfig {
	// mapper接口的扫描器
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.taotao.cart.mapper");
		return mapperScannerConfigurer;
	}
}

4.5、设置事务管理
在Spring Boot中推荐使用@Transactional注解来申明事务。

首先需要导入依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

当引入jdbc依赖之后,Spring Boot会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager,所以我们不需要任何额外配置就可以用@Transactional注解进行事务的使用。

在Service中添加@Transactional注解:
在这里插入图片描述

4.6、设置Redis和Spring的整合
在Spring Boot中提供了RedisTempplate的操作,我们暂时不做学习,先按照我们之前的实现来完成。

代码:

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisSpringConfig {
	@Value("${redis.maxTotal}")
	private Integer redisMaxTotal;

	@Value("${redis.node1.host}")
	private String redisNode1Host;

	@Value("${redis.node1.port}")
	private Integer redisNode1Port;

	private JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(redisMaxTotal);
		return jedisPoolConfig;
	}

	@Bean
	public ShardedJedisPool shardedJedisPool() {
		List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();
		jedisShardInfos.add(new JedisShardInfo(redisNode1Host, redisNode1Port));
		return new ShardedJedisPool(jedisPoolConfig(), jedisShardInfos);
	}
}
学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值