SpringBoot自动配置实现

创建SpringBoot自动配置

  • 新建一个Maven工程
  • 创建安全配置类
package com.cvsea.test.spring_boot_starter_hello;

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

@ConfigurationProperties(prefix="hello")//引用application.properties中的配置
public class HelloServiceProperties {

    private static final String MSG="world";

    private String msg=MSG;

    public String getMsg() {
        return msg;
    }

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

}
  • 创建使用配置的Service
package com.cvsea.test.spring_boot_starter_hello;

public class HelloService {
    private String msg;

    public String getMsg() {
        return msg;
    }

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

    public String sayHello()
    {
        return "Hello,"+msg;
    }
}
  • 创建自动配置类
package com.cvsea.test.spring_boot_starter_hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    public HelloService helloService()
    {
        HelloService helloService=new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}
  • 使用spring.factories注册自动配置类
org.springframework.boot.autoconfiguration.EnableAutoConfig=com.cvsea.test.spring_boot_starter_hello.HelloServiceAutoConfiguration

测试

  • :使用starter创建一个SpringBoot项目,加入刚刚实现的自动配置的starter作为依赖
<dependency>
    <groupId>com.cvseagroupId>
    <artifactId>spring-boot-starter-helloartifactId>
    <version>0.0.1-SNAPSHOTversion>
dependency>
  • 在application.properties添加配置内容
hello.msg=pxs
  • 调用自动配置中的方法
package com.cvsea;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cvsea.test.spring_boot_starter_hello.HelloService;

@RestController
@SpringBootApplication
public class Learning1Application {

    @Autowired
    HelloService helloService;

    @RequestMapping("/")
    public String hello()
    {
        return helloService.sayHello(); 
    }

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

你可能感兴趣的:(SpringBoot)