美文网首页
自定义《自动装配》

自定义《自动装配》

作者: RalapHao | 来源:发表于2017-09-13 15:16 被阅读0次
    Paste_Image.png

    引言:

    Spring 传统项目配置http编码:

    <filter>
         <filter-name>CharacterEncoding</filter-name>
         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
         </init-param>
         <init-param>
             <param-name>forceEncoding</param-name>
             <param-value>true</param-value>
         </init-param>
     </filter>
    

    Spring Boot中没有配置,因使用自动配置帮我们设置好,那么到底是怎么自动配置的呢,下面我通过自定义自动配置来更进一步的了解它的原理。

    正文

    1. 使用maven将我们要配置的类打成jar文件在本地仓库中,然后创建Spring Boot 项目使用验证。
    2. 话不多说,上代码

    pom.xml

    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
      </dependency>
    </dependencies>
    

    HelloServiceProper

    //设置读取application.properties文件中的前缀
    @ConfigurationProperties(prefix = "hello")
    public class HelloServiceProper {
        private static final String MSG  = "world";
        private String msg = MSG;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    

    HelloService
    自动装配的类

    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;
        }
    }
    

    HelloConfig
    类加载

    //项目启动时加载
    @Configuration
    //加载读取spring.factories文件
    @EnableConfigurationProperties(HelloServiceProper.class)
    //加载类别(当前路径加载类)
    @ConditionalOnClass(HelloService.class)
    @ConditionalOnProperty(prefix = "hello",value = "enabled", matchIfMissing = true)
    public class HelloServiceConfig {
    
        @Autowired
        private HelloServiceProper helloServiceProper;
        @Bean
        @ConditionalOnMissingBean(HelloService.class)
        public HelloService helloService(){
            HelloService helloService = new HelloService();
            helloService.setMsg(helloServiceProper.getMsg());
            return helloService;
    
        }
    
    }
    

    spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ralap.auto.HelloServiceConfig
    
    1. 使用自定义配置
      启动类
    @RestController
    @SpringBootApplication
    public class UseautoApplication {
    
    
        @Autowired
        private HelloService helloService;
    
        @GetMapping("/hello")
        public String hello(){
            return helloService.sayHello();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(UseautoApplication.class, args);
        }
    }
    

    application.properties

    hello.msg=hjx
    
    1. 效果
    Paste_Image.png

    相关文章

      网友评论

          本文标题:自定义《自动装配》

          本文链接:https://www.haomeiwen.com/subject/ztoksxtx.html