SpringBoot系列:2.3.2版本官方文档阅读笔记-使用SpringBoot的一些建议

SpringBoot版本:2.3.2
文档链接:SpringBoot 2.3.2官方文档

@EnableAutoConfiguration注解

@EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

Starters and Auto-configuration

Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick and choose jar dependencies outside of the starters. Spring Boot still does its best to auto-configure your application.

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.

。。。。。

3.2 使用SpringBoot的代码结构建议(Structuring Your Code)

  1. 如果一个类不放在任何包中,那么它的包是default-package,SpringBoot处理这样的类时可能存在问题。
  2. 存放main方法的类放在root package下,这样SpringBoot默认扫描的就是main方法所在类的包已经所有子包。例如Application.java方法中有main方法,那么Application类上标注的@SpringBootApplication默认会扫描com.example.myapplication包以及com.example.myapplication.customer包、com.example.myapplication.order包
com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

3.3 配置类(Configuration Classes)

SpringBoot官方建议优先使用基于java代码的配置类,而不是XML形式的配置类。

不需要把所有的配置类都放在@Configuration标注的类中,还可以通过@Import、@ComponentScan引入其他的配置类。

如果一定要使用基于XML的配置类,最好是通过@ImportResource注解引入。

3.4 自动装配(Auto-configuration)

什么是自动装配。自动装配是SpringBoot的一大特性,SpringBoot的自动装配机制会尝试基于你添加的依赖配置你的Spring应用。例如如果classPath中有HSQLDB并且你没有手动配置数据库连接相关的bean,那么SpringBoot会为你自动配置一个内存数据库。但是,如果你配置了DataSource的Bean,那么默认的内存数据库则不会被配置。

开启自动装配的方法。在使用SpringBoot的自动转配机制时,只需要把@SpringBootApplication注解或者@EnableAutoConfiguration注解中的一个加在@Configuration指定的配置类上。

如何禁止自动装配。如果想禁止某些类的自动装配,在@SpringBootApplication注解或@EnableAutoConfiguration中可以使用exclude或excludeName,也可以在application.properties文件中指定spring.autoconfigure.exclude属性。

3.5 Spring的Bean和依赖注入(Spring Beans and Dependency Injection)

可以通过SpringFramework支持的方式来定义Bean并完成Bean的依赖注入,例如通过@ComponentScan来扫描bean并通过@Autowired进行Bean的自动注入。

3.6 使用@SpringBootApplication注解(Using the @SpringBootApplication Annotation)

@SpringBootApplication注解由三个注解构成,分别是

  • @EnableAutoConfiguration,实现SpringBoot的自动装配机制
  • @ComponentScan,完成组件扫描
  • @Configuration,用例注册额外的Bean或者引入其他的配置类

3.7 运行应用(Running Your Application)

一般有两种执行方式

  • 本地测试,可以通过引入spring-boot-maven-plugin,然后通过mvn spring-boot:run执行SpringBoot应用
  • 生产环境,首先将SpringBoot应用打包成Fat Jar(可执行jar),然后通过java -jar进行执行

文档中还介绍了jvm的 Hot Swapping,有兴趣可以看下jrebel。SpringBoot本身提供了spring-boot-devtools模块支持应用快速的重新启动。

未完待续。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值