1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot 學習——運行原理學習及自定義 Starter pom

        共 7673字,需瀏覽 16分鐘

         ·

        2017-04-18 17:05

        SpringBoot學習——運行原理學習及自定義Starter pom

        運行原理

        SpringBoot最大的特點就是提供了很多默認的配置,該能力就是通過Spring4.x提供了基于條件來配置Bean的能力這一原理來實現(xiàn)的。
        那么我們如何來實現(xiàn)一個自定義的starter呢

        自定義Starter pom

        新建一個maven項目

        參考SpringBoot學習——SpringBoot入門HelloWorld

        總目錄結構

        修改pom.xml,添加相關依賴

        
            4.0.0
            com.pzr
            spring-boot-starter-pzrhello
            0.0.1-SNAPSHOT
            
            
                org.springframework.boot
                spring-boot-starter-parent
                1.5.1.RELEASE
            
            
            
                
                
                    org.springframework.boot
                    spring-boot-autoconfigure
                
                
                
                    org.springframework.boot
                    spring-boot-starter-web
                
        
            
        

        添加屬性配置類

        package com.pzr.spring_boot_stater_pzrhello;
        
        import org.springframework.boot.context.properties.ConfigurationProperties;
        
        /**
         * 屬性配置類
         * @author pzr
         *
         */
        @ConfigurationProperties(prefix="hello")
        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;
            }
        
        }

        代碼說明:
        使用@ConfigurationProperties注解來設置前綴,在application中通過hello.msg=來設置,若不設置,默認為hello.msg=world。

        添加判斷依據(jù)類

        package com.pzr.spring_boot_stater_pzrhello;
        
        /**
         * 判斷依據(jù)類
         * @author pzr
         *
         */
        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;
            };
        
        }
        

        代碼說明
        通過此類來調用msg,進行打印。

        添加自動配置類

        package com.pzr.spring_boot_stater_pzrhello;
        
        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;
        
        /**
         * 自動配置類
         * @author pzr
         *
         */
        @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;
            }
        
        }

        代碼說明

        1. @Configuration:使用該注解來說明該類是配置類,等價于xml中的beans
        2. @EnableConfigurationProperties(HelloServiceProperties.class):開啟屬性注入,對注解配置Bean的支持
        3. @ConditionalOnClass(HelloService.class):條件注解,當類路徑下有指定的類的條件下。
        4. @ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true):條件注解,指定的屬性是否有指定的值。當設置hello=enabled,如果沒有設置則默認為true,即為條件符合。假如我們將matchIfMissing設置為false,則當設置hello=enabled時,條件為false,則不會將該Bean加載進容器類,當使用@Autowired注入HelloService時會報錯。
          org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.pzr.spring_boot_stater_pzrhello.HelloService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        5. @Autowired:將屬性配置類注入進來。
        6. @Bean:使用Java配置的方式來配置這個類,等價于xml中的bean。
        7. @ConditionalOnMissingBean(HelloService.class):容器中沒有這個Bean時,新建這個Bean。

        注冊配置

        在src/main/resource下新建META-INFO/spring.factories文件。該文件可在spring-boot-1.5.1.RELEASE\spring-boot-autoconfigure\src\main\resources\META-INF下找到。
        個人的只需要設置如下即可:

        # Auto Configure
        org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        com.pzr.spring_boot_stater_pzrhello.HelloServiceAutoConfiguration
        

        如果有多個,逗號分隔即可,如下:

        # Auto Configure
        org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
        org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
        org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration

        同項目下使用

        添加主方法

        在當前項目下,添加MainApplication.java,用于測試之前寫的starter是否成功

        package com.pzr.spring_boot_stater_pzrhello;
        
        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;
        
        @RestController
        @SpringBootApplication
        public class MainApplication {
        
            @Autowired
            HelloService helloService;
        
            @RequestMapping("/")
            public String index(){
                return helloService.sayHello();
            }
        
            public static void main(String[] args){
                SpringApplication.run(MainApplication.class, args);
            }
        }
        

        添加application.properties

        在src/main/resources下添加application.properties配置文件

        debug=true
        hello.msg=1231231

        debug是為了看到啟動日志
        hello.msg是我們自定義的打印內容

        啟動項目

        結果如下:

        打包使用

        將項目打包并發(fā)到本地庫

        右鍵項目-->Run as-->Maven build..

        輸入clean install發(fā)到本地庫

        新建一個maven項目

        修改pom文件

        
            4.0.0
            HelloConsumer
            HelloConsumer
            0.0.1-SNAPSHOT
            
            
                org.springframework.boot
                spring-boot-starter-parent
                1.5.1.RELEASE
            
            
            
                
                    
                        org.springframework.boot
                        spring-boot-maven-plugin
                    
                    
                    
                        org.apache.maven.plugins
                        maven-jar-plugin
                        
                            
                                
                                    true
                                    com.pzr.consumer.test.OfficialDemo
                                
                            
                        
                    
                
            
            
            
                
                
                    org.springframework.boot
                    spring-boot-starter-web
                
        
                
                    com.pzr
                    spring-boot-starter-pzrhello
                    0.0.1-SNAPSHOT
                
        
            
        

        其中spring-boot-starter-pzrhello是我們剛剛打的包

        添加MainApplication.java文件

        package com.pzr.springbootautoconfig;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.annotation.ComponentScan;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        
        import com.pzr.spring_boot_stater_pzrhello.HelloService;
        
        @RestController
        @ComponentScan(basePackages={"com.pzr"})  
        @SpringBootApplication
        public class MainApplication {
        
            @Autowired
            HelloService helloService;
        
            @RequestMapping("/")
            public String index(){
                return helloService.sayHello();
            }
        
            public static void main(String[] args){
                SpringApplication.run(MainApplication.class, args);
            }
        }
        

        注意:如果不設置@ComponentScan(basePackages={"com.pzr"}) ,則會掃描不到com.pzr.spring_boot_stater_pzrhello下的bean,只會默認掃描所在類包下的所有目錄。

        添加application.properties

        debug=true
        hello.msg=pzrmsg
        

        運行查看效果


        從結果可以看出雖然spring-boot-starter-pzrhello包里也有application.properties文件,但是最終使用的是新項目中的application.properties文件的設置。

        問題

        1. 無論我設不設置@ConditionalOnMissingBean(HelloService.class),bean都會加載。
        2. 無論我在不在spring.factories中添加HelloServiceAutoConfiguration,配置都會生效。
        3. 在jar包使用時,如果不設置@ComponentScan(basePackages={"com.pzr"})將會報錯,找不到bean。給注解是設置掃描位置的,可設置多個。
          org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.pzr.spring_boot_stater_pzrhello.HelloService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

          最近將會繼續(xù)學習springboot,如果有大神能看到這篇筆記,希望能不吝賜教。

        瀏覽 45
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            我与娇妻和黑人交换 | 成年人网站国产 | 有肏屄的视频吗 | 人与野鲁xxxx毛片十二寡妇 | 午夜精品一区二区三区视频免费看 | ysl蜜桃色成人 | 董小宛和天美传媒兄妹在干嘛 | 一级伊人 | 偷拍肏屄| 动漫美女无遮挡 |