SpringBoot study notes (1) - B station power node

001- JavaConfig

1.1 Why use springboot

insert image description here

1.2 JavaConfig

JavaConfig: 使用java类作为xml配置文件的替代, 是配置spring容器的纯java的方式。 
在这个java类就可以创建java对象,把对象放入spring容器中(注入到容器)

使用两个注解:
1)@Configuration: 放在一个类的上面,表示这个类是作为配置文件使用的。
2)@Bean:声明对象,把对象注入到容器中。

创建一个空项目 再创建一个maven java 模块进行演示

insert image description here


使用原本的方法 xml配置文件

insert image description here
insert image description here
insert image description here


使用javaconfig

insert image description here
insert image description hereinsert image description hereinsert image description here

1.3 @ImporResource

作用:导入其他的xml配置文件,等于在xml中的
<import resources="其他配置文件"/>

insert image description hereinsert image description hereinsert image description here
insert image description here

1.4 @PropertyResource

@PropertyResource: 
	读取properties属性配置文件。
	使用属性配置文件可以实现外部化配置,在程序代码之外提供数据。

步骤:
	在resources目录下,创建properties文件, 使用k=v的格式提供数据
	在@PropertyResource 指定properties文件的位置
	使用@Value(value=“${key}”)

insert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description hereinsert image description here

002- Getting started with springboot

2.1 Introduction

SpringBoot是Spring中的一个成员,可以简化Spring,SpringMVC的使用。
他的核心还是IOC容器。

Features:

  • Create stand-alone Spring applications

    Create a spring application

  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    Embedded tomcat, jetty, Undertow

  • Provide opinionated ‘starter’ dependencies to simplify your build configuration

    The starter dependency is provided to simplify the configuration of the application.


    For example, to use the MyBatis framework, you need to configure the MyBatis object SqlSessionFactory and the Dao proxy object in the Spring project .

    In the SpringBoot project, add a mybatis-spring-boot-starter dependency in pom.xml, and
    the configuration of the above object has been configured and can be used directly.

  • Automatically configure Spring and 3rd party libraries whenever possible

    Configure spring and third-party libraries as much as possible.
    It is called automatic configuration (that is, all objects in spring and third-party libraries are created and placed in the container, and developers can use them directly)

  • Provide production-ready features such as metrics, health checks, and externalized configuration

    Provides health checks, statistics, externalized configuration

  • Absolutely no code generation and no requirement for XML configuration

    No need to generate code, no xml configuration.

2.2 Create a springboot project

使用Spring提供的初始化器,就是向导,创建SpringBoot应用

默认使用的地址:Default:https://start.spring.io
国内地址:Custom:https://start.springboot.io

insert image description hereinsert image description here
insert image description hereinsert image description hereinsert image description here
insert image description here

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<!--SpringBoot的父项目-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/>
	</parent>

	<!--当前项目的gav-->
	<groupId>com.bjpowernode</groupId>
	<artifactId>002-springboot-first</artifactId>
	<version>1.0.0</version>


	<properties>
		<java.version>1.8</java.version>
	</properties>

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

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

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

</project>

insert image description here


insert image description here


2.3 Getting started project

使用springboot实现简单的控制层操作

和上面一样 使用maven创建一个模块 spring initializr
只选spring web 依赖

insert image description hereinsert image description here
insert image description here
insert image description here

insert image description here

2.4 @SpringBootApplication

@SpringBootApplication:
	是一个复合注解,主要由以下三个组成
		@SpringBootConfiguration
		@EnableAutoConfiguration
		@ComponentScan

1.@SpringBootConfiguration

@Configuration
public @interface SpringBootConfiguration {
    @AliasFor(
        annotation = Configuration.class
    )
    boolean proxyBeanMethods() default true;
}

说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用,
    可以使用@Bean声明对象,注入到容器,如下:

insert image description here

2.@EnableAutoConfiguration

启用自动配置,把java对象配置好,注入到spring容器中。
例如可以把mybatis的对象创建好,放入到容器中

3.@ComponentScan

@ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
默认扫描的包:有@ComponentScan注解的类所在的包和子包。

So other code must be at the same level as the Application file or smaller than

2.5 important configuration files of springboot

即application.properties
springboot的配置文件有两种格式 .properties 和.yml

配置文件名称:application

扩展名有:
	properties	(k=v)
	yml 		(k: v) (k:空格v)

使用application.properties,application.yml
.yml更轻量 主推这个

Example 1: application.properties setting port and context

#设置端口号
server.port=8082

#设置访问应用上下文路径,contextpath
server.servlet.context-path=/myboot

insert image description here
insert image description hereinsert image description here

Example 2: application.yml

server:
  port: 8083
  servlet:
    context-path: /myboot2

insert image description here
insert image description hereinsert image description here

2.6 Multi-environment configuration

There are development environment, test environment, and online environment.

Each environment has different configuration information, such as port, upper and lower files, database url, user name, password, etc.

Using multi-environment configuration files, you can easily switch between different configurations.

Usage: create multiple configuration files, name rule: application-environment name.properties(yml)

Create a configuration file for the development environment: application-dev.properties( application-dev.yml )

Create the configuration used by the tester: application-test.properties

insert image description hereinsert image description hereinsert image description hereinsert image description here

2.7 springboot custom configuration

(1)@Value

SpringBoot 的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配置,
然后采用如下注解去读取配置的属性值
	@Value("${key}") , key 来自 application.properties(yml)

insert image description hereinsert image description hereinsert image description here

(2)@ConfigurationProperties

将整个文件映射成一个对象,用于自定义配置项比较多的情况

insert image description here
insert image description hereinsert image description here
insert image description hereinsert image description here

2.8 springboot uses jsp

不推荐在spingboot使用jsp
springboot也不提供使用jsp 需要进行配置才能使用
在springboot中使用jsp逐渐会被淘汰

会使用模板来完成视图层的操作

Using jsp requires configuration:

  1. Add a dependency to handle jsp. Responsible for compiling jsp files
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  1. If you need to use the functions of servlet, jsp, jstl
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
</dependency>

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
	<artifactId>javax.servlet.jsp-api</artifactId>
	<version>2.3.1</version>
</dependency>

  1. Create a directory to store jsp, generally called webapp

    index.jsp

  2. It is necessary to specify the storage directory of the compiled jsp file in pom.xml.

    META-INF/resources

  3. Create Controller, access jsp

  4. Configure the view resolver in the application.propertis file


具体操作见视频

insert image description here
insert image description here

2.9 Use ApplicationContext container in springboot

如果你想通过代码,从容器中获取对象。
SpringApplication.run(Application.class, args)的返回值就是ApplicationContext容器。

insert image description hereinsert image description hereinsert image description hereinsert image description here

2.10 ComnandLineRunner interface ApplcationRunner interface

开发中可能会有这样的情景。需要在容器启动后执行一些内容。
比如读取配置文件,数据库连接之类的。
SpringBoot 给我们提供了两个接口来帮助我们实现这种需求。
这两个接口分别为 CommandLineRunner 和 ApplicationRunner。
他们的执行时机为容器启动完成的时候。

这两个接口都有一个 run 方法,我们只需要实现这个方法即可。
这两个接口的不同之处在于: 
	ApplicationRunner 中 run 方法的参数为 ApplicationArguments
	而CommandLineRunner接口中 run 方法的参数为 String 数组
@FunctionalInterface
public interface CommandLineRunner {
    
    
    void run(String... args) throws Exception;
}

@FunctionalInterface
public interface ApplicationRunner {
    
    
    void run(ApplicationArguments args) throws Exception;
}

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_52041525/article/details/125867299
Recommended