springboot项目实战(2)之自动装配和热部署

< 上一篇 :springboot项目实战(1)环境准备和服务运行(本地+服务器)
返回目录:springboot系列学习计划


  • 在上一篇博客第一个疑问的解答:
  • 阅读源码或者官方文档中遇到的生词,可点击查看此文档,粉丝免费

    1.官方文档和源码同样重要,一个给你提供广度,一个提供深度
    2.博客中最后采用一段一段式翻译,大家如果英语欠佳,可以自己先翻译一遍,在看我翻译的(水平有限),篇幅较长。能力强的可以略过。
    3.本来打算单独做一版官方文档全部翻译,一来是网上这种资源很多,二来有点本末倒置,太花时间,现在是遇到文档中的知识点,拿过来这一小节进行翻译(原文档地址会在具体位置给出)

一、 主启动的位置和原理
  1. 上篇实践中,将主启动类放在了B包下,访问时候报错,原因是没有扫描到controller包,无法注入相应的bean。
    在这里插入图片描述

    在上图中,放在A包目录下,是可以访问到的,或者放在更上一级的目录,B处是无法扫描到A处的bean的,C这个位置为classpath,一般把启动类放在这个包下。

  2. springboot包扫描的原理
    官方文档描述:https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-structuring-your-code
Structuring Your Code
Spring Boot does not require any specific code layout to work. However, there are some best practices that help.

-- 构建你的代码
springboot 不需要特定的代码布局 来工作,然而,这里有些最佳实践可以提供帮助

2.1. Using the “default” Package
-- 使用 “默认”的包
When a class does not include a package declaration, it is considered to be in the “default package. The use of the
“default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot 
applications that use the @ComponentScan, @ConfigurationPropertiesScan, @EntityScan, or @SpringBootApplication
annotations, since every class from every jar is read.
We recommend that you follow Java’s recommended package naming conventions and use a reversed domain name (for 
example, com.example.project).

-- 当一个类不在一个包的声明下,那么我们认为i他在默认的包下,使用
默认的包通常是不被提倡,应该是被避免的,他会在实际的springboot项目中造成问题
当我们使用这些扫描注解(组件扫描,配置扫描,实体类扫描,SpringBoot应用程序等注解)
当所有的类都应该被读到的时候。
我们推荐你跟随Java推荐的包命名约定:使用倒过来的域名命名(如com.example,project)。

2.2. Locating the Main Application Class
-- 定位到主启动类
We generally recommend that you locate your main application class in a root package above other classes. The 
@SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search packagefor certain items. For example, if you are writing a JPA application, the package of the @SpringBootApplication annotated 
class is used to search for @Entity items. Using a root package also allows component scan to apply only on your 
project.

-- 我们通常推荐你将主启动类放置到应用的根包下,在其他的的上层。
@SprinigBootApplication 这个注解通常放在主启动类上,并且它隐式的定义了一个基本的搜索包名
(对于某些项目)。例如,如果你在写一个jpa(Java Persistence API)项目,写了@SpringBootApplication注解的类所在的
包,用于搜索实体类。用根目录包同样允许组件扫描在你的项目中

If you don’t want to use @SpringBootApplication, the @EnableAutoConfiguration and @ComponentScan annotations 
that it imports defines that behaviour so you can also use those instead.
The following listing shows a typical layout:

--如果你不想使用@SpringBootApplication,可以使用@EnableAutoConfiguration@ComponentScan注解替代,
他们同样可以定义这种行为, 下面列举了典型的布局:
	com
	 +- example
	     +- myapplication
	         +- Application.java
	         |
	         +- customer
	         |   +- Customer.java
	         |   +- CustomerController.java
	         |   +- CustomerService.java
	         |   +- CustomerRepository.java
	         |
	         +- order
	             +- Order.java
	             +- OrderController.java
	             +- OrderService.java
	             +- OrderRepository.java
The Application.java file would declare the main method, along with the basic @SpringBootApplication, as follows:
主启动类应该声明一个main方法,伴随着@SpringBootApplication注解
代码示例:
	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);
	    }
	
	}
  1. 默认的扫描包就是上述典型布局中的【com.example.myapplication】,下面从源码的角度看看这个值是怎么读取到的
    在这里插入图片描述
    1.上图中通过注解一级一级找到类Registrar.class
    在这里插入图片描述
    2.点击register方法:
    在这里插入图片描述
    类似springmvc 的配置文件和spring的xml配置文件都可以配置包扫描,如下:
    <context:component-scan base-package="com.xxx"/>
    
    只不过springboot是隐式的配置basepackage,即启动类所在的包
    拓展:上述验证的时@SpringBootApplication注解,当在某个类上@EnableAutoConfiguration也存在时,也会走上述同样的自动扫描过程
  2. 自动配置bean
    springboot 扫描当前 classpath 下所有的 jar 包,筛选出来EnableAutoConfiguration 下的所有自动配置类注入到 spring 容器中,完成自动的 bean 的配置
    在这里插入图片描述
    这里的bean的默认配置都在类中,同样我们如果要修改,可以简便的通过配置文件来自定义值,只需要知道相应的key即可,我们采用application.yml或者application.properties来配置。
