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:

 数组(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 相关模块

最新文章

  1. 1-1 Linux系统安装
  2. JSTL标签功能集锦
  3. java web学习总结(二十一) -------------------模拟Servlet3.0使用注解的方式配置Servlet
  4. C#Winform连接Oracle数据库 , 及角色讲解
  5. 将表里的数据批量生成INSERT语句的存储过程 继续增强版
  6. [Json.net]忽略不需要的字段
  7. 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)
  8. cxf动态调用wsdl的一个冲突以及解决
  9. XMIND
  10. Android开发了解——Dalvik
  11. 把zlog封装成模块,隐藏zlog
  12. OnsenUI 前端框架(三)
  13. Jmeter代理服务器设置
  14. jQuery源码的一个坑
  15. DiskLruCache硬盘缓存技术详解
  16. Laravel 框架结构 以及目录文件解读(学习笔记)
  17. sqlserver 目录名称无效解决办法
  18. trace spring
  19. iOS:时间相关(18-10-13更)
  20. VirtualBox虚拟机上安装windows7系统

热门文章

  1. Netty之内存泄露
  2. vue中的路由传参及跨组件传参
  3. springboot+solr基本使用
  4. QT程序中显示中文字体解决办法
  5. Exception in thread &quot;main&quot; java.lang.AbstractMethodError
  6. D - Project Presentation(DFS序+倍增LCA)
  7. 【更新中】Hotspot tracer
  8. linux文件系统与链接
  9. 学习python-20191107
  10. java5的静态导入import static