【springboot】基于springboot运行原理实现springboot的自动配置

    本文参照Http编码的配置源码,自己实现一个starter。

    1.新建maven工程。



    在pom.xml中修改,添加autoconfigure依赖:

<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.springboot</groupId>
  <artifactId>springboot_starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <packaging>jar</packaging>

  <name>springboot_starter</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>1.5.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>1.5.8.RELEASE</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

    2.属性配置类,代码如下:

package com.springboot;

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

import javax.naming.Name;

/**
 * Created by leijun on 2018/4/3.
 */
@ConfigurationProperties(prefix = "user")
public class UserInfoProperties {

    private static final String UNAME = "springboot";

    private String uName = UNAME;

    public String getUName() {
        return uName;
    }

    public void setUName(String uName) {
        this.uName = uName;
    }
}

    3.判断依据类,代码如下:

package com.springboot;

/**
 * Created by leijun on 2018/4/3.
 */
public class UserInfo {

    private String uName;

    public String getUserInfo(){
        return "My name is " + getUName();
    }

    public String getUName() {
        return uName;
    }

    public void setUName(String uName) {
        this.uName = uName;
    }
}

    4.自动配置类,代码如下:

package com.springboot;

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 leijun on 2018/4/3.
 */
@Configuration
@EnableConfigurationProperties(UserInfoProperties.class)
@ConditionalOnClass(UserInfo.class)
@ConditionalOnProperty(prefix = "user",value = "enabled",matchIfMissing = true)
public class UserInfoAutoConfiguration {

    @Autowired
    private UserInfoProperties userInfoProperties;

    @Bean
    @ConditionalOnMissingBean(UserInfo.class)
    public UserInfo userInfo(){
        UserInfo userInfo = new UserInfo();
        userInfo.setUName(userInfoProperties.getUName());
        return userInfo;
    }
}

    5.注册配置。自动配置类需要注册才可以实现自动配置。在resources下创建META-INF/spring.factories,spring.factories中内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.springboot.UserInfoAutoConfiguration

    其中如果想要多个自动配置类,用”,“隔开,”\“作用是换行仍能读到属性值。

    至此,整个starter编写完成,下面来使用这个starter。

    创建新springboot的项目。



    在pom.xml中添加刚才编写的springboot_starter的依赖:

<dependency>
   <groupId>com.springboot</groupId>
   <artifactId>springboot_starter</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

    测试类代码如下:

package com.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by leijun on 2018/4/3.
 */
@RestController
@SpringBootApplication
public class App {

    @Autowired
    private UserInfo userInfo;

    @GetMapping("/")
    public String getUserInfo(){
        return userInfo.getUserInfo();
    }

    public static void main( String[] args ){
        SpringApplication.run(App.class);
    }
}

    测试类中UserInfo在新项目中并没有配置Bean,但是可以注入,这就是通过自动配置完成的。运行程序,在浏览器输入localhost:8080,效果如下:


    在application.properties中配置uName的属性值:

user.uName=leijun

    在浏览器再次测试:


    在application.properties中添加debug属性,查看自动配置报告:

debug=true

    可以看到我们自定义的配置显示在控制台:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值