git&shell
@@ -1,6 +1,6 @@
|
||||
## 1 创建项目
|
||||
|
||||
### maven设置
|
||||
### 1.1. maven设置
|
||||
* 配置maven的Aliyun镜像和jdk版本
|
||||
```xml
|
||||
<mirrors>
|
||||
@@ -29,13 +29,13 @@
|
||||
```
|
||||
|
||||
|
||||
### 创建maven工程
|
||||
### 1.2. 创建maven工程
|
||||
|
||||
```
|
||||
mvn archetype:generate
|
||||
```
|
||||
|
||||
### 引入依赖
|
||||
### 1.3. 引入依赖
|
||||
|
||||
```
|
||||
<parent>
|
||||
@@ -54,7 +54,7 @@ mvn archetype:generate
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
### 创建主程序
|
||||
### 1.4. 创建主程序
|
||||
|
||||
```java
|
||||
/**
|
||||
@@ -71,7 +71,7 @@ public class MainApplication {
|
||||
```
|
||||
|
||||
|
||||
### 编写业务逻辑
|
||||
### 1.5. 编写业务逻辑
|
||||
|
||||
```java
|
||||
@RestController
|
||||
@@ -88,12 +88,12 @@ public class HelloController {
|
||||
```
|
||||
|
||||
|
||||
### 测试
|
||||
### 1.6. 测试
|
||||
|
||||
直接运行main方法
|
||||
|
||||
|
||||
### 配置
|
||||
### 1.7. 配置
|
||||
|
||||
application.properties
|
||||
|
||||
@@ -101,8 +101,8 @@ application.properties
|
||||
server.port=8888
|
||||
```
|
||||
|
||||
### 简化部署
|
||||
|
||||
### 1.8. 简化部署
|
||||
把项目打成jar包,直接在服务器运行。
|
||||
```xml
|
||||
<build>
|
||||
<plugins>
|
||||
@@ -117,7 +117,7 @@ server.port=8888
|
||||
|
||||
## 2 工程结构
|
||||
|
||||
### 推荐工程结构
|
||||
### 2.1. 推荐工程结构
|
||||
```
|
||||
com
|
||||
+- example
|
||||
@@ -144,7 +144,7 @@ com
|
||||
> Spring Boot的应用主类会自动扫描root package以及所有子包下的所有类来进行初始化。
|
||||
|
||||
|
||||
### 非典型结构下的初始化
|
||||
### 2.2. 非典型结构下的初始化
|
||||
|
||||
1. 使用@ComponentScan注解指定具体的加载包
|
||||
|
||||
@@ -179,7 +179,7 @@ public class Bootstrap {
|
||||
|
||||
## 3 两大特性——依赖管理
|
||||
|
||||
### 依赖管理的原理
|
||||
### 3.1. 依赖管理的原理
|
||||
通过父项目进行依赖管理。通过starter进行依赖导入。
|
||||
```xml
|
||||
<parent>
|
||||
@@ -196,7 +196,10 @@ public class Bootstrap {
|
||||
1. 几乎声明了所有的版本,查看Spring-boot-dependencies中的版本。可以自定义properties标签,修改版本号。
|
||||
2. stater场景启动器。自动引入相关的所有依赖。可以自定义场景启动器,所有场景启动器最基本的以来。spring-boot-starter。引入依赖一部分可以不写版本号。引入非版本仲裁的版本号,必须要写。
|
||||
|
||||
### spring-boot-starter-web的依赖树
|
||||
修改默认的版本号,就近优先原则。
|
||||
* 查看parent中定义了版本的key,在子项目中覆盖该key,即可修改该版本。
|
||||
|
||||
### 3.2. spring-boot-starter-web的依赖树
|
||||
* (spring-boot-starter)-web
|
||||
* spring-boot相关的依赖
|
||||
* ()+logging依赖
|
||||
@@ -250,9 +253,9 @@ cmd+U 父项目
|
||||
[INFO] \- org.springframework:spring-expression:jar:5.2.9.RELEASE:compile
|
||||
```
|
||||
|
||||
## 4 两大特性——自动配置
|
||||
## 4. 4 两大特性——自动配置
|
||||
|
||||
### 自动配置的体现
|
||||
### 4.1. 自动配置的体现
|
||||
|
||||
1. 自动配好了SpringMVC
|
||||
1. 引入了SpringMVC全套组件
|
||||
@@ -264,8 +267,13 @@ cmd+U 父项目
|
||||
4. 按需加载所有的自动配置,自动配置都在spring-boot-autoconfigure包中
|
||||
|
||||
|
||||
### @ConfigurationProperties
|
||||
### 4.2. @ConfigurationProperties
|
||||
|
||||
@ConfigurationProperties是springboot提供读取配置文件的一个注解。
|
||||
|
||||
它是实现了BeanPostProcessor接口,在bean被实例化后,会调用后置处理,递归的查找属性,通过反射注入值,对大多数属性而言强制需提供其setter和getter方法。
|
||||
它是实现了BeanPostProcessor接口,在bean被实例化后,会调用后置处理,递归的查找属性,通过反射注入值,对大多数属性而言强制需提供其setter和getter方法。
|
||||
|
||||
|
||||
|
||||
## 4 启动过程
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ public class MyConfig {}
|
||||
|
||||
* @ConditionalOnBean(name="bean")当容器中存在指定名称的容器的时候,才会进行注册。
|
||||
|
||||
```xml
|
||||
```java
|
||||
@=====================测试条件装配==========================
|
||||
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
|
||||
//@ConditionalOnBean(name = "tom")
|
||||
|
||||
@@ -1,52 +1,166 @@
|
||||
|
||||
|
||||
## 1 springboot的启动过程
|
||||
|
||||
springcontext.run到底干了什么
|
||||
```plantuml
|
||||
|
||||
@startuml
|
||||
|
||||
' 定义对象,第一个对象SpringApplication是Springboot提供的第一个对象公民。
|
||||
class SpringApplication{
|
||||
run:通过静态的run方法创建第一个对象SpringApplication,然后执行对象的run方法。
|
||||
getRunListeners:通过Thread.concurrentThread.getContextClassLoader方法得到主线程,当前线程的类加载器(用于加载字节码文件)
|
||||
preapareEnviroment:准备属性配置文件。
|
||||
}
|
||||

|
||||
|
||||
|
||||
@enduml
|
||||
SpringApplication.run()到底干了什么
|
||||
|
||||
```
|
||||
### 服务构建
|
||||
|
||||
* SpringApplication.run该run方法可以启动一个类,也可以启动多个类
|
||||
调用SpringApplication的静态run方法。通过一系列配置创建SpringApplication类。
|
||||
1. 初始化资源加载器
|
||||
2. 初始化服务类型
|
||||
3. 初始化spring.factories中定义的初始化类。包括Initializer和Listener
|
||||
4. 找到启动类
|
||||
|
||||
```
|
||||
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
|
||||
return run(new Class<?>[] { primarySource }, args);
|
||||
}
|
||||

|
||||
|
||||
```java
|
||||
/**
|
||||
* Static helper that can be used to run a {@link SpringApplication} from the
|
||||
* specified sources using default settings and user supplied arguments.
|
||||
* @param primarySources the primary sources to load
|
||||
* @param args the application arguments (usually passed from a Java main method)
|
||||
* @return the running {@link ApplicationContext}
|
||||
* Create a new {@link SpringApplication} instance. The application context will load
|
||||
* beans from the specified primary sources (see {@link SpringApplication class-level}
|
||||
* documentation for details). The instance can be customized before calling
|
||||
* {@link #run(String...)}.
|
||||
* @param resourceLoader the resource loader to use
|
||||
* @param primarySources the primary bean sources
|
||||
* @see #run(Class, String[])
|
||||
* @see #setSources(Set)
|
||||
*/
|
||||
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
|
||||
return new SpringApplication(primarySources).run(args);
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
Assert.notNull(primarySources, "PrimarySources must not be null");
|
||||
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
|
||||
this.webApplicationType = WebApplicationType.deduceFromClasspath();
|
||||
this.bootstrapRegistryInitializers = new ArrayList<>(
|
||||
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
|
||||
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
|
||||
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
|
||||
this.mainApplicationClass = deduceMainApplicationClass();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### 环境准备
|
||||
调用SpringApplicationContext的run方法。
|
||||
1. 加载bootstrapContext上下文
|
||||
2. 获取并注册监听器。
|
||||
3. 加载环境变量,并发布环境变量加载完成的事件。(通过观察者模式)
|
||||
|
||||
|
||||

|
||||
|
||||
```java
|
||||
/**
|
||||
* Run the Spring application, creating and refreshing a new
|
||||
* {@link ApplicationContext}.
|
||||
* @param args the application arguments (usually passed from a Java main method)
|
||||
* @return a running {@link ApplicationContext}
|
||||
*/
|
||||
public ConfigurableApplicationContext run(String... args) {
|
||||
long startTime = System.nanoTime();
|
||||
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
|
||||
ConfigurableApplicationContext context = null;
|
||||
configureHeadlessProperty();
|
||||
SpringApplicationRunListeners listeners = getRunListeners(args);
|
||||
listeners.starting(bootstrapContext, this.mainApplicationClass);
|
||||
try {
|
||||
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
|
||||
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
|
||||
configureIgnoreBeanInfo(environment);
|
||||
Banner printedBanner = printBanner(environment);
|
||||
context = createApplicationContext();
|
||||
context.setApplicationStartup(this.applicationStartup);
|
||||
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
|
||||
refreshContext(context);
|
||||
afterRefresh(context, applicationArguments);
|
||||
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
|
||||
if (this.logStartupInfo) {
|
||||
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
|
||||
}
|
||||
listeners.started(context, timeTakenToStartup);
|
||||
callRunners(context, applicationArguments);
|
||||
}
|
||||
```
|
||||
|
||||
### 容器创建
|
||||
在run方法中创建容器上下文SpringApplicationContext
|
||||
|
||||
|
||||

|
||||
|
||||
1. 默认创建AnnotationConfigServletWebServerApplicationContext。在该类中调用两个注解处理方法。
|
||||
|
||||
```java
|
||||
public AnnotationConfigServletWebServerApplicationContext() {
|
||||
this.reader = new AnnotatedBeanDefinitionReader(this);
|
||||
this.scanner = new ClassPathBeanDefinitionScanner(this);
|
||||
}
|
||||
```
|
||||
2. 构建conttext。加载Initializer,注册启动参数,加载postProcess.
|
||||
|
||||
```java
|
||||
private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
|
||||
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
|
||||
ApplicationArguments applicationArguments, Banner printedBanner) {
|
||||
context.setEnvironment(environment);
|
||||
postProcessApplicationContext(context);
|
||||
applyInitializers(context);
|
||||
listeners.contextPrepared(context);
|
||||
bootstrapContext.close(context);
|
||||
if (this.logStartupInfo) {
|
||||
logStartupInfo(context.getParent() == null);
|
||||
logStartupProfileInfo(context);
|
||||
}
|
||||
// Add boot specific singleton beans
|
||||
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
||||
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
|
||||
if (printedBanner != null) {
|
||||
beanFactory.registerSingleton("springBootBanner", printedBanner);
|
||||
}
|
||||
if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
|
||||
((AbstractAutowireCapableBeanFactory) beanFactory).setAllowCircularReferences(this.allowCircularReferences);
|
||||
if (beanFactory instanceof DefaultListableBeanFactory) {
|
||||
((DefaultListableBeanFactory) beanFactory)
|
||||
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
|
||||
}
|
||||
}
|
||||
if (this.lazyInitialization) {
|
||||
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
|
||||
}
|
||||
context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context));
|
||||
// Load the sources
|
||||
Set<Object> sources = getAllSources();
|
||||
Assert.notEmpty(sources, "Sources must not be empty");
|
||||
load(context, sources.toArray(new Object[0]));
|
||||
}
|
||||
```
|
||||
3. 发布资源监听事件
|
||||
```java
|
||||
listeners.contextLoaded(context);
|
||||
|
||||
```
|
||||
|
||||
### 填充容器——自动装配
|
||||
|
||||

|
||||
1. refreshContext(conext)
|
||||
2. 发布启动完成事件,调用自定义实现的runner接口。
|
||||
|
||||
## 2 自动配置加载的过程
|
||||
### 加载过程
|
||||
|
||||
|
||||
* @SpringBootConfiguration,就是一个@Configuration配置类。定义这是一个配置类。
|
||||
* @ComponentScan指定包扫描
|
||||
* @EnableAutoConfiguration
|
||||
* @AutoConfigurationPackage自动配置包。将该包下的所有配置类导入进去。
|
||||
* @Import(AutoConfigurationImportSelect.class)导入一个包。在autoconfiguration.jar包下,META_INF/spring.factories文件中,给出了全类名。
|
||||
* @AutoConfigurationPackage自动配置包。将该包下的所有配置类导入进去。
|
||||
* @Import(AutoConfigurationPackages.Register.class)利用register,将指定的包下的所有组件注册到容器中。所以默认包路径是Main程序所在的包。
|
||||
* @Import(AutoConfigurationImportSelect.class)获取所有导入到容器中的配置类。利用Spring工厂加载器,从spring-boot-autoconfigure./META-INF/spring-factories中加载文件。Springboot一启动就要加载的所有配置类。
|
||||
|
||||

|
||||
|
||||
### 自动配置总结
|
||||
* Spring 加载所有的自动配置类
|
||||
|
||||
@@ -1,17 +1,8 @@
|
||||
# web开发
|
||||
|
||||
## 概述
|
||||
将springboot和springmvc结合起来的讲解
|
||||
|
||||
关于父类和子类的作用的说明。记得在写毕设的时候,自己把父类当做工具类,子类在调用很多方法的时候,为了少写代码,就直接调用父类中的方法,然后导致父类中的流程函数,会调用子类中的方法,子类中的函数又会调用父类中的方法,非常凌乱,两个类相互冗余。当时也在思考,这些工具函数写在父类中供所有的子类调用与写一个util类有什么区别?
|
||||
|
||||
现在发现,应该遵循一些默认的编码规则,父类用来构建整体的流程,而子类用来完善丰富一些子流程。相当于父类在构建主流程的时候,空出一些细节实现的部分,让子类来完善。而不是写一写类似的工具函数,让子类来调用,子类能够看到更加全面丰富的流程,那么父类就没有存在的必要了,父类的作用可能就是一个接口了,只提供一些对外的方法声明。
|
||||
|
||||
|
||||
综上所属:
|
||||
* 接口:只提供对外的方法声明
|
||||
* 父类:提供完整的流程,由父类调用未经实现的抽象方法完成整体流程的构建。
|
||||
* 子类:提供丰富的细节实现,由子类实现抽象方法的细节。
|
||||
* 工具类:提供给所有子类的通用的处理函数。
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -60,7 +60,8 @@ CREATE TABLE `User` (
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
|
||||
```
|
||||
|
||||
### 编写领域对象(并不是MVC的一部分。数据层,实现数据访问)
|
||||
### 编写领域对象
|
||||
并不是MVC的一部分。数据层,实现数据访问
|
||||
|
||||
```
|
||||
@Data
|
||||
@@ -73,10 +74,11 @@ public class User {
|
||||
}
|
||||
```
|
||||
|
||||
### 编写数据访问对象(并非MVC的一部分。服务层,实现业务逻辑)
|
||||
### 编写数据访问对象
|
||||
并非MVC的一部分。服务层,实现业务逻辑
|
||||
|
||||
* 定义包含插入、删除、查询的抽象接口UserService
|
||||
```
|
||||
定义包含有插入、删除、查询的抽象接口UserService
|
||||
```java
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
@@ -45,3 +45,65 @@ spring.datasource.hikari.connection-timeout=60000//连接超时时间
|
||||
spring.datasource.hikari.connection-test-query=SELECT 1//用于测试连接是否可用的查询语句
|
||||
```
|
||||
|
||||
## 3 druid数据源
|
||||
|
||||
### pom.xml配置druid依赖
|
||||
|
||||
```
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.21</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### application.properties配置数据库连接信息
|
||||
|
||||
以spring.datasource.druid作为前缀
|
||||
```
|
||||
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.druid.username=root
|
||||
spring.datasource.druid.password=
|
||||
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
```
|
||||
|
||||
### 配置druid连接池
|
||||
> 具体的信息可以自己查询相关的内容。
|
||||
```
|
||||
spring.datasource.druid.initialSize=10
|
||||
spring.datasource.druid.maxActive=20
|
||||
spring.datasource.druid.maxWait=60000
|
||||
spring.datasource.druid.minIdle=1
|
||||
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
|
||||
spring.datasource.druid.minEvictableIdleTimeMillis=300000
|
||||
spring.datasource.druid.testWhileIdle=true
|
||||
spring.datasource.druid.testOnBorrow=true
|
||||
spring.datasource.druid.testOnReturn=false
|
||||
spring.datasource.druid.poolPreparedStatements=true
|
||||
spring.datasource.druid.maxOpenPreparedStatements=20
|
||||
spring.datasource.druid.validationQuery=SELECT 1
|
||||
spring.datasource.druid.validation-query-timeout=500
|
||||
spring.datasource.druid.filters=stat
|
||||
```
|
||||
|
||||
### 配置druid监控
|
||||
|
||||
* 在pom.xml中增加依赖
|
||||
|
||||
```
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
* 在application.properties中添加druid监控配置
|
||||
|
||||
```
|
||||
spring.datasource.druid.stat-view-servlet.enabled=true
|
||||
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
|
||||
spring.datasource.druid.stat-view-servlet.reset-enable=true
|
||||
spring.datasource.druid.stat-view-servlet.login-username=admin
|
||||
spring.datasource.druid.stat-view-servlet.login-password=admin
|
||||
```
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 在application.properties中配置mysql的链接配置
|
||||
### 配置数据库连接
|
||||
在application.properties中配置mysql的链接配置
|
||||
|
||||
```
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/test
|
||||
20
Spring/Springboot/11 源码解析(一)Bean生命周期.md
Normal file
@@ -0,0 +1,20 @@
|
||||
## 1 Bean生命周期
|
||||
|
||||
### 创建
|
||||
|
||||

|
||||
|
||||
### 使用
|
||||
|
||||
|
||||
### 销毁
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
|
||||
## 2 IOC容器创建
|
||||
|
||||
|
||||
## 3 AOP原理
|
||||
@@ -1,75 +0,0 @@
|
||||
```
|
||||
@RestController
|
||||
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在/users下
|
||||
public class UserController {
|
||||
|
||||
// 创建线程安全的Map,模拟users信息的存储
|
||||
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
|
||||
|
||||
/**
|
||||
* 处理"/users/"的GET请求,用来获取用户列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/")
|
||||
public List<User> getUserList() {
|
||||
// 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
|
||||
List<User> r = new ArrayList<User>(users.values());
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理"/users/"的POST请求,用来创建User
|
||||
*
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/")
|
||||
public String postUser(@RequestBody User user) {
|
||||
// @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
|
||||
users.put(user.getId(), user);
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public User getUser(@PathVariable Long id) {
|
||||
// url中的id可通过@PathVariable绑定到函数的参数中
|
||||
return users.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理"/users/{id}"的PUT请求,用来更新User信息
|
||||
*
|
||||
* @param id
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public String putUser(@PathVariable Long id, @RequestBody User user) {
|
||||
User u = users.get(id);
|
||||
u.setName(user.getName());
|
||||
u.setAge(user.getAge());
|
||||
users.put(id, u);
|
||||
return "success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理"/users/{id}"的DELETE请求,用来删除User
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public String deleteUser(@PathVariable Long id) {
|
||||
users.remove(id);
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@@ -1,39 +0,0 @@
|
||||
## swagger2使用
|
||||
|
||||
### pom.xml添加依赖
|
||||
|
||||
```
|
||||
<dependency>
|
||||
<groupId>com.spring4all</groupId>
|
||||
<artifactId>swagger-spring-boot-starter</artifactId>
|
||||
<version>1.9.0.RELEASE</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 应用主类中添加@EnableSwagger2Doc注解
|
||||
|
||||
```
|
||||
@EnableSwagger2Doc
|
||||
@SpringBootApplication
|
||||
public class Chapter22Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Chapter22Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### application.properties中配置文档相关内容,比如
|
||||
|
||||
swagger.title=spring-boot-starter-swagger
|
||||
swagger.description=Starter for swagger 2.x
|
||||
swagger.version=1.4.0.RELEASE
|
||||
swagger.license=Apache License, Version 2.0
|
||||
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
|
||||
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
|
||||
swagger.contact.name=didi
|
||||
swagger.contact.url=http://blog.didispace.com
|
||||
swagger.contact.email=dyc87112@qq.com
|
||||
swagger.base-package=com.didispace
|
||||
swagger.base-path=/**
|
||||
@@ -1,61 +0,0 @@
|
||||
## 1 基本配置
|
||||
|
||||
### pom.xml配置druid依赖
|
||||
|
||||
```
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.21</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### application.properties配置数据库连接信息
|
||||
|
||||
以spring.datasource.druid作为前缀
|
||||
```
|
||||
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test
|
||||
spring.datasource.druid.username=root
|
||||
spring.datasource.druid.password=
|
||||
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
```
|
||||
|
||||
### 配置druid连接池
|
||||
> 具体的信息可以自己查询相关的内容。
|
||||
```
|
||||
spring.datasource.druid.initialSize=10
|
||||
spring.datasource.druid.maxActive=20
|
||||
spring.datasource.druid.maxWait=60000
|
||||
spring.datasource.druid.minIdle=1
|
||||
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
|
||||
spring.datasource.druid.minEvictableIdleTimeMillis=300000
|
||||
spring.datasource.druid.testWhileIdle=true
|
||||
spring.datasource.druid.testOnBorrow=true
|
||||
spring.datasource.druid.testOnReturn=false
|
||||
spring.datasource.druid.poolPreparedStatements=true
|
||||
spring.datasource.druid.maxOpenPreparedStatements=20
|
||||
spring.datasource.druid.validationQuery=SELECT 1
|
||||
spring.datasource.druid.validation-query-timeout=500
|
||||
spring.datasource.druid.filters=stat
|
||||
```
|
||||
|
||||
### 配置druid监控
|
||||
|
||||
* 在pom.xml中增加依赖
|
||||
|
||||
```
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
* 在application.properties中添加druid监控配置
|
||||
|
||||
```
|
||||
spring.datasource.druid.stat-view-servlet.enabled=true
|
||||
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
|
||||
spring.datasource.druid.stat-view-servlet.reset-enable=true
|
||||
spring.datasource.druid.stat-view-servlet.login-username=admin
|
||||
spring.datasource.druid.stat-view-servlet.login-password=admin
|
||||
```
|
||||
@@ -1,12 +0,0 @@
|
||||
## 自动配置类
|
||||
|
||||
### SpringBootApplication注解的详细解释
|
||||
```
|
||||
@SpringBootApplication ==>
|
||||
@SpringBootConfiguration //本身是一个配置类@Configuration,利用容器中的东西完成业务逻辑
|
||||
@EnableAutoConfiguration
|
||||
@AutoConfigurationPackage
|
||||
@Import(AutoConfigurationPackages.Register.class)利用register,将指定的包下的所有组件注册到容器中。所以默认包路径是Main程序所在的包。
|
||||
@Import(AutoConfigurationSelector.class)获取所有导入到容器中的配置类。利用Spring工厂加载器,从spring-boot-autoconfigure./META-INF/spring-factories中加载文件。Springboot一启动就要加载的所有配置类。
|
||||
@ComponentScan //包扫描的范围
|
||||
```
|
||||
BIN
Spring/Springboot/image/2023-01-09-10-44-48.png
Normal file
|
After Width: | Height: | Size: 1.9 MiB |
BIN
Spring/Springboot/image/2023-01-09-10-47-27.png
Normal file
|
After Width: | Height: | Size: 634 KiB |
BIN
Spring/Springboot/image/2023-01-09-10-56-25.png
Normal file
|
After Width: | Height: | Size: 3.2 MiB |
BIN
Spring/Springboot/image/2023-01-09-11-25-19.png
Normal file
|
After Width: | Height: | Size: 2.9 MiB |
BIN
Spring/Springboot/image/2023-01-09-11-39-47.png
Normal file
|
After Width: | Height: | Size: 3.3 MiB |
BIN
Spring/Springboot/image/2023-01-09-11-40-17.png
Normal file
|
After Width: | Height: | Size: 2.4 MiB |
BIN
Spring/Springboot/image/2023-01-10-20-48-09.png
Normal file
|
After Width: | Height: | Size: 1.4 MiB |
BIN
Spring/Springboot/image/2023-01-10-20-49-12.png
Normal file
|
After Width: | Height: | Size: 1.7 MiB |