第一节:Spring Boot 基础使用

tech2023-01-23  49

此博客用于个人学习,来源于网上,对知识点进行一个整理。

1. 了解 SpringBoot:


1.1 概念:

Spring Boot 是 Spring 项目中的一个子工程,与我们所熟知的 Spring-framework 同属于spring的产品。

Spring Boot 称为搭建程序的脚手架。其最主要作用就是帮我们快速的构建庞大的spring项目,并且尽可能的减少一切 xml 配置,做到开箱即用,迅速上手,让我们关注于业务而非配置。

1.2 使用背景:

java 一直被人诟病的一点就是臃肿、麻烦。当我们还在辛苦的搭建项目时,可能其他语言程序员已经把功能写好了,究其原因主要是两点:

复杂的配置

项目各种配置其实是开发时的损耗, 因为在思考 Spring 特性配置和解决业务问题之间需要进行思维切换,所以写配置挤占了写应用程序逻辑的时间。

混乱的依赖管理

项目的依赖管理也是件吃力不讨好的事情。决定项目里要用哪些库就已经够让人头痛的了,你还要知道这些库的哪个版本和其他库不会有冲突,这也是件棘手的问题。并且,依赖管理也是一种损耗,添加依赖不是写应用程序代码。一旦选错了依赖的版本,随之而来的不兼容问题毫无疑问会是生产力杀手。

1.3 特点:

主要特征是:

创建独立的 spring 应用程序。直接内嵌 tomcat、jetty和undertow(不需要打包成 war 包部署)。提供了固定化的 “starter” 配置,以简化构建配置。尽可能的自动配置 spring 和第三方库。提供产品级的功能,如:安全指标、运行状况监测和外部化配置等。绝对不会生成代码,并且不需要 XML 配置。

总之,Spring Boot为所有 Spring 的开发者提供一个开箱即用的、非常快速的、广泛接受的入门体验。

2. 一些主要注解与概念:


2.1 启动器:

为了让 SpringBoot 帮我们完成各种自动配置,我们必须引入 SpringBoot 提供的自动配置依赖,我们称为启动器。spring-boot-starter-parent 工程将依赖关系声明为一个或者多个启动器,我们可以根据项目需求引入相应的启动器,因为我们是 web 项目,这里我们引入 web 启动器:

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

需要注意的是,我们并没有在这里指定版本信息。因为 SpringBoot 的父工程已经对版本进行了管理了。项目中多出了大量的依赖都是 SpringBoot 根据 spring-boot-starter-web 这个依赖自动引入的,而且所有的版本都已经管理好,不会出现冲突。

2.2 @EnableAutoConfiguration:

开启 spring 应用程序的自动配置,SpringBoot 基于你所添加的依赖和你自己定义的 bean,试图去猜测并配置你想要的配置。比如我们引入了 spring-boot-starter-web,而这个启动器中帮我们添加了 tomcat 、 SpringMVC 的依赖。此时自动配置就知道你是要开发一个 web 应用,所以就帮你完成了 web 及 SpringMVC 的默认配置了。

一般该注解加在 controller 类上。SpringBoot 内部对大量的第三方库或 Spring 内部库进行了默认配置,这些配置是否生效,取决于我们是否引入了对应库所需的依赖,如果有那么默认配置就会生效。

2.3 引导类:

如果工程中只有一个Controller,可以在该类中配置注解和 main 方法,但如果有多个 Controller ,就需要定义一个全局的引导类。

通常请求下,我们在一个 springboot 工程中都会在基包下创建一个引导类,一些 springboot 的全局注解(@EnableAutoConfiguration 注解)以及 springboot 程序的入口 main 方法都放在该类中。

@EnableAutoConfiguration public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }

2.4 @ComponentScan:

但此时发现所有的 Controller 都不能访问了,原因是没有被扫描到,这个时候需要用到 @ComponentScan 注解了。

