4个注解+一个spring.factories文件建一个start

pom.xml


    4.0.0
    com.allen.start
    no1
    0.0.1-SNAPSHOT
    no1
    自定义启动器

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-autoconfigure
            2.1.6.RELEASE
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                
                    1.8
                    1.8
                    1.8
                
            
        
    


 

自定义start

4个注解+一个spring.factories完成自定义start

4个注解:

2个作用:

  1. 属性类(把配置文件的配置项目转化java对象)@ConfigurationProperties
  2. 自动配置类:

2.1 指定其是配置类---@Configuration

    1. 开启属性配置@EnableConfigurationProperties
    2. 指定其自动配置类在什么情况下,使用--@ConditionalOnClass(条件类)

 

 

    1. @Configuration

告诉spring这个是配置类

1.2 @ConfigurationProperties(prefix = "前缀")

    告诉spring这个配置属性类,并归spring管理可以直接注入使用

    1. @EnableConfigurationProperties(@ConfigurationProperties修饰的类.class)

开启此配置属性类

    1. @ConditionalOnClass(MyService.class--条件类)

条件类,说明classpath下有此类时,此配置类才可以使用

  1. 在resource建个META-INF目录,并新建一个spring.factorties文件

其内容:自动配置类的全限定名

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.allen.start.no1.mystart.MyServiceAutoConfiguration

 

 

  1. 配置属性类
import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.context.properties.ConfigurationProperties;



import java.nio.charset.StandardCharsets;



/**

 * @author AW

 * @date 2019/7/19 10:13

 * @desc 用于和配置文件字段映射

 * @ConfigurationProperties 2个作用:

 * 1. 标识此类是配置属性类

 * 2. 此类被spring管理,可以直接注入

 */

@ConfigurationProperties(prefix = "allen"//必须要有@EnableConfigurationProperties使用此类,否则idea飘红

public class MyProperties {



    private static final String DEFAULT_NAME = "allen";

    private static final String DEFAULT_MSG = "JAVA之窗";

    private String name = DEFAULT_NAME;

    private String msg = DEFAULT_MSG;



    public String getName() {

        return name;

    }



    public void setName(@Value("${allen.name}") String name) {

        //解决配置文件中文乱码问题

        this.name = new String(name.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

//        this.name = name;

    }



    public String getMsg() {

        return msg;

    }



    public void setMsg(String msg) {

        this.msg = new String(msg.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

//        this.msg = msg;

    }

}
  1. 此start对外提供的服务
/**

 * @author AW

 * @date 2019/7/19 10:16

 * @desc 用于其他项目调用(测试用的)

 */

public class MyService {

    private String msg;

    private String name;

    public String sayHello() {

        return name + " say " + msg + " !";

    }



    public String getMsg() {

        return msg;

    }



    public void setMsg(String msg) {

        this.msg = msg;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }

}
  1. 配置类:
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;



import javax.annotation.Resource;



/**

 * @author AW

 * @date 2019/7/19 10:17

 * @desc

 * 1. 是个配置类

 * 2. 打开配置项开关(如果此jar被使用)

 * 3. 开关:什么条件下,开启此功能

 *

 */

@Configuration

@EnableConfigurationProperties(MyProperties.class)

@ConditionalOnClass(MyService.class)

public class MyServiceAutoConfiguration {



    @Resource

    MyProperties helloProperties;



    /**

     * 创建一个bean,一般其他依赖此jar包的项目可以直接使用

     */

    @Bean

    MyService myService() {

        MyService myService = new MyService();

        myService.setName(helloProperties.getName());

        myService.setMsg(helloProperties.getMsg());

        return myService;

    }

}

ps:把此项目,执行maven的install命令打包到本地,其他项目引入依赖即可

源码在我的csdn可以下载

 

 

你可能感兴趣的:(Spring,Boot)