spring boot--creating your own auto-configuration

本篇主要讲述spring boot自动配置的部分功能,也算是为自己抛砖引玉,逐步的去理解博大精深的spring,更多自动配置细节请参考官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html#boot-features-custom-starter
看看大神的博客:http://www.jianshu.com/p/ddb6e32e3faf
http://blog.csdn.net/liaokailin/article/details/49559951
下面开始实践吧:

一、制作你需要auto configuration的jar包:

bookstore-common:
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── jiaobuchong
    │   │           └── common
    │   │               ├── config
    │   │               │   ├── MyConfiguration.java
    │   │               │   └── MyDatabaseConfig.java
    │   └── resources
    │       └── META-INF
    │           └── spring.factories
    └── test
MyDatabaseConfig.java:
package com.jiaobuchong.common.config;

public class MyDatabaseConfig {
    private String databaseName;

    public MyDatabaseConfig(String databaseName) {
        this.databaseName = databaseName;
    }

    public void testConfig() {
        System.out.println(databaseName);
        System.out.println("spring boot--creating your own auto-configuration");
    }
}
MyConfiguration.java:
package com.jiaobuchong.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfiguration {
 @Bean
 @ConditionalOnProperty("com.jiaobuchong.myConfig.enabled")
 @ConditionalOnMissingBean(MyDatabaseConfig.class)
    public MyDatabaseConfig myDatabaseConfig(@Value("${com.jiaobuchong.databaseName}") 
    String databaseName) {
        return new MyDatabaseConfig(databaseName);
    }
}
@Bean是表示注册为spring ApplicationContext中的bean,
@ConditionalOnProperty:allows configuration to be 
included based on a Spring Environment property. 
Use the prefix and name attributes to specify the 
property that should be checked. By default any 
property that exists and is not equal to false 
will be matched.(来自官方手册)意思就是通过检查特定的属性去进行自动配置。
@ConditionalOnMissingBean:表示这个类在classpath路径下找 
不到才会去auto-config对应的类型,生成对应类型的bean。
@Value: 将配置文件中的内容注入到对应的变量中
spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jiaobuchong.common.config.MyConfiguration
Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar. 
The file should list your configuration classes under the EnableAutoConfiguration key.(来自官方手册)

现在使用mvn clean install 到本地仓库中

二、引入刚才创建的jar包

现在新开一个maven项目,引入刚才创建的jar包:
        <dependency>
            <groupId>com.jiaobuchong</groupId>
            <artifactId>bookstore-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
项目配置文件,application.yml:
com:
  jiaobuchong:
    myConfig:
      enabled: true
    databaseName: jiaobuchong
写一个HelloController.java测试一下:
package com.jiaobuchong.springboot.controller;

import com.jiaobuchong.common.config.MyDatabaseConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/test")
@RestController
public class HelloController1 {
    @Autowired
    private MyDatabaseConfig myDatabaseConfig;

    @RequestMapping("/autoconfig")
    public String testAutoConfig() {
        myDatabaseConfig.testConfig();
        return "autoconfig ok!";
    }
}
运行Application的main()方法:
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.run(args);
    }
}

访问:http://localhost:8080/test/autoconfig,在console中运行结果:

jiaobuchong
spring boot--creating your own auto-configuration
That's ok, your own auto-configuration is ok。说明我们将自定义的jar包导到classpath中,spring boot可以自动配置
了,通过@Value标注的变量也将相应的配置值注入到对应的变量中去了, so nice。再来看看其中的小细节,如果将
com:
  jiaobuchong:
    myConfig:
      enabled: false
这个设置为false,那 MyDatabaseConfig 这个类就不会通过自动配置注册为spring上下文中的bean了,故 @ConditionalOnProperty 
这个注解的意义大伙应该心知肚明了吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值