spring 框架除了提供配置方式的注解扫描 <context:component-scan />,还提供了注解方式的注解扫描 @ComponentScan。在 TestApplication.class 中,使用 @ComponentScan 注解:

@EnableAutoConfiguration @ComponentScan public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }

通过跟踪源码可以得知内部逻辑:1. 配置组件扫描的指令提供了类似与 <context:component-scan/> 标签的作用。2. 通过 basePackageClasses 或者 basePackages 属性来指定要扫描的包。如果没有指定这些属性,那么将从声明这个注解的类所在的包开始,扫描包及子包。

@ComponentScan 注解声明的类就是 main 函数所在的启动类,因此扫描的包是该类所在包及其子包。一般启动类会放在一个比较浅的包目录中。

2.5 @SpringBootApplication:

现在的引导类中使用了 @EnableAutoConfiguration 和 @ComponentScan 注解,有点麻烦。springboot 提供了一种简便的注解:@SpringBootApplication 注解

使用@SpringBootApplication改造TestApplication:

@SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }

通过跟踪源码可以得出:@SpringBootApplication 其实是一个组合注解,这里重点的注解有3个:

@SpringBootConfiguration:声明当前类是SpringBoot应用的配置类,项目中只能有一个。@EnableAutoConfiguration:开启自动配置。@ComponentScan:开启注解扫描。

2.6 @SpringBootConfiguration:

通过源码我们可以看出,在这个注解上面,又有一个 @Configuration 注解。通过上面的注释阅读我们知道:这个注解的作用就是声明当前类是一个配置类,然后 Spring 会自动扫描到添加了 @Configuration 的类,并且读取其中的配置信息。而 @SpringBootConfiguration 是来声明当前类是SpringBoot应用的配置类,项目中只能有一个。所以一般我们无需自己添加。

3. 默认配置原理:

springboot 的默认配置方式和 ssm 的常见配置方式不太一样,没有任何的 xml。比如我们要配置一个数据库连接池,会这样定义:

<!-- 配置连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>

3.1 尝试 java 配置:

java配置主要靠java类和一些注解来达到和xml配置一样的效果,比较常用的注解有:

`@Configuration:声明一个类作为配置类,代替 xml 文件。@Bean:声明在方法上,将方法的返回值加入Bean容器,代替 <bean> 标签。@Value:属性注入。@PropertySource:指定外部属性文件。

使用纯注解的方式的进行数据库连接池的配置,如下:

3.1.1 引入依赖:

首先在 pom.xml 中,引入 Druid 连接池依赖:

<dependency> <groupId>com.github.drtrang</groupId> <artifactId>druid-spring-boot2-starter</artifactId> <version>1.1.10</version> </dependency>

3.1.2 添加 jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/leyou jdbc.username=root jdbc.password=123

3.1.3 配置数据源:

创建 JdbcConfiguration 类:

@Configuration @PropertySource("classpath:jdbc.properties") public class JdbcConfiguration { @Value("${jdbc.url}") String url; @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } } @Configuration:声明 JdbcConfiguration 是一个配置类。@PropertySource:指定属性文件的路径是: classpath:jdbc.properties。通过 @Value 为属性注入值。通过 @Bean 将 dataSource() 方法声明为一个注册 Bean 的方法,Spring 会自动调用该方法,将方法的返回值加入 Spring 容器中,相当于以前的 bean 标签。

然后就可以在任意位置通过 @Autowired 注入DataSource了。

3.1.4 测试:

@RestController public class HelloController { @Autowired private DataSource dataSource; @GetMapping("show") public String test(){ return "hello Spring Boot!"; } }

3.2 SpringBoot 的属性注入:

属性注入使用的是 @Value 注解。这种方式虽然可行,但是不够强大,因为它只能注入基本类型值。 在 SpringBoot 中,提供了一种新的属性注入方式,支持各种 java 基本数据类型及复杂类型的注入。

1)新建 JdbcProperties,用来进行属性注入:

@ConfigurationProperties(prefix = "jdbc") public class JdbcProperties { private String url; private String driverClassName; private String username; private String password; // getters 和 setters }

在类上通过 @ConfigurationProperties 注解声明当前类为属性读取类。

prefix=“jdbc” 读取属性文件中,前缀为 jdbc 的值。

在类上定义各个属性,名称必须与属性文件中 jdbc. 后面部分一致,并且必须具有getter和setter方法。

需要注意的是,这里我们并没有指定属性文件的地址,SpringBoot 默认会读取文件名为 application.properties 的资源文件,所以我们把 jdbc.properties 名称改为 application.properties。

2)在 JdbcConfiguration 中使用这个属性:

通过 @EnableConfigurationProperties(JdbcProperties.class) 来声明要使用 JdbcProperties 这个类的对象。

然后你可以通过以下方式在 JdbcConfiguration 类中注入 JdbcProperties:

@Autowired 注入:

@Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration { @Autowired private JdbcProperties jdbcProperties; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(jdbcProperties.getUrl()); dataSource.setDriverClassName(jdbcProperties.getDriverClassName()); dataSource.setUsername(jdbcProperties.getUsername()); dataSource.setPassword(jdbcProperties.getPassword()); return dataSource; } }

构造函数注入:

@Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration { private JdbcProperties jdbcProperties; public JdbcConfiguration(JdbcProperties jdbcProperties){ this.jdbcProperties = jdbcProperties; } @Bean public DataSource dataSource() { // 略 } }

@Bean 方法的参数注入:

@Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration { @Bean public DataSource dataSource(JdbcProperties jdbcProperties) { // ... } }

3.3 更优雅的注入:

如果一段属性只有一个 Bean 需要使用,我们无需将其注入到一个类(JdbcProperties)中,而是直接在需要的地方声明即可:

@Configuration public class JdbcConfiguration { @Bean // 声明要注入的属性前缀,SpringBoot会自动把相关属性通过set方法注入到DataSource中 @ConfigurationProperties(prefix = "jdbc") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); return dataSource; } }

我们直接把 @ConfigurationProperties(prefix = “jdbc”) 声明在需要使用的 @Bean 的方法上,然后 SpringBoot 就会自动调用这个 Bean(此处是DataSource)的 set 方法,然后完成注入。使用的前提是:该类必须有对应属性的set方法!

3.4 SpringBoot 中的默认配置:

@EnableAutoConfiguration 会开启 SpringBoot 的自动配置,并且根据你引入的依赖来生效对应的默认配置。那么问题来了:

这些默认配置是怎么配置的,在哪里配置的呢?为何依赖引入就会触发配置呢?这些默认配置的属性来自哪里呢?

其实在我们的工程中,已经引入了一个依赖:spring-boot-autoconfigure,其中定义了大量自动配置类,几乎涵盖了现在主流的开源框架,例如:

redisjmsamqpjdbcjacksonmongodbjpasolrelasticsearch

… 等等

查看 mvc 的自动配置类: 我们看到这个类上的4个注解:

@Configuration:声明这个类是一个配置类。

@ConditionalOnWebApplication(type = Type.SERVLET):ConditionalOn,就是在某个条件下,此处就是满足项目的类是是Type.SERVLET类型,也就是一个普通web工程。

@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }):这里的条件为 OnClass,也就是满足以下类存在:Servlet、DispatcherServlet、WebMvcConfigurer,其中 Servlet 只要引入了 tomcat 依赖自然会有,后两个需要引入 SpringMVC 才会有。这里就是判断你是否引入了相关依赖,引入依赖后该条件成立,当前类的配置才会生效。

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class):这个条件与上面不同,OnMissingBean ,是说环境中没有指定的 Bean 这个才生效。其实这就是自定义配置的入口,也就是说,如果我们自己配置了一个 WebMVCConfigurationSupport 的类,那么这个默认配置就会失效。

3.5 总结:

SpringBoot为我们提供了默认配置,而默认配置生效的条件一般有两个:

你引入了相关依赖你自己没有配置

1)启动器

之所以,我们如果不想配置,只需要引入依赖即可,而依赖版本我们也不用操心,因为只要引入了 SpringBoot 提供的 stater(启动器),就会自动管理依赖及版本了。

因此,使用 SpringBoot 的第一件事情,就是找启动器,SpringBoot提供了大量的默认启动器。

2)全局配置

另外,SpringBoot 的默认配置,都会读取默认属性,而这些属性可以通过自定义 application.properties 文件来进行覆盖。这样虽然使用的还是默认配置,但是配置中的值改成了我们自定义的。

因此,使用 SpringBoot 的第二件事情,就是通过 application.properties 来覆盖默认属性值,形成自定义配置。

4. 创建 SpringBoot 项目:


4.1 创建工程:

使用 maven 进行创建,不需要选择任何模板,简单的一个 maven 工程。

4.2 编写基本代码:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?> <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>cn.itcast.user</groupId> <artifactId>itcast-user</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

编写引导类:

@SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class); } }

编写UserController:

@RestController @RequestMapping("user") public class UserController { @GetMapping("hello") public String test(){ return "hello ssm"; } }

4.3 整合 SpringMVC:

4.3.1 修改端口:

添加全局配置文件:application.properties:

# 映射端口 server.port=80

4.3.2 访问静态资源:

没有 webapp,我们的静态资源该放哪里呢?

由源码可以看出,有一个叫做 ResourceProperties 的类,里面就定义了静态资源的默认查找路径: 默认的静态资源路径为:

classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/

只要静态资源放在这些目录中任何一个,SpringMVC 都会帮我们处理。

4.3.3 添加拦截器:

拦截器不是一个普通属性,而是一个类,所以就要用到 java 配置方式了——通过实现 WebMvcConfigurer 并添加 @Configuration 注解来实现自定义部分 SpringMvc 配置。

首先我们定义一个拦截器:

@Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle method is running!"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle method is running!"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion method is running!"); } }

然后定义配置类,注册拦截器:

@Configuration public class MvcConfiguration implements WebMvcConfigurer { @Autowired private HandlerInterceptor myInterceptor; /** * 重写接口中的addInterceptors方法,添加自定义拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor).addPathPatterns("/**"); } }

4.4 整合连接池:

jdbc 连接池是 spring 配置中的重要一环,在 SpringBoot 中不需要处理,我们只要找到 SpringBoot 提供的启动器即可。

在 pom.xml 中引入 jdbc 的启动器:

<!--jdbc的启动器,默认使用HikariCP连接池--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>

我们只需要指定连接池参数即可:

# 连接四大参数 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root # 可省略,SpringBoot自动推断 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.hikari.idle-timeout=60000 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.minimum-idle=10

4.5 整合 mybatis:

pom.xml:

<!--mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>

配置:

# mybatis 别名扫描 mybatis.type-aliases-package=cn.itcast.pojo # mapper.xml文件位置,如果没有映射文件,请注释掉 mybatis.mapper-locations=classpath:mappers/*.xml

这里没有配置 mapper 接口扫描包,因此需要给每一个 Mapper 接口添加 @Mapper 注解,才能被识别。

@Mapper public interface UserMapper { }

通用 mapper 也需要在 pom.xml 引入依赖:

<!-- 通用mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency>

不需要做任何配置就可以使用了:

@Mapper public interface UserMapper extends Mapper<User>{ }

4.6 整合事务:

SpringBoot 中通过 @Transactional 注解来控制。

@Service public class UserService { @Autowired private UserMapper userMapper; public User queryById(Long id){ return this.userMapper.selectByPrimaryKey(id); } @Transactional public void deleteById(Long id){ this.userMapper.deleteByPrimaryKey(id); } }
最新回复(0)