二、热部署(automatic restart)和强刷浏览器(liveReload)

上一节来到了配置文件,为了更直观的看到配置文件对程序的影响,引入热部署概念,不用频繁重启,更方便

①热部署(automatic restart)

引入devtools 详情可查看官方文档 https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools
在这里插入图片描述
官方文档:

  1. Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features. To include devtools support, add the module dependency to your build, as shown in the following listings for Maven and Gradle:
    <springboot 包含了一个额外的配置工具可以是应用开发者开发更愉快,这个开发工具模块可以被嵌入所有的项目中去提供额外的开发期特性。为了添加开发工具支持,需要在maven/gradle中添加依赖:
    <dependencies>
    	    <dependency>
    	        <groupId>org.springframework.boot</groupId>
    	        <artifactId>spring-boot-devtools</artifactId>
    	        <optional>true</optional>
    	    </dependency>
    </dependencies>
    
  2. Developer tools are automatically disabled when running a fully packaged application. If your application is launched from java -jar or if it is started from a special classloader, then it is considered a “production application”. If that does not apply to you (i.e. if you run your application from a container), consider excluding devtools or set the -Dspring.devtools.restart.enabled=false system property.
    开发者工具当全量打包时会自动失效,如果你是使用java -jar命令运行的,或者是从一个特殊的类加载器加载的,那么他会被认定为是一个生产应用工具,如果这种工具不适用于你(或者在一个容器中运行),可以考虑去除devtools或者设置重启失效(-Dspring.devtools.restart.enabled=false )
  3. Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a directory is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application.
    应用程序使用的开发者工具自动重启(热部署),当文件在classPath下被改变即生效。它在使用IDE中开发中是一个非常有用的特性,因为其循环查找代码的变化并迅速给出反馈。默认情况下,任何在classpath下的实体所在的文件夹都在被监控着变化,注明的某些资源,如静态资源和视图模板,是不需要自启动的
  4. Triggering a restart
    As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart. In IntelliJ IDEA, building the project (Build +→+ Build Project) has the same effect.
    触发重启:
    由于开发者工具监听这classpath资源,想要触发重启的方式就是更新这些资源。不同的IDE会有不同的触发方式,eclipse中保存修改后的文件即可更新并且触发重启。在IJ中构建项目可以达到同样效果

    5.热部署原理
    The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.
    这个重启的技术源于springboot是由两个类装载器一起工作的。不会修改的类(如第三方的jar包)是被加载到基础类加载器中。类在开发中的会被加载到重启类加载器。当一个应用重启,这个重启类加载器被弃用,而一个新的加载器被创建。这种方式意味着应用重启通常会比“冷启动”快,前提是基础类加载器已经可用并且构建好。
    补充:如下图:
    在这里插入图片描述

