springboot-22-自定义starter

先说下springboot的运行原理

springboot最主要的配置 是 @SpringBootApplication

 

 然后这里面 @EnableAutoCOnfiguration 最为重要, 继续往里跟

 

 

可看到通过 @Import 导入了一个 EnableAutoConfigurationImportSelector 的类

在这个类中, 通过SpringFactoriesLoader.loadFactoryNames方法来扫描所有包下的 spring.factories文件,,, 而在spring-boot-autoconfigure.jar下就有这个文件

 

点进去几个注解可发现, 都有这么几个注解: 

@ConditionalOnBean:              容器中有指定的Bean时
@ConditionalOnMissingBean:   没有

@ConditionalOnClass:              类路径下游指定的类时 
@ConditionalOnMissingClass:   没有

@ConditionalOnExpression:      基于spel表达式判断
@ConditionalOnJava:                基于JVM版本判断
@ConditionalOnJndi:                 基于JNDI存在的条件下查找指定位置
@ConditionalOnProperty:          指定的属性是否有指定的值
@ConditionalOnResource:         类路径是否有指定的值
@ConditionalOnSingleCadidate: 指定的bean在容器中只有一个, 或者有多个但是指定首选的bean

@ConditionalOnWebApplication: 是Web项目的时候
@ConditionalOnNotWebApplication: 不是Web项目的时候

 

 实例分析: 

找到 HttpEncodingAutoConfiguration 并点击进去: 

配置类: 
@Configuration
@EnableConfigurationProperties(HttpEncodingProperties.class)    // 开启属性注入声明, @Autowired注入
@ConditionalOnWebApplication
@ConditionalOnClass(CharacterEncodingFilter.class)      // 在类路径下
// 配置文件中有这个配置才会生效 @ConditionalOnProperty(prefix
= "spring.http.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration {   
  // 注入properties
private final HttpEncodingProperties properties; public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) { this.properties = properties; }
  // 设置 @Bean @ConditionalOnMissingBean(CharacterEncodingFilter.
class) public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE)); return filter; } }

 上面需要一个HttpEncodingProperties, 这儿再看一下

其他属性未列出, 只看需要的: 

@ConfigurationProperties(prefix = "spring.http.encoding")
public class HttpEncodingProperties {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    public Charset getCharset() {
        return this.charset;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }
}

其实是一个用来读取yml配置的安全属性注入的类, 没有配置就默认使用UTF8;

实战

新建一个maven项目: 

pom.xml

<!-- springboot的自动配置, 自定义starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

属性注入类: 

package com.wenbronk.core.autoconfig;

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

/**
 * 自定义starter的配置类
 * Created by wenbronk on 2017/5/18.
 */
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    public static final String MSG = "world";

    private String msg = MSG;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMSG() {
        return MSG;
    }
}

 

实例类: 

package com.wenbronk.core.autoconfig;

/**
 * 执行类
 * 根据这个类是否存在, 来创建这个类的Bean
 * Created by wenbronk on 2017/5/18.
 */
public class HelloService {

    private String msg;

    public String sayHello() {
        return "hello" + msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

自动配置类: 

package com.wenbronk.core.autoconfig;

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;

/**
 *
 * 自动配置类
 * Created by wenbronk on 2017/5/18.
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)       // 声明开启属性注入, 通过 @Autowired注入
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)
public class AutoConfigHello {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloProperties.getMSG());
        return helloService;
    }

}

 

如果想要配置生效, 需要在resources/META-INF/spring.propertes

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wenbronk.core.autoconfig.AutoConfigHello

 

此时将自己的项目打包, 然后发布在私服上, 就可以在其他项目中通过引入依赖 进行自动开启了

 

 

 

 

原创地址: http://www.cnblogs.com/wenbronk/p/6873627.html, 转载请注明出处, 谢谢

posted @ 2017-05-18 15:27  bronk  阅读(454)  评论(0编辑  收藏  举报