5.springboot自定义starter

2018-10-23  本文已影响0人  小manong
@ConditionalOnBean:当SpringIoc容器内存在指定Bean的条件 
@ConditionalOnClass:当SpringIoc容器内存在指定Class的条件 
@ConditionalOnExpression:基于SpEL表达式作为判断条件 
@ConditionalOnJava:基于JVM版本作为判断条件 
@ConditionalOnJndi:在JNDI存在时查找指定的位置 
@ConditionalOnMissingBean:当SpringIoc容器内不存在指定Bean的条件 
@ConditionalOnMissingClass:当SpringIoc容器内不存在指定Class的条件 
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件 
@ConditionalOnProperty:指定的属性是否有指定的值 
@ConditionalOnResource:类路径是否有指定的值 
@ConditionalOnSingleCandidate:当指定Bean在SpringIoc容器内只有一个,或者虽然有多个但是指定首选的Bean 
@ConditionalOnWebApplication:当前项目是Web项目的条件

一、创建maven项目导入自动配置依赖

<?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>com.qiu</groupId>
    <artifactId>hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>

二、自定义业务所需要的默认操作

//配置默认的参数实体类
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    //消息内容
    private String message = "hello world";
    //是否显示消息内容
    private boolean show = true;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }
}

三、编写自定义的业务

//自定义的业务类
public class HelloService {
    //消息内容
    private String message;
    //是否显示消息内容
    private boolean show ;

    public String sayHello()
    {
        return show ? "Hello," + message : "Hidden";
    }


    public void setMessage(String message) {
        this.message = message;
    }

    public void setShow(boolean show) {
        this.show = show;
    }
}

四、自定义starter自动化配置

@Configuration//开启配置
@EnableConfigurationProperties(HelloProperties.class)//开启使用映射实体对象
@ConditionalOnClass(HelloService.class)//存在HelloService时初始化该配置类
public class HelloAutoConfiguration {
    //application.properties配置文件映射前缀实体对象
    @Autowired
    private HelloProperties helloProperties;

    /**
     * 根据条件判断不存在HelloService时初始化新bean到SpringIoc
     * @return
     */
    @Bean//创建HelloService实体bean
    @ConditionalOnMissingBean(HelloService.class)//缺失HelloService实体bean时,初始化HelloService并添加到SpringIoc
    public HelloService helloService()
    {
        System.out.println(">>>The HelloService Not Found,Execute Create New Bean.");
        HelloService helloService = new HelloService();
        helloService.setMessage(helloProperties.getMessage());//设置消息内容
        helloService.setShow(helloProperties.isShow());//设置是否显示
        return helloService;
    }
}

五、自定义spring.factories

//配置自定义Starter的自动化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.qiu.hello.HelloAutoConfiguration

六、另一个项目中进行测试


        <!--自定义starter依赖-->
        <dependency>
            <groupId>com.qiu</groupId>
            <artifactId>hello</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
@RestController
public class HelloController
{
    //注入自定义starter内逻辑
    @Autowired
    HelloService helloService;

    /**
     * 测试访问地址/hello
     * @return 格式化字符串
     */
    @RequestMapping(value = "/hello")
    public String sayHello()
    {
        return helloService.sayHello();
    }
}

返回:Hello,hello world


参考:https://blog.csdn.net/weixin_42033269/article/details/80026078?utm_source=blogxgwz0

上一篇 下一篇

猜你喜欢

热点阅读