zoukankan      html  css  js  c++  java
  • 自定义Starters(自动配置Bean)

    1,创建Spring Startes Project的项目

    2,修改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.5.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>gjx.comMVC_demo2</groupId>
        <artifactId>spring_boot_mystarters</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>spring_boot_mystarters</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>11</java.version>
        </properties>
        <dependencies>
            
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 增加自身的自动配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                
            </dependency>
            <!-- 增加自身的自动配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    3,创建属性配置类

    作用:用来自动配置bean所需要的参数

     1 package gjx.comIOC_demo2.spring_boot_mystarters;
     2 
     3 import org.springframework.boot.context.properties.ConfigurationProperties;
     4 //读取对应的配置文件中的属性到配置类中
     5 @ConfigurationProperties(prefix = "my")
     6 public class MyProperties {
     7     private static String msg="天师默认值";
     8     public String getMsg() {
     9         return msg;
    10     }
    11     public void setMsg(String msg) {
    12         this.msg=msg;
    13     }
    14 }
    MyProperties

    4,创建将要自动配置(注入的类)

     1 package gjx.comIOC_demo2.spring_boot_mystarters;
     2 
     3 public class MyService {
     4     private String msg;
     5     public String sayMsg() {
     6         return "my"+msg;
     7     }
     8     public String getMsg() {
     9         return msg;
    10     }
    11     public void setMsg(String msg) {
    12         this.msg=msg;
    13     }
    14 }
    MyService

    5,创建自动配置的类

     1 @Configuration
     2 //开启属性配置类提供的参数来配置MyService类
     3 @EnableConfigurationProperties(MyProperties.class)
     4 //类加载器(类路径中)是否存在MyService.class指定的类
     5 @ConditionalOnClass(MyService.class)
     6 //应用环境中属性是否存在指定的值
     7 @ConditionalOnProperty(prefix = "my",value = "enabled",matchIfMissing = true)
     8 
     9 public class MyAutoConfiguration {
    10     @Autowired
    11     private MyProperties myProperties;
    12     @Bean
    13     //当容器中不存在MyService的Bean时自动配置这个Bean
    14     @ConditionalOnMissingBean(MyService.class)
    15     public MyService myService() {
    16         MyService myService=new MyService();
    17         //用属性配置类中的属性进行配置
    18         myService.setMsg(myProperties.getMsg());
    19         return myService;
    20     }
    21 }

    6,注册配置

    作用:告诉Spring Boot这个是可以自动配置的

    在resource/META-INF/spring.factories文件

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    gjx.comIOC_demo2.spring_boot_mystarters.MyAutoConfiguration

    注意: 的作用是换行后依然可以读取,有多个自动配置用  (逗号)分离

  • 相关阅读:
    C# 打印文件
    oc语言学习之基础知识点介绍(五):OC进阶
    oc语言学习之基础知识点介绍(四):方法的重写、多态以及self、super的介绍
    oc语言学习之基础知识点介绍(三):类方法、封装以及继承的介绍
    oc语言学习之基础知识点介绍(二):类和对象的进一步介绍
    oc语言学习之基础知识点介绍(一):OC介绍
    c语言学习之基础知识点介绍(二十):预处理指令
    c语言学习之基础知识点介绍(十九):内存操作函数
    XCTF-ics-04
    Portswigger-web-security-academy:dom-base_xss
  • 原文地址:https://www.cnblogs.com/gjx1212/p/14799255.html
Copyright © 2011-2022 走看看