• 自定义Starters


    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <packaging>jar</packaging>
    
        <groupId>org.tszr</groupId>
        <artifactId>spring_boot_mystarters1</artifactId>
        <version>1.0-SNAPSHOT</version>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
        </parent>
    
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <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>
            <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-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    package com.tszr.configuration;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    //在application.properties中通过my.msg=设置属性
    @ConfigurationProperties(prefix="my")
    public class MyProperties {
        private String msg = "默认值";
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    package com.tszr.service;
    
    public class MyService {
        private String msg;
        public String sayMsg() {
            return "my " + msg;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    package com.tszr.configuration;
    
    import com.tszr.service.MyService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    //开启属性配置类MyProperties提供参数
    @EnableConfigurationProperties(MyProperties.class)
    //类加载器(类路径)中是否存在对应的类
    @ConditionalOnClass(MyService.class)
    //应用环境中属性是否存在指定的值
    @ConditionalOnProperty(prefix = "my", value = "enabled", matchIfMissing = true)
    public class MyAutoConfiguration {
        @Autowired
        private MyProperties myProperties;
        @Bean
        //当容器中不存在MyService的Bean时,自动配置这个Bean
        @ConditionalOnMissingBean(MyService.class)
        public MyService myService() {
            MyService myService = new MyService();
            myService.setMsg(myProperties.getMsg());
            return myService;
        }
    }
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tszr.configuration.MyAutoConfiguration
    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>cha-4</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>cha-4</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
        </parent>
    
        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.tszr</groupId>
                <artifactId>spring_boot_mystarters1</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    package com.tszr.controller;
    
    import com.tszr.service.MyService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MyController {
        @Autowired
        MyService myService;
    
        @RequestMapping("/testStarters")
        public String index() {
            return myService.sayMsg();
        }
    }
  • 相关阅读:
    SPOJ 4487. Can you answer these queries VI splay
    Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置改动及測试步骤
    欧拉函数
    安装Windows7步骤
    在Eclipse中执行、配置Hadoop
    java设计模式演示样例
    VC中获取窗体句柄的各种方法
    HTML5 Canvas中实现绘制一个像素宽的细线
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
    Java实现 蓝桥杯VIP 基础练习 Sine之舞
  • 原文地址:https://www.cnblogs.com/tszr/p/15966736.html
Copyright © 2020-2023  润新知