SpringBoot
Spring Boot是一站式整合所有應用架構的架構,簡化Spring應用開發,約定大于配置,去繁從簡,開箱即用,準生産環境的運作時應用監控架構
快速建構 SpringBoot 應用必須聯網建立,使用 Eclipse 或 Idea,選擇好 SpringBoot 版本及 Spring Starter 子產品即可
1) 自動生成主程式類,用于啟動項目 2) 自動生成靜态資源目錄及屬性配置檔案 3) 自動增加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>
<!-- 用來做依賴管理,幾乎将我們用到的所有的依賴的版本都聲明好了;-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath/>
</parent>
<!-- 建構的項目子產品都需繼承 org.springframework.boot 做依賴管理-->
<groupId>cn.gcaca</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 版本仲裁中心-->
<properties>
<java.version>1.8</java.version>
</properties>
<!-- spring-boot-starter-xxx:場景啟動器 SpringBoot自動引入場景所需要的所有依賴-->
<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>
<!-- 引入springboot插件;打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在static檔案夾下存放靜态資源檔案
application.properties 或 application.yml 配置檔案名是固定的,定義配置加載屬性檔案規則
YAML基本文法
l 使用縮進表示層級關系
l 縮進時不允許使用Tab鍵,隻允許使用空格。
l 縮進的空格數目不重要,隻要相同層級的元素左側對齊即可
l 大小寫敏感
字面量:普通的值(數字,字元串,布爾)
l k: v:字面值直接書寫即可;字元串預設不用加上單引号或者雙引号;
l " ":雙引号;不會轉義字元串裡面的特殊字元;特殊字元會作為本身想表示的意思 例 : name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi
l ' ':單引号;會轉義特殊字元,特殊字元最終隻是一個普通的字元串資料 例 : name: 'zhangsan \n lisi':輸出;zhangsan \n lisi
對象、Map(屬性和值)(鍵值對)
l k: v:通過縮進在下一行來寫對象的屬性和值表示其關系;注意縮進
l 對象還是k: v的方式
l 行内寫法 friends: {lastName: zhangsan,age: 18}
friends:
lastName: zhangsan
age: 20
數組(List、Set)
l 行内寫法 pets: [cat,dog,pig]
用- 值表示數組中的一個元素
pets:
‐ cat
‐ dog
自動配置原理
1)、SpringBoot 幫我們配好了所有的應用場景
2)、SpringBoot 中會有很多的 xxxxAutoConfigurarion(幫我們給容器中自動裝配好元件)
3)、xxxxAutoConfigurarion 給容器中配元件的時候,元件預設的屬性一般都是從 xxxProperties 中擷取這些屬性的值
4)、xxxProperties 是和配置檔案綁定的(屬性一 一對應)
5)、如預設值不符合我們的需要隻需改掉這些預設配置即可;
6)、如果預設的元件我們不用;還可以自定義元件。
SpringBoot 的一個最大政策:自定義元件用自己的,否則,使用預設的。
自定義配置類
@SpringBootConfiguration
public class AppConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
return resolver;
}
// 補充:擷取ApplicationContext 對象
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.getBean("myBean");
}
}
SpringBoot 建構 WEB 應用項目
建構 web,jdbc,mybatis,mysql,redis,thymeleaf 等相關元件
增加 Druid 資料源
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
application.yml 配置相關屬性
spring:
datasource:
username: root
password: 12345
url: jdbc:mysql://localhost:3306/scw?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
config-location: classpath:/mybatis/mybatis-config.xml
mapper-locations: classpath:/mybatis/mapper/*.xml
主程式配置類
//主程式掃描加載 mapper
@MapperScan("cn.gc.dao")
//內建事務管理,在Service接口中增加@Transactional
@EnableTransactionManagement
//整合Web元件,在配置類增加@WebServlet @WebFilter @WebListener
@ServletComponentScan
@SpringBootApplication
public class SpringMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SptingMybatisApplication.class, args);
}
}
Druid 配置類及監控背景管理
@SpringBootConfiguration
public class DruidConfig {
//引入yml檔案配置屬性
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setFilters("stat");// 配置監控統計攔截的filters
return dataSource;
}
// 配置Druid的監控 1、配置一個管理背景的Servlet
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "admin");
initParams.put("loginPassword", "12345");
initParams.put("allow", "");// 預設就是允許所有通路
initParams.put("deny", "");// 拒絕哪個ip通路
bean.setInitParameters(initParams);
return bean;
}
// 2、配置一個web監控的filter
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<>();
initParams.put("exclusions", "*.js,*.css,/druid/*");// 排除過濾
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
Spring Boot 常用注解彙總
SpringBoot 相關子產品