实际操作
1. pom中添加依赖;
2. 勾选Build project automatically,自动编译在这里插入图片描述
3. ctrl+shift+alt+/ 开启
在这里插入图片描述
4. ctrl+f9 使其刷新
5. 修改文件保存后,console就会打出重启的日志
6. 去除不需要重启的文件夹:

	spring.devtools.restart.exclude=static/**,public/**
②living reload(浏览器强制刷新)

热部署不会自启动静态资源和视图模板,所以需要living reload

  • The spring-boot-devtools module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. LiveReload browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.
    springboot开发者工具模块包括一个嵌入式的LivingReload 服务器用于触发浏览器刷新,当资源文件改变的时候。livingReload浏览器扩展对于chrome,firefox和safari是免费的,详见网址:livereload.com
    注:启动的时候会发现livingReload的服务端口
    在这里插入图片描述

  • If you do not want to start the LiveReload server when your application runs, you can set the spring.devtools.liveReload.enabled property to false.
    如果你在程序运行的时候不想开启liveReload服务,你可以在配置文件中设置【spring.devtools.livereload.enabled=false】
    在这里插入图片描述

  • You can only run one LiveReload server at a time. Before starting your application, ensure that no other LiveReload servers are running. If you start multiple applications from your IDE, only the first has LiveReload support.
    你只能运行一个liveReload服务器在同一时间,当你启动应用程序的时候,确保没有别的liveReload服务器在运行中,如果你启动了多个应用,只有第一个liveReload服务是有效的。


下一篇 > :springboot项目实战(1)环境准备和服务运行(本地+服务器)

返回目录:springboot系列学习计划

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 好的,下面是一个简单的Spring Boot项目实战。 1. 创建项目 首先,使用Spring Initializr创建一个新的Maven项目。在Spring Initializr页面上选择以下选项: - Project: Maven Project - Language: Java - Spring Boot: 2.5.3 - Packaging: Jar - Java: 11 点击Generate,下载生成的项目压缩包并解压。 2. 添加依赖 打开项目的pom.xml文件,在dependencies标签中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- H2 Database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- Spring Boot Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` 这些依赖将会在我们的项目中使用Spring Boot Web、Spring Boot Data JPA、H2数据库和Spring Boot Test。 3. 配置数据源 在src/main/resources目录下创建application.properties文件,并添加以下配置: ``` spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect ``` 这些配置将会使用H2内存数据库作为我们的数据源。 4. 创建实体类 在src/main/java目录下创建一个包,命名为com.example.demo.entity。在该包中创建一个名为User的实体类,代码如下: ```java @Entity @Table(name="users") public class User { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; @Column(name="first_name") private String firstName; @Column(name="last_name") private String lastName; @Column(name="email") private String email; // getters and setters } ``` 这个实体类将会对应于一个名为“users”的表,具有id、firstName、lastName和email四个字段。 5. 创建数据访问层 在src/main/java目录下创建一个包,命名为com.example.demo.repository。在该包中创建一个名为UserRepository的数据访问接口,代码如下: ```java public interface UserRepository extends JpaRepository<User, Long> { } ``` 这个接口将会自动实现使用Spring Data JPA的常见CRUD操作。 6. 创建控制器 在src/main/java目录下创建一个包,命名为com.example.demo.controller。在该包中创建一个名为UserController的控制器,代码如下: ```java @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/") public List<User> getUsers() { return userRepository.findAll(); } @PostMapping("/") public User createUser(@RequestBody User user) { return userRepository.save(user); } } ``` 这个控制器将会处理对“/api/users”路径的GET和POST请求。GET请求将会返回所有用户,而POST请求将会创建一个新的用户。 7. 运行项目 现在,我们可以运行我们的Spring Boot项目了。在命令行中进入项目根目录,并输入以下命令: ``` mvn spring-boot:run ``` 这个命令将会启动一个嵌入式Tomcat服务器,并将我们的应用程序部署到其中。 8. 测试应用 现在,我们可以使用任何HTTP客户端来测试我们的应用程序。例如,我们可以使用curl命令来测试POST请求: ``` curl -X POST -H "Content-Type: application/json" -d '{"firstName": "John", "lastName": "Doe", "email": "john.doe@example.com"}' http://localhost:8080/api/users/ ``` 这个命令将会创建一个新的用户,并将其保存到H2数据库中。 我们可以使用curl命令来测试GET请求: ``` curl http://localhost:8080/api/users/ ``` 这个命令将会返回所有用户的列表。 以上就是一个简单的Spring Boot项目实战。 ### 回答2: Spring Boot是一个开源的Java框架,它简化了Spring应用程序的开发过程。通过使用Spring Boot,开发人员可以更快速地创建可独立运行的、基于Spring的应用程序。以下是关于Spring Boot项目实战的一些介绍。 1.快速搭建项目:Spring Boot提供了一种快速搭建项目的方式,只需几个简单的步骤即可创建一个可运行的Spring应用程序。它通过提供默认的配置和自动装配来降低项目配置和依赖管理的复杂性。 2.自动装配:Spring Boot利用了自动配置的原则,根据应用程序的依赖关系自动配置工程中所需的bean。开发人员只需添加所需的依赖项,Spring Boot就会自动生成相应的配置。 3.内嵌Web服务器:Spring Boot集成了多个常用的Web服务器,如Tomcat、Jetty等,开发者无需手动配置这些服务器,只需添加相应的依赖项,即可实现Web应用的运行。 4.简化的配置:Spring Boot使用约定优于配置的原则,提供了默认的配置,避免了繁琐的配置文件。这使得开发人员可以更专注于应用程序的开发,而不必花费大量时间在配置上。 5.监控和管理:Spring Boot提供了丰富的监控和管理功能,可用于跟踪、调试和管理应用程序。开发人员可以使用Spring Boot Actuator来监视应用程序的健康状况、性能指标等。 6.丰富的生态系统:Spring Boot不仅提供了自身的功能,还可以与其他Spring项目(如Spring Cloud、Spring Data等)无缝集成,形成一个完整的解决方案。 总之,Spring Boot项目实战可以极大简化开发过程,提高开发效率。它的快速搭建、自动装配、内嵌Web服务器、简化的配置、监控和管理功能以及丰富的生态系统使得开发人员能够更专注于业务逻辑的开发,同时保持了良好的扩展性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值