Java框架之SpringBoot 09-Web構建-yml-模塊-註解

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 相關模塊

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章