標籤: 包裝設計

  • 同是2.0T+7速雙離合,為何那麼多人放棄大眾買貴4萬的豪車?

    同是2.0T+7速雙離合,為何那麼多人放棄大眾買貴4萬的豪車?

    我曾經從一位一汽的裝配工口中聽過這樣一句話:大眾的車底盤焊縫要求不能伸進指甲就合格,但是。奧迪的要求是不能伸進頭髮。上面我們說到了兩車的造車成本以及測試種類,那就不免出現一個更加專業名詞——NVH,直白一點說就是車輛的胎噪,風噪和發動機噪音。

    在如今這個車水馬龍的城市中,滿大街都是車,而車一多,咱們買車選車就自然變得更加困難了。而且中國人買車需要的可不單單隻是性價比,更需要的是面子,並且一定要大。

    特別是當BBA這種豪華品牌不斷的將車型價格下探之後,吸引了更多的中國人可以花普通品牌的價錢買到掛着“豪華品牌商標”的汽車,這也是為什麼如今奔馳、寶馬、奧迪這些車會滿大街跑的原因。

    當然,相比起豪華品牌的紙醉金迷,普通品牌的優勢更多在於極高性價比。不過不可否認的是,豪華品牌的品牌溢價能力,的確是普通品牌無法比擬的。

    並且豪華車除了可以給人帶來身份的象徵,還帶來了更極佳的駕乘感受,畢竟“豪華”兩字已經說明了一切。如今各類豪華品牌的入門車型售價,儼然已經可以跟普通品牌針鋒相對了。

    那在面對預算有限的情況下,到底是咬咬牙弄台豪華品牌,享受一下豪車帶來的優越感呢,還是輕鬆的拿下普通品牌,得到其極具性價比配置的同時還能多買幾台蘋果XXX呢?

    如果有一天,你開着一輛頂配大眾高爾夫GTI去相親,對方父母只會認為你混得一般,但是當你開着一輛掛這“四個環”的丐版奧迪A3去相親,人家則會認為,這家孩子不錯,年紀輕輕就開得起奧迪了。

    一輛頂配的GTI比起低配的奧迪A3,怎麼著也得貴上好幾萬吧,但是為什麼人人都會認為奧迪更高級呢。

    奧迪嘛,在國人心目中一直存在着“官車”的形象,而大眾則是普羅大眾都消費得起的存在。而且再怎麼說你也只是大眾,我可是奧迪啊,世界公認的三大豪華品牌之一,買車看的不正是牌子,面子嘛!

    不過很多朋友都認為,A3其實就是換標高爾夫,而且那個標還不便宜。那這次咱們就以高爾夫GTI以及奧迪A3兩廂頂配來說話。到底用幾台蘋果XXX的錢去買一個四環標值不值得?

    目前兩車在廣州的裸車價基本是:奧迪A3頂配23萬左右,高爾夫GTI19萬左右,也就是四萬左右的差價,雖然不同地區情況不同,不過差價相信基本也是相差無幾。

    相信有了解過這兩款車的朋友都知道,兩者同為一汽旗下合資公司的產品、同是MQB平台、同一工廠生產、相同的EA888發動機、相同的變速箱。聽起來兩者的差異性極小,但是細分之後你就知道還是有區別的。

    相同的EA888發動機,在奧迪A3頂配身上只能輸出190匹馬力,但是在GTI身上則能爆發出220馬力。兩者都匹配了7速雙離合變速器,不過在大眾身上叫DSG,在奧迪身上喊S tronic而已。

    從平台上看,兩者均出於MQB橫置模塊化平台,這就不難讓人認為兩者的底盤變現以及操控性上是不是一致呢?事實卻並非如此。就好比一台計算機,平台就如同一個主板,再裝配不同的顯卡,聲卡,內存,CpU,不同搭配所得出的效果自然也是截然不同的。

    另外,雖然奧迪有着品牌溢價,但相對應的供應商也會與大眾有所區別,零配件的成本也會相應的提高,並且在裝配工藝上奧迪也高於大眾。。單單從用漆方面就可以看得出這點,奧迪A3大部分採用的均為高成本、高品質的珠光漆。

    還有奧迪需要做的測試種類,無論是整車測試還是零部件試驗的嚴苛程度,複雜性,精密度,都遠遠高於大眾。我曾經從一位一汽的裝配工口中聽過這樣一句話:大眾的車底盤焊縫要求不能伸進指甲就合格,但是………奧迪的要求是不能伸進頭髮!!!

    上面我們說到了兩車的造車成本以及測試種類,那就不免出現一個更加專業名詞——NVH,直白一點說就是車輛的胎噪,風噪和發動機噪音。從各大測評機構的測試結果都不難看出,奧迪A3對NVH的把控要比高爾夫好,另一方面也表明了,豪華品牌在駕乘感受上下的功夫要比大眾這種普通品牌更多。

    至於外觀,咱們就略過不說了,反正蘿蔔青菜各有所愛,奧迪自帶四環光環,高爾夫GTI則滿身情懷。內飾的話,GTI也不能說完敗,只能說那萬年不變的布局真的讓人又愛又恨,而A3的內飾那才真的稱得上人人喜愛,既精緻又簡潔還顯高級感,而且細看內飾的做工用料,無論是視覺的衝擊力還是觸感的細膩度上,相信什麼叫奧迪,什麼叫大眾自然一目瞭然了。

    不過在配置的差異上,奧迪A3比起GTI似乎真的閹割太多了,要知道那可是四萬塊的差價啊。此外,奧迪選裝配置的價錢也非常感人,一套真皮座椅就小一萬了,估計想選裝到GTI這麼豐富配置的時候你就得多掏幾百張毛爺爺了,不得不說奧迪這生意還真好賺!

    估計看到這裏,大家都不難得出一個結論:奧迪A3當然不是換標高爾夫那麼簡單。

    儘管GTI的馬力、配置都比A3更優越,但是從用料做工、質感品控、駕乘感受方面來看,A3比起GTI則是更勝一籌。終究是豪華品牌嘛,還是會有自己的擔當,哪怕是十幾萬起步的入門車型,他們都會用心去對待。所以與其說多花了幾萬塊錢要裝逼買個豪華商標,倒不如老老實實的承認“一分錢一分貨”這個道理。

    再舉個例子,如果奧迪A3賣着高爾夫的價錢,我估計你也不大敢買吧?又或者說,奧迪如果把A3的價格降低到高爾夫這個層次,我相信一定會大大衝擊高爾夫的銷量,而且奧迪作為豪華品牌的形象也會因此大打折扣。在汽車界,類似的例子比比皆是,所以單單拿平台來說事,似乎就有點無稽之談了。

    至於遇到類似的問題該怎麼去選擇,那就只能看個人需求以及兜里毛爺爺的多與少啦。反正有錢的話,那必須選豪華品牌,去酒吧把鑰匙往桌子一拍,嘿嘿,那效應老司機都懂都懂!本站聲明:網站內容來源於http://www.auto6s.com/,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※教你寫出一流的銷售文案?

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※回頭車貨運收費標準

    ※別再煩惱如何寫文案,掌握八大原則!

    ※超省錢租車方案

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • 玩車的人都在捧的本田發動機,真的這麼神?

    玩車的人都在捧的本田發動機,真的這麼神?

    然而,這種熟悉的“爆tec”感覺沒有出現在最新的謳歌TLX-L上。謳歌是本田的豪華品牌,一切的用料,技術,做工等都用上高標準。就連本田被人詬病的隔音謳歌也能做得安靜。被拉高轉的本田發動機“爆tec”時的感覺聲音和動力都有着明顯變化,讓人為之興奮,但開着謳歌TLX-L卻沒有這種感覺。

    本田,一個無人不知無人不曉的一個汽車品牌。尤其是在廣東地區,更是本田粉活躍的地方,大街上跑的10輛車中,有4輛或5輛是本田車型。

    買本田車,除了買質量可靠性外,更多的是為了“買發動機送車”,而坊間也流傳着本田金句VTEC is the best。可想而知,本田發動機技術含量和民望有多高。

    本田發動機的看家技術VTEC和i-VTEC技術外,在近年也推出VTEC Turbo這渦輪組合技術。

    那本田技術牛在哪?主要除了有可變氣門正時外,還有氣門升程這一技術。氣門升程技術並不是很多發動機有,除了寶馬和英菲尼迪VQ37系列外,還真是寥寥無幾。

    開過本田的人未必知道什麼叫“爆tec”,但拉過高轉的人就知道。當發動機轉速拉到5000轉左右的時候,聲音突變,發動機也感覺顫抖一下。這就是“爆tec”給人的感覺。

    然而,這種熟悉的“爆tec”感覺沒有出現在最新的謳歌TLX-L上。謳歌是本田的豪華品牌,一切的用料,技術,做工等都用上高標準。就連本田被人詬病的隔音謳歌也能做得安靜。

    被拉高轉的本田發動機“爆tec”時的感覺聲音和動力都有着明顯變化,讓人為之興奮,但開着謳歌TLX-L卻沒有這種感覺。難道就因為高端車型而被“技術”隔絕?

    其實這種“爆tec”的感覺在自主品牌的渦輪車上有着“更好的體現”。只不過我們吐槽它而已,這一技術就是渦輪遲滯。體驗過大部分自主品牌渦輪車,輕踩油門不去,踩重一點像是被踢出去的感覺。這種感覺比“爆tec”還爽快。

    總結:高轉“爆tec”還是低轉渦輪介入明顯,都使得發動機動力輸出沒那麼“線性”。只是拉高轉的時候,亢奮的聲音和動力的凸顯使得“爆tec”的感覺被神化。渦輪介入明顯,其實就是“爆tec”時動力凸顯的一部分,只是沒有亢奮的聲音罷了。本站聲明:網站內容來源於http://www.auto6s.com/,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※超省錢租車方案

    ※別再煩惱如何寫文案,掌握八大原則!

    ※回頭車貨運收費標準

    ※教你寫出一流的銷售文案?

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

  • Spring AOP學習筆記03:AOP的核心實現之獲取增強器

      上文講了spring是如何開啟AOP的,簡單點說就是將AnnotationAwareAspectJAutoProxyCreator這個類註冊到容器中,因為這個類最終實現了BeanPostProcessor接口,並且在其postProcessAfterInitialization()方法中完成了AOP代理對象的創建,創建時機則是在bean的init方法被執行之後即bean初始化完成之後。postProcessAfterInitialization()方法是重點,本文及下一篇文章都是圍繞着這個方法來的。

      該方法是實現在其父類中的AbstractAutoProxyCreator,我們先來看一下其實現:

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean != null) {
                // 根據給定的bean的class和name構件key,格式:beanClassName_beanName
            Object cacheKey = getCacheKey(bean.getClass(), beanName);
            if (!this.earlyProxyReferences.containsKey(cacheKey)) {
                        // 如果它適合被代理,則在這裏面生成代理類
                return wrapIfNecessary(bean, beanName, cacheKey);
            }
        }
        return bean;
    }
    
    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
        // 如果已經處理過則不再處理
        if (beanName != null && this.targetSourcedBeans.containsKey(beanName)) {
            return bean;
        }
        // 無需增強則直接返回
        if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
            return bean;
        }
        // 指定的bean類是否代表一個基礎設施類,基礎設施類不應被代理,或者配置了指定bean也不需要自動代理
        if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
            this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return bean;
        }
    
        // 如果存在Advice則創建代理
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
        // 如果獲取到了增強則需要針對增強創建代理
        if (specificInterceptors != DO_NOT_PROXY) {
            this.advisedBeans.put(cacheKey, Boolean.TRUE);
                // 創建代理
            Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
            this.proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        }
    
        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

      在上面的函數中我們已經可以看到代理創建的雛形,當然,在真正開始之前還需要經過一系列判斷,比如是否已經處理過或者是否是需要跳過的bean,而真正創建代理是從getAdvicesAndAdvisorsForBean開始的。
      創建代理主要包含了兩個步驟:

    • 獲取增強方法或者增強器;
    • 根據獲取的增強進行代理創建;

      這裏雖然看起來只有簡單的兩步,但是每一步中都有着大量複雜的邏輯。本文先來看看獲取增強方法的實現邏輯。

    protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) {
        List advisors = findEligibleAdvisors(beanClass, beanName);
        if (advisors.isEmpty()) {
            return DO_NOT_PROXY;
        }
        return advisors.toArray();
    }
    
    protected List<Advisor> findEligibleAdvisors(Class beanClass, String beanName) {
        List<Advisor> candidateAdvisors = findCandidateAdvisors();
        List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
        extendAdvisors(eligibleAdvisors);
        if (!eligibleAdvisors.isEmpty()) {
            eligibleAdvisors = sortAdvisors(eligibleAdvisors);
        }
        return eligibleAdvisors;
    }

      對於某個bean的增強方法的獲取也是包含兩個步驟的,獲取所有的增強以及尋找所有增強中適用於bean的增強並應用,而findCandidateAdvisors()與findAdvisorsThatCanApply()便是做了這兩件事情。這裏如果無法找到對應的增強器則直接返回DO_NOT_PROXY,也就是null。

    1. 獲取增強器

      這裏我們分析的是註解方式的AOP,對於findCandidateAdvisors的實現其實是由AnnotationAwareAspectJAutoProxyCreator類來完成的,繼續跟蹤:

    protected List<Advisor> findCandidateAdvisors() {
        // 使用註解方式配置AOP的時候並不會丟棄對XML配置的支持,在這裡會調用父類方法去加載配置文件中的AOP聲明
        List<Advisor> advisors = super.findCandidateAdvisors();
        // Build Advisors for all AspectJ aspects in the bean factory.
        advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
        return advisors;
    }

      AnnotationAwareAspectJAutoProxyCreator間接繼承了AbstractAdvisorAutoProxyCreator,在實現獲取增強的方法中除了保留父類的獲取配置文件中定義的增強外,同時增加了獲取Bean的註解增強的功能,這部分實現是由this.aspectJAdvisorsBuilder.buildAspectJAdvisors()來完成的。

      這裏其實可以自己先嘗試想象一下解析思路,看看自己的實現與Spring是否有差別?我們可以先在自己頭腦中嘗試實現一下獲取增強這個功能點,看看是否有思路。實際上,Spring在實現的時候主要分成了四步:

    • 獲取所有beanName,這一步驟中所有在beanFactory中註冊的Bean都會被提取出來;
    • 遍歷所有beanName,並找出聲明AspectJ註解的類,進行進一步的處理;
    • 對標記為AspectJ註解的類進行增強器的提取;
    • 將提取結果加入緩存;

      我們來看看Spring如何實現對所有的類進行分析並提取Advisor:

    public List<Advisor> buildAspectJAdvisors() {
        List<String> aspectNames = null;
    
        synchronized (this) {
            aspectNames = this.aspectBeanNames;
            if (aspectNames == null) {
                List<Advisor> advisors = new LinkedList<Advisor>();
                aspectNames = new LinkedList<String>();
                // 獲取所有的beanName
                String[] beanNames =
                        BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object.class, true, false);
                // 遍歷所有的beanName找出對應的增強方法
                for (String beanName : beanNames) {
                    // 不合法的bean則略過,由子類定義規則,默認返回true
                    if (!isEligibleBean(beanName)) {
                        continue;
                    }
                    // 獲取對應的bean的類型
                    Class beanType = this.beanFactory.getType(beanName);
                    if (beanType == null) {
                        continue;
                    }
                    // 如果該bean上存在Aspect註解
                    if (this.advisorFactory.isAspect(beanType)) {
                        aspectNames.add(beanName);
                        AspectMetadata amd = new AspectMetadata(beanType, beanName);
                        if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
                            MetadataAwareAspectInstanceFactory factory =
                                    new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName);
                            // 解析增強方法
                            List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
                            if (this.beanFactory.isSingleton(beanName)) {
                                this.advisorsCache.put(beanName, classAdvisors);
                            }
                            else {
                                this.aspectFactoryCache.put(beanName, factory);
                            }
                            advisors.addAll(classAdvisors);
                        }
                        else {
                            // Per target or per this.
                            if (this.beanFactory.isSingleton(beanName)) {
                                throw new IllegalArgumentException("Bean with name '" + beanName +
                                        "' is a singleton, but aspect instantiation model is not singleton");
                            }
                            MetadataAwareAspectInstanceFactory factory =
                                    new PrototypeAspectInstanceFactory(this.beanFactory, beanName);
                            this.aspectFactoryCache.put(beanName, factory);
                            advisors.addAll(this.advisorFactory.getAdvisors(factory));
                        }
                    }
                }
                this.aspectBeanNames = aspectNames;
                return advisors;
            }
        }
    
        if (aspectNames.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        // 記錄緩存
        List<Advisor> advisors = new LinkedList<Advisor>();
        for (String aspectName : aspectNames) {
            List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName);
            if (cachedAdvisors != null) {
                advisors.addAll(cachedAdvisors);
            }
            else {
                MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName);
                advisors.addAll(this.advisorFactory.getAdvisors(factory));
            }
        }
        return advisors;
    }

      到這裏已經完成了Advisor的提取,在上面步驟中最為重要也最為複雜的就是增強器的獲取,這一功能是委託給getAdvisors方法來實現(this.advisorFactory.getAdvisors(factory))。 

    public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory maaif) {
        // 獲取標記為AspectJ的類及其名字,並驗證
        final Class<?> aspectClass = maaif.getAspectMetadata().getAspectClass();
        final String aspectName = maaif.getAspectMetadata().getAspectName();
        validate(aspectClass);
    
        // We need to wrap the MetadataAwareAspectInstanceFactory with a decorator
        // so that it will only instantiate once.
        final MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
                new LazySingletonAspectInstanceFactoryDecorator(maaif);
    
        final List<Advisor> advisors = new LinkedList<Advisor>();
        for (Method method : getAdvisorMethods(aspectClass)) {
            // 獲取增強器
            Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
            if (advisor != null) {
                advisors.add(advisor);
            }
        }
    
        // If it's a per target aspect, emit the dummy instantiating aspect.
        if (!advisors.isEmpty() && lazySingletonAspectInstanceFactory.getAspectMetadata().isLazilyInstantiated()) {
            Advisor instantiationAdvisor = new SyntheticInstantiationAdvisor(lazySingletonAspectInstanceFactory);
            advisors.add(0, instantiationAdvisor);
        }
    
        // Find introduction fields.
        for (Field field : aspectClass.getDeclaredFields()) {
            Advisor advisor = getDeclareParentsAdvisor(field);
            if (advisor != null) {
                advisors.add(advisor);
            }
        }
    
        return advisors;
    }

      上面函數中首先完成了對增強器的獲取,包括獲取註解以及根據註解生成增強的步驟,然後考慮到在配置中可能會將增強配置成延遲初始化,那麼需要在首位加入同步實例化增強器以保證增強使用之前的實例化,最後是對DeclareParents註解的獲取,這裏着重分析對增強器的獲取。
      對普通增強器的獲取邏輯是實現在getAdvisor方法中的,實現步驟包括對切點的註解的獲取以及根據註解信息生成增強。

    public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aif,
            int declarationOrderInAspect, String aspectName) {
    
        validate(aif.getAspectMetadata().getAspectClass());
        // 切點信息的獲取
        AspectJExpressionPointcut ajexp =
                getPointcut(candidateAdviceMethod, aif.getAspectMetadata().getAspectClass());
        if (ajexp == null) {
            return null;
        }
        // 根據切點信息生成增強器
        return new InstantiationModelAwarePointcutAdvisorImpl(
                this, ajexp, aif, candidateAdviceMethod, declarationOrderInAspect, aspectName);
    }

    1.1 切點信息的獲取

      所謂獲取切點信息就是指定註解的表達式信息的獲取,如@Before(“test()”)。

    private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) {
        // 獲取方法上的註解
        AspectJAnnotation<?> aspectJAnnotation =
                AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
        if (aspectJAnnotation == null) {
            return null;
        }
        // 使用AspectJExpressionPointcut實例封裝獲取的信息
        AspectJExpressionPointcut ajexp =
                new AspectJExpressionPointcut(candidateAspectClass, new String[0], new Class[0]);
        // 提取得到的註解中的表達式,如:
        // @Pointcut("execution(* *.*test*(..))")中的execution(* *.*test*(..))")
        ajexp.setExpression(aspectJAnnotation.getPointcutExpression());
        return ajexp;
    }
    
    protected static AspectJAnnotation findAspectJAnnotationOnMethod(Method method) {
        // 設置敏感的註解類
        Class<? extends Annotation>[] classesToLookFor = new Class[] {
                Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class};
        for (Class<? extends Annotation> c : classesToLookFor) {
            AspectJAnnotation foundAnnotation = findAnnotation(method, c);
            if (foundAnnotation != null) {
                return foundAnnotation;
            }
        }
        return null;
    }
    
    // 獲取指定方法上的註解並使用AspectJAnnotation封裝
    private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
        A result = AnnotationUtils.findAnnotation(method, toLookFor);
        if (result != null) {
            return new AspectJAnnotation<A>(result);
        }
        else {
            return null;
        }
    }

    1.2 根據切點信息生成增強

      所有的增強都是由Advisor的實現類InstantiationModelAwarePointcutAdvisorImpl統一封裝的。

    public InstantiationModelAwarePointcutAdvisorImpl(AspectJAdvisorFactory af, AspectJExpressionPointcut ajexp,
            MetadataAwareAspectInstanceFactory aif, Method method, int declarationOrderInAspect, String aspectName) {
    
        this.declaredPointcut = ajexp;
        this.method = method;
        this.atAspectJAdvisorFactory = af;
        this.aspectInstanceFactory = aif;
        this.declarationOrder = declarationOrderInAspect;
        this.aspectName = aspectName;
    
        if (aif.getAspectMetadata().isLazilyInstantiated()) {
            // Static part of the pointcut is a lazy type.
            Pointcut preInstantiationPointcut =
                    Pointcuts.union(aif.getAspectMetadata().getPerClausePointcut(), this.declaredPointcut);
    
            // Make it dynamic: must mutate from pre-instantiation to post-instantiation state.
            // If it's not a dynamic pointcut, it may be optimized out
            // by the Spring AOP infrastructure after the first evaluation.
            this.pointcut = new PerTargetInstantiationModelPointcut(this.declaredPointcut, preInstantiationPointcut, aif);
            this.lazy = true;
        }
        else {
            // A singleton aspect.
            this.instantiatedAdvice = instantiateAdvice(this.declaredPointcut);
            this.pointcut = declaredPointcut;
            this.lazy = false;
        }
    }

      在封裝過程中只是簡單地將信息封裝在類的實例中,所有的信息單純地賦值,在實例初始化的過程中還完成了對於增強器的初始化。因為不同的增強所體現的邏輯是不同的,比如@Before(“test()”)與@After(“test()”)標籤的區別就是增強器增強的位置不同,所以就需要不同的增強器來完成不同的邏輯,而根據註解中的信息初始化對應的增強器就是在instantiateAdvice函數中實現的。

    private Advice instantiateAdvice(AspectJExpressionPointcut pcut) {
        return this.atAspectJAdvisorFactory.getAdvice(
                this.method, pcut, this.aspectInstanceFactory, this.declarationOrder, this.aspectName);
    }
    
        public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut ajexp,
            MetadataAwareAspectInstanceFactory aif, int declarationOrderInAspect, String aspectName) {
    
        Class<?> candidateAspectClass = aif.getAspectMetadata().getAspectClass();
        validate(candidateAspectClass);
    
        AspectJAnnotation<?> aspectJAnnotation =
                AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
        if (aspectJAnnotation == null) {
            return null;
        }
    
        // If we get here, we know we have an AspectJ method.
        // Check that it's an AspectJ-annotated class
        if (!isAspect(candidateAspectClass)) {
            throw new AopConfigException("Advice must be declared inside an aspect type: " +
                    "Offending method '" + candidateAdviceMethod + "' in class [" +
                    candidateAspectClass.getName() + "]");
        }
    
        if (logger.isDebugEnabled()) {
            logger.debug("Found AspectJ method: " + candidateAdviceMethod);
        }
    
        AbstractAspectJAdvice springAdvice;
        // 根據不同的註解類型封裝不同的增強器
        switch (aspectJAnnotation.getAnnotationType()) {
            case AtBefore:
                springAdvice = new AspectJMethodBeforeAdvice(candidateAdviceMethod, ajexp, aif);
                break;
            case AtAfter:
                springAdvice = new AspectJAfterAdvice(candidateAdviceMethod, ajexp, aif);
                break;
            case AtAfterReturning:
                springAdvice = new AspectJAfterReturningAdvice(candidateAdviceMethod, ajexp, aif);
                AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
                if (StringUtils.hasText(afterReturningAnnotation.returning())) {
                    springAdvice.setReturningName(afterReturningAnnotation.returning());
                }
                break;
            case AtAfterThrowing:
                springAdvice = new AspectJAfterThrowingAdvice(candidateAdviceMethod, ajexp, aif);
                AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
                if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
                    springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
                }
                break;
            case AtAround:
                springAdvice = new AspectJAroundAdvice(candidateAdviceMethod, ajexp, aif);
                break;
            case AtPointcut:
                if (logger.isDebugEnabled()) {
                    logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
                }
                return null;
            default:
                throw new UnsupportedOperationException(
                        "Unsupported advice type on method " + candidateAdviceMethod);
        }
    
        // Now to configure the advice...
        springAdvice.setAspectName(aspectName);
        springAdvice.setDeclarationOrder(declarationOrderInAspect);
        String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
        if (argNames != null) {
            springAdvice.setArgumentNamesFromStringArray(argNames);
        }
        springAdvice.calculateArgumentBindings();
        return springAdvice;
    }

      從上面可以看到,Spring會根據不同的註解生成不同的增強器,例如AtBefore會對應AspectJMethodBeforeAdvice,在AspectJMethodBeforeAdvice中完成了增強方法的邏輯。本文嘗試分析幾個常用的增強器實現。

    AspectJMethodBeforeAdvice

      這是前置增強器,在Spring中,它會封裝到MethodBeforeAdviceInterceptor類的內部,這個是一個攔截器,會被放在攔截器鏈中,在創建代理bean時會用到:

    public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {
    
        private MethodBeforeAdvice advice;
    
        /**
         * Create a new MethodBeforeAdviceInterceptor for the given advice.
         * @param advice the MethodBeforeAdvice to wrap
         */
        public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
            Assert.notNull(advice, "Advice must not be null");
            this.advice = advice;
        }
    
        public Object invoke(MethodInvocation mi) throws Throwable {
            this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );
            return mi.proceed();
        }
    }

      其中的屬性MethodBeforeAdvicce就代表着前置增強的AspectJMethodBeforeAdvice,跟蹤其before()方法:

    public void before(Method method, Object[] args, Object target) throws Throwable {
        invokeAdviceMethod(getJoinPointMatch(), null, null);
    }
    
    protected Object invokeAdviceMethod(JoinPointMatch jpMatch, Object returnValue, Throwable ex) throws Throwable {
        return invokeAdviceMethodWithGivenArgs(argBinding(getJoinPoint(), jpMatch, returnValue, ex));
    }
    
    protected Object invokeAdviceMethodWithGivenArgs(Object[] args) throws Throwable {
        Object[] actualArgs = args;
        if (this.aspectJAdviceMethod.getParameterTypes().length == 0) {
            actualArgs = null;
        }
        try {
            ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);
            // 激活增強方法
            return this.aspectJAdviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance(), actualArgs);
        }
        catch (IllegalArgumentException ex) {
            throw new AopInvocationException("Mismatch on arguments to advice method [" +
                    this.aspectJAdviceMethod + "]; pointcut expression [" +
                    this.pointcut.getPointcutExpression() + "]", ex);
        }
        catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }

       invokeAdviceMethodWithGivenArgs方法中的aspectJAdviceMethod正是對應於前置增強的方法,在這裏實現了調用。

    AspectJAfterAdvice

      後置增強與前置增強有稍許不一致的地方。回顧上面講過的前置增強,大致的結構是在攔截器鏈中放置MethodBeforeAdviceInterceptor,並在MethodBeforeAdviceInterceptor中又放置了AspectJMethodBeforeAdvice,在調用inovke時首先串聯調用。但是在後置增強的時候卻是不一樣的,沒有提供如MethodBeforeAdviceInterceptor的中間類,而是直接實現MethodInterceptor接口,並在攔截器鏈中使用了中間的AspectAfterAdvice。

    public class AspectJAfterAdvice extends AbstractAspectJAdvice implements MethodInterceptor, AfterAdvice {
    
        public AspectJAfterAdvice(Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
            super(aspectJBeforeAdviceMethod, pointcut, aif);
        }
    
        public Object invoke(MethodInvocation mi) throws Throwable {
            try {
                return mi.proceed();
            }
            finally {
                // 激活增強方法
                invokeAdviceMethod(getJoinPointMatch(), null, null);
            }
        }
    }

    2. 尋找匹配的增強器

      前面已經把所有增強器的獲取分析完了,但是對於所有增強來說,並不一定都適用於當前的Bean,還要挑選出適合的增強器,也就是滿足我們配置的通配符的增強器。這部分的具體實現在findAdvisorsThatCanApply中。

    protected List<Advisor> findAdvisorsThatCanApply(
            List<Advisor> candidateAdvisors, Class beanClass, String beanName) {
    
        ProxyCreationContext.setCurrentProxiedBeanName(beanName);
        try {
            // 過濾已經得到的advisors
            return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
        }
        finally {
            ProxyCreationContext.setCurrentProxiedBeanName(null);
        }
    }
    
    public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
        if (candidateAdvisors.isEmpty()) {
            return candidateAdvisors;
        }
        List<Advisor> eligibleAdvisors = new LinkedList<Advisor>();
        for (Advisor candidate : candidateAdvisors) {
            if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
                eligibleAdvisors.add(candidate);
            }
        }
        boolean hasIntroductions = !eligibleAdvisors.isEmpty();
        for (Advisor candidate : candidateAdvisors) {
            if (candidate instanceof IntroductionAdvisor) {
                // already processed
                continue;
            }
            if (canApply(candidate, clazz, hasIntroductions)) {
                eligibleAdvisors.add(candidate);
            }
        }
        return eligibleAdvisors;
    }

      findAdvisorsThatCanApply函數的主要功能是尋找所有增強器中適用於當前class的增強器,而對於真正的匹配其實是在canApply中實現的。

    public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
        if (advisor instanceof IntroductionAdvisor) {
            return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
        }
        else if (advisor instanceof PointcutAdvisor) {
            PointcutAdvisor pca = (PointcutAdvisor) advisor;
            return canApply(pca.getPointcut(), targetClass, hasIntroductions);
        }
        else {
            // It doesn't have a pointcut so we assume it applies.
            return true;
        }
    }
    
    public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
        Assert.notNull(pc, "Pointcut must not be null");
        if (!pc.getClassFilter().matches(targetClass)) {
            return false;
        }
    
        MethodMatcher methodMatcher = pc.getMethodMatcher();
        IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
        if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
            introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
        }
    
        Set<Class> classes = new LinkedHashSet<Class>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
        classes.add(targetClass);
        for (Class<?> clazz : classes) {
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                if ((introductionAwareMethodMatcher != null &&
                        introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||
                        methodMatcher.matches(method, targetClass)) {
                    return true;
                }
            }
        }
    
        return false;
    }

      這裏的邏輯也不是很複雜,無非就是根據advisor中封裝的classFilter來判斷是否match對應的類。

     3. 總結

      本文主要學習了Spring AOP核心實現原理中的對增強方法的獲取,主要包含兩個步驟:

    • 獲取所有增強;
    • 尋找所有增強中適用於bean的增強;

      下文會聚焦在AOP核心實現原理的另一部分–代理的創建。

    本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※自行創業缺乏曝光? 網頁設計幫您第一時間規劃公司的形象門面

    網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

    ※想知道最厲害的網頁設計公司"嚨底家"!

    ※別再煩惱如何寫文案,掌握八大原則!

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • C++ Primer Plus(一)

    完整閱讀C++ Primer Plus 

      系統重新學習C++語言部分,記錄重要但易被忽略的,關鍵但易被遺忘的。

     

    預備

      1、C++相對於C增加了最關鍵的兩項,面向對象和范型編程。

     

    處理數據

      2、對於變量明,C++沒有長度限制;同時,以兩個下劃線或一個下劃線和大寫字母開頭的名稱被保留給實現(編譯器及其使用的資源)使用;以一個下劃線開頭的名稱被保留給實現,用作全局標識符。

      3、C++11提供一種大括號初始化器,可以用它初始化任何類型。

    1 int ham = {24};
    2 int ems{7};
    3 int roc = {}; // 為0
    4 int rhs{};

      4、對於整型字面值,如果第一位為1~9則為十進制;如果第一位為0,第二位為1~7,則是八進制;如果前兩位為0x或0X,則為十六進制。如果希望使用cout輸出八進制或十六進制格式的整數,可以使用cout的控制符(這三個控制符都被包含在std命名空間)。

    1 cout << dec << a; // 10進制,默認的
    2 cout << oct << b; // 8進制
    3 cout << hex << c; // 16進制

      5、cout.put()、cin.get()的用法,可參照C語言中get()與put()的用法。

      6、C++有一種表示特殊字符的機制,他獨立於鍵盤,被稱作通用字符名。通用字符名以\u或\U開頭,前者後面是8個十六進制位,後者後面則是16個十六進制位。這些位表示的是ISO10646碼點。

      7、對於寬字符類型wcha_t,cin和cout無法很好的處理,此時應該使用wcinwcout

      8、C++11新增了char16_t和char32_t類型,C++11使用前綴u表示前者,U表示後者;並與形式為\u00F6和\U0000222B的通用字符名匹配。

    1 u'C' u“be good”   U'R' U”dirty rat”

      

    複合類型

      9、cin使用空白(空格、製表符、換行符)來確定字符串結束的位置,而cin.getline()可以依據換行符來讀取整行,並且可以制定最多讀取字符的數量。

      10、可以使用沒有名稱的結構類型,方法是省略名稱,同時定義一個結構類型和一個這種類型的變量,不常用,可用作臨時變量。

      11、C++允許對一個整數強制類型轉換為一個枚舉值,並參与賦值操作;同時可以有多個值相同的枚舉值,目前枚舉值也可以使用long,long long類型的值。對於較小的值編譯器會使用一個字節甚至更少的的字節,對於包含long類型的枚舉,會使用4個字節。

      12、在C中允許給指針直接賦字面值,但C++不允許,必須進行類型轉換。

     

    循環和關係表達式

      13、前綴遞增,前綴遞減,解除引用運算符的優先級相同,以從右往左的方式進行結合;後綴遞增,後綴遞減的優先級相同,但比前綴運算符的優先級高,以從左往右的方式結合。

      14、 cin.get(ch)和cin.get()的區別。

    屬性 cin.get(ch) cin.get()
    傳遞輸入字符的方式 賦值給參數ch 將函數返回值賦給ch
    用於字符輸入時函數的返回值  istream對象(執行bool轉換後為true)  int類型的字符編碼
    到達EOF時函數的返回值   istream對象(執行bool轉換後為false) EOF 

     

    函數——C++編程模塊

      15、如果數據類型本身並不是指針,則可以將const數據或者非const數據的地址賦給指向const的指針,但只能將非const數據的地址賦給非const指針

     

    函數探幽

      16、函數重載后,在調用函數時,如果沒有完全相同的參數類型,編譯器會做強制類型轉換進行匹配,但如果有多個可轉換的類型(也就是說有多個重載過的函數),C++將拒絕這種函數調用。

      17、函數重載的關鍵是函數的參數列表,也成為特徵標,以下兩個聲明互斥:

    1 long gronk(int n, float m);
    2 double gronk(int n, float m);

      C++不允許這種方式的重載,返回類型可以不同,但特徵標必須也不同。

      18、對於重載了引用參數的函數,C++將選擇調用最匹配的版本,這使得能夠根據參數是左值引用,const類型的左值引用還是右值引用來定製函數的行為。

      19、 函數模板並非函數定義,在使用模板時,編譯器會針對特定的類型生成函數實例,這種實例化方式被稱為隱式實例化

      20、C++允許顯式實例化,也就是說可以針對特定類型使用模板生成特定的實例。

    1 template void Swap<int>(int, int); // 用<>指定類型,在聲明前加template

      它的語義為“使用Swap()模板生成int類型的函數定義”。

      與顯式實例化不同的是,顯式具體化使用下面兩個等價的聲明之一:

    1 template<> void Swap<int>(int, int);
    2 template<> void Swap(int, int);

      它們的語義是,“不要使用Swap模板來生產函數定義,而應使用專門為int類型显示地定義的函數定義”。

      21、還可以在程序中創建顯式實例化:

    1 template <class T>
    2 T Add(T a, T b){ return a + b; }
    3 int m = 6;
    4 double n = 9.8;
    5 cout << Add<double>(m, n) << endl;

      由於這裏显示實例化中的特定類型為double,所以變量m會被強制類型轉換成double類型。

      22、對於函數重載函數模板函數模板重載,C++將選擇哪個版本?

      請看這裏———>   C++ 函數重載,函數模板和函數模板重載,選擇哪一個?

      23、C++11,在函數模板中,當無法得知一個變量的值時,可以使用decltype關鍵字來決定返回值類型:

    1 template<class T1, class T2>
    2 void ft(T1 x, T2 y)
    3 {
    4     decltype(x+y) xpy = x + y; // 此時xpy的類型就是x+y后的類型
    5 }

      24、decltype關鍵字本質上更複雜一些,編譯器必須遍歷一個核對錶去確定類型,現在有如下聲明:

    1 decltype(expression) var;

      第一,expression是一個沒有用括號括起來的標識符,則var的類型與該標識符相同,包括const等限定符。

      第二,如果expression是一個函數調用,則於與函數的返回值相同,這裏並不執行函數,只是查看返回值類型。

      第三,如果expression是一個左值,並且expression是被括號括起的,var會是引用類型,否則第一步就會處理。

      第四,到了這裏,var的類型只能與expression相同。

      25、C++11,在函數模板中,當無法得知返回值類型時,一般不可以直接使用關鍵字decltype來得到返回值類型,因為此時往往decltype後面表達式中的變量還不在作用域內,此時,需要使用後置返回類型

    1 template<class T1, class T2>
    2 auto gt(T1 x, T2 y) -> decltype(x+y) // 此時x,y已在作用域內
    3 {
    4     return x + y;
    5 }

      auto表示是一個佔位符,表示後置返回類型提供的類型。

    本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※別再煩惱如何寫文案,掌握八大原則!

    ※教你寫出一流的銷售文案?

    ※超省錢租車方案

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • 一篇文章搞懂filebeat(ELK)

    一篇文章搞懂filebeat(ELK)

    本文使用的filebeat是7.7.0的版本
    本文從如下幾個方面說明:

    • filebeat是什麼,可以用來幹嘛
    • filebeat的原理是怎樣的,怎麼構成的
    • filebeat應該怎麼玩

    一、filebeat是什麼

    1.1、filebeat和beats的關係

      首先filebeat是Beats中的一員。
      Beats在是一個輕量級日誌採集器,其實Beats家族有6個成員,早期的ELK架構中使用Logstash收集、解析日誌,但是Logstash對內存、cpu、io等資源消耗比較高。相比Logstash,Beats所佔系統的CPU和內存幾乎可以忽略不計。
    目前Beats包含六種工具:

    • Packetbeat:網絡數據(收集網絡流量數據)
    • Metricbeat:指標(收集系統、進程和文件系統級別的CPU和內存使用情況等數據)
    • Filebeat:日誌文件(收集文件數據)
    • Winlogbeat:windows事件日誌(收集Windows事件日誌數據)
    • Auditbeat:審計數據(收集審計日誌)
    • Heartbeat:運行時間監控(收集系統運行時的數據)

    1.2、filebeat是什麼

      Filebeat是用於轉發和集中日誌數據的輕量級傳送工具。Filebeat監視您指定的日誌文件或位置,收集日誌事件,並將它們轉發到Elasticsearch或 Logstash進行索引。

      Filebeat的工作方式如下:啟動Filebeat時,它將啟動一個或多個輸入,這些輸入將在為日誌數據指定的位置中查找。對於Filebeat所找到的每個日誌,Filebeat都會啟動收集器。每個收集器都讀取單個日誌以獲取新內容,並將新日誌數據發送到libbeat,libbeat將聚集事件,並將聚集的數據發送到為Filebeat配置的輸出。

           工作的流程圖如下:

     

    1.3、filebeat和logstash的關係

      因為logstash是jvm跑的,資源消耗比較大,所以後來作者又用golang寫了一個功能較少但是資源消耗也小的輕量級的logstash-forwarder。不過作者只是一個人,加入http://elastic.co公司以後,因為es公司本身還收購了另一個開源項目packetbeat,而這個項目專門就是用golang的,有整個團隊,所以es公司乾脆把logstash-forwarder的開發工作也合併到同一個golang團隊來搞,於是新的項目就叫filebeat了。

     

    二、filebeat原理是什麼

    2.1、filebeat的構成

      filebeat結構:由兩個組件構成,分別是inputs(輸入)和harvesters(收集器),這些組件一起工作來跟蹤文件並將事件數據發送到您指定的輸出,harvester負責讀取單個文件的內容。harvester逐行讀取每個文件,並將內容發送到輸出。為每個文件啟動一個harvester。harvester負責打開和關閉文件,這意味着文件描述符在harvester運行時保持打開狀態。如果在收集文件時刪除或重命名文件,Filebeat將繼續讀取該文件。這樣做的副作用是,磁盤上的空間一直保留到harvester關閉。默認情況下,Filebeat保持文件打開,直到達到close_inactive

    關閉harvester可以會產生的結果:

    • 文件處理程序關閉,如果harvester仍在讀取文件時被刪除,則釋放底層資源。
    • 只有在scan_frequency結束之後,才會再次啟動文件的收集。
    • 如果該文件在harvester關閉時被移動或刪除,該文件的收集將不會繼續

      一個input負責管理harvesters和尋找所有來源讀取。如果input類型是log,則input將查找驅動器上與定義的路徑匹配的所有文件,併為每個文件啟動一個harvester。每個input在它自己的Go進程中運行,Filebeat當前支持多種輸入類型。每個輸入類型可以定義多次。日誌輸入檢查每個文件,以查看是否需要啟動harvester、是否已經在運行harvester或是否可以忽略該文件

    2.2、filebeat如何保存文件的狀態

      Filebeat保留每個文件的狀態,並經常將狀態刷新到磁盤中的註冊表文件中。該狀態用於記住harvester讀取的最後一個偏移量,並確保發送所有日誌行。如果無法訪問輸出(如Elasticsearch或Logstash),Filebeat將跟蹤最後發送的行,並在輸出再次可用時繼續讀取文件。當Filebeat運行時,每個輸入的狀態信息也保存在內存中。當Filebeat重新啟動時,來自註冊表文件的數據用於重建狀態,Filebeat在最後一個已知位置繼續每個harvester。對於每個輸入,Filebeat都會保留它找到的每個文件的狀態。由於文件可以重命名或移動,文件名和路徑不足以標識文件。對於每個文件,Filebeat存儲唯一的標識符,以檢測文件是否以前被捕獲。

    2.3、filebeat何如保證至少一次數據消費

      Filebeat保證事件將至少傳遞到配置的輸出一次,並且不會丟失數據。是因為它將每個事件的傳遞狀態存儲在註冊表文件中。在已定義的輸出被阻止且未確認所有事件的情況下,Filebeat將繼續嘗試發送事件,直到輸出確認已接收到事件為止。如果Filebeat在發送事件的過程中關閉,它不會等待輸出確認所有事件后再關閉。當Filebeat重新啟動時,將再次將Filebeat關閉前未確認的所有事件發送到輸出。這樣可以確保每個事件至少發送一次,但最終可能會有重複的事件發送到輸出。通過設置shutdown_timeout選項,可以將Filebeat配置為在關機前等待特定時間

    三、filebeat怎麼玩

    3.1、壓縮包方式安裝

    本文採用壓縮包的方式安裝,linux版本,filebeat-7.7.0-linux-x86_64.tar.gz

    curl-L-Ohttps://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.7.0-linux-x86_64.tar.gz
    tar -xzvf filebeat-7.7.0-linux-x86_64.tar.gz

    配置示例文件:filebeat.reference.yml(包含所有未過時的配置項)
    配置文件:filebeat.yml

    3.2、基本命令

    詳情見官網:https://www.elastic.co/guide/en/beats/filebeat/current/command-line-options.html

    export   #導出
    run      #執行(默認執行)
    test     #測試配置
    keystore #秘鑰存儲
    modules  #模塊配置管理
    setup    #設置初始環境

    例如:./filebeat test config  #用來測試配置文件是否正確

    3.3、輸入輸出

    支持的輸入組件:

    Multilinemessages,Azureeventhub,CloudFoundry,Container,Docker,GooglePub/Sub,HTTPJSON,Kafka,Log,MQTT,NetFlow,Office365ManagementActivityAPI,Redis,s3,Stdin,Syslog,TCP,UDP(最常用的額就是log)

    支持的輸出組件:

    Elasticsearch,Logstash,Kafka,Redis,File,Console,ElasticCloud,Changetheoutputcodec(最常用的就是Elasticsearch,Logstash)

    3.4、keystore的使用

    keystore主要是防止敏感信息被泄露,比如密碼等,像ES的密碼,這裏可以生成一個key為ES_PWD,值為es的password的一個對應關係,在使用es的密碼的時候就可以使用${ES_PWD}使用

    創建一個存儲密碼的keystore:filebeat keystore create
    然後往其中添加鍵值對,例如:filebeatk eystore add ES_PWD
    使用覆蓋原來鍵的值:filebeat key store add ES_PWD–force
    刪除鍵值對:filebeat key store remove ES_PWD
    查看已有的鍵值對:filebeat key store list

    例如:後期就可以通過${ES_PWD}使用其值,例如:

    output.elasticsearch.password:”${ES_PWD}”

    3.5、filebeat.yml配置(log輸入類型為例)

    詳情見官網:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-input-log.html

    type: log #input類型為log
    enable: true #表示是該log類型配置生效
    paths:     #指定要監控的日誌,目前按照Go語言的glob函數處理。沒有對配置目錄做遞歸處理,比如配置的如果是:
    - /var/log/* /*.log  #則只會去/var/log目錄的所有子目錄中尋找以".log"結尾的文件,而不會尋找/var/log目錄下以".log"結尾的文件。
    recursive_glob.enabled: #啟用全局遞歸模式,例如/foo/**包括/foo, /foo/*, /foo/*/* encoding:#指定被監控的文件的編碼類型,使用plain和utf-8都是可以處理中文日誌的
    exclude_lines: ['^DBG'] #不包含匹配正則的行
    include_lines: ['^ERR', '^WARN']  #包含匹配正則的行
    harvester_buffer_size: 16384 #每個harvester在獲取文件時使用的緩衝區的字節大小
    max_bytes: 10485760 #單個日誌消息可以擁有的最大字節數。max_bytes之後的所有字節都被丟棄而不發送。默認值為10MB (10485760)
    exclude_files: ['\.gz$']  #用於匹配希望Filebeat忽略的文件的正則表達式列表
    ingore_older: 0 #默認為0,表示禁用,可以配置2h,2m等,注意ignore_older必須大於close_inactive的值.表示忽略超過設置值未更新的
    文件或者文件從來沒有被harvester收集
    close_* #close_ *配置選項用於在特定標準或時間之後關閉harvester。 關閉harvester意味着關閉文件處理程序。 如果在harvester關閉
    後文件被更新,則在scan_frequency過後,文件將被重新拾取。 但是,如果在harvester關閉時移動或刪除文件,Filebeat將無法再次接收文件
    ,並且harvester未讀取的任何數據都將丟失。
    close_inactive  #啟動選項時,如果在制定時間沒有被讀取,將關閉文件句柄
    讀取的最後一條日誌定義為下一次讀取的起始點,而不是基於文件的修改時間
    如果關閉的文件發生變化,一個新的harverster將在scan_frequency運行后被啟動
    建議至少設置一個大於讀取日誌頻率的值,配置多個prospector來實現針對不同更新速度的日誌文件
    使用內部時間戳機制,來反映記錄日誌的讀取,每次讀取到最後一行日誌時開始倒計時使用2h 5m 來表示
    close_rename #當選項啟動,如果文件被重命名和移動,filebeat關閉文件的處理讀取
    close_removed #當選項啟動,文件被刪除時,filebeat關閉文件的處理讀取這個選項啟動后,必須啟動clean_removed
    close_eof #適合只寫一次日誌的文件,然後filebeat關閉文件的處理讀取
    close_timeout #當選項啟動時,filebeat會給每個harvester設置預定義時間,不管這個文件是否被讀取,達到設定時間后,將被關閉
    close_timeout 不能等於ignore_older,會導致文件更新時,不會被讀取如果output一直沒有輸出日誌事件,這個timeout是不會被啟動的,
    至少要要有一個事件發送,然後haverter將被關閉
    設置0 表示不啟動
    clean_inactived #從註冊表文件中刪除先前收穫的文件的狀態
    設置必須大於ignore_older+scan_frequency,以確保在文件仍在收集時沒有刪除任何狀態
    配置選項有助於減小註冊表文件的大小,特別是如果每天都生成大量的新文件
    此配置選項也可用於防止在Linux上重用inode的Filebeat問題
    clean_removed #啟動選項后,如果文件在磁盤上找不到,將從註冊表中清除filebeat
    如果關閉close removed 必須關閉clean removed
    scan_frequency #prospector檢查指定用於收穫的路徑中的新文件的頻率,默認10s
    tail_files:#如果設置為true,Filebeat從文件尾開始監控文件新增內容,把新增的每一行文件作為一個事件依次發送,
    而不是從文件開始處重新發送所有內容。
    symlinks:#符號鏈接選項允許Filebeat除常規文件外,可以收集符號鏈接。收集符號鏈接時,即使報告了符號鏈接的路徑,
    Filebeat也會打開並讀取原始文件。
    backoff: #backoff選項指定Filebeat如何积極地抓取新文件進行更新。默認1s,backoff選項定義Filebeat在達到EOF之後
    再次檢查文件之間等待的時間。
    max_backoff: #在達到EOF之後再次檢查文件之前Filebeat等待的最長時間
    backoff_factor: #指定backoff嘗試等待時間幾次,默認是2
    harvester_limit:#harvester_limit選項限制一個prospector并行啟動的harvester數量,直接影響文件打開數
    
    tags #列表中添加標籤,用過過濾,例如:tags: ["json"]
    fields #可選字段,選擇額外的字段進行輸出可以是標量值,元組,字典等嵌套類型
    默認在sub-dictionary位置
    filebeat.inputs:
    fields:
    app_id: query_engine_12
    fields_under_root #如果值為ture,那麼fields存儲在輸出文檔的頂級位置
    
    multiline.pattern #必須匹配的regexp模式
    multiline.negate #定義上面的模式匹配條件的動作是 否定的,默認是false
    假如模式匹配條件'^b',默認是false模式,表示講按照模式匹配進行匹配 將不是以b開頭的日誌行進行合併
    如果是true,表示將不以b開頭的日誌行進行合併
    multiline.match # 指定Filebeat如何將匹配行組合成事件,在之前或者之後,取決於上面所指定的negate
    multiline.max_lines #可以組合成一個事件的最大行數,超過將丟棄,默認500
    multiline.timeout #定義超時時間,如果開始一個新的事件在超時時間內沒有發現匹配,也將發送日誌,默認是5s
    max_procs #設置可以同時執行的最大CPU數。默認值為系統中可用的邏輯CPU的數量。
    name #為該filebeat指定名字,默認為主機的hostname
     

    3.6、實例一:logstash作為輸出

    filebeat.yml配置

    #=========================== Filebeat inputs =============================
    
    filebeat.inputs:
    
    # Each - is an input. Most options can be set at the input level, so
    # you can use different inputs for various configurations.
    # Below are the input specific configurations.
    
    - type: log
    
      # Change to true to enable this input configuration.
      enabled: true
    
      # Paths that should be crawled and fetched. Glob based paths.
      paths:  #配置多個日誌路徑
        - /var/logs/es_aaa_index_search_slowlog.log
        - /var/logs/es_bbb_index_search_slowlog.log
        - /var/logs/es_ccc_index_search_slowlog.log
        - /var/logs/es_ddd_index_search_slowlog.log
        #- c:\programdata\elasticsearch\logs\*
    
      # Exclude lines. A list of regular expressions to match. It drops the lines that are
      # matching any regular expression from the list.
      #exclude_lines: ['^DBG']
    
      # Include lines. A list of regular expressions to match. It exports the lines that are
      # matching any regular expression from the list.
      #include_lines: ['^ERR', '^WARN']
    
      # Exclude files. A list of regular expressions to match. Filebeat drops the files that
      # are matching any regular expression from the list. By default, no files are dropped.
      #exclude_files: ['.gz$']
    
      # Optional additional fields. These fields can be freely picked
      # to add additional information to the crawled log files for filtering
      #fields:
      #  level: debug
      #  review: 1
    
      ### Multiline options
    
      # Multiline can be used for log messages spanning multiple lines. This is common
      # for Java Stack Traces or C-Line Continuation
    
      # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
      #multiline.pattern: ^\[
    
      # Defines if the pattern set under pattern should be negated or not. Default is false.
      #multiline.negate: false
    
      # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
      # that was (not) matched before or after or as long as a pattern is not matched based on negate.
      # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
      #multiline.match: after
    
    
    #================================ Outputs =====================================
    
    #----------------------------- Logstash output --------------------------------
    output.logstash:
      # The Logstash hosts #配多個logstash使用負載均衡機制
      hosts: ["192.168.110.130:5044","192.168.110.131:5044","192.168.110.132:5044","192.168.110.133:5044"]  
      loadbalance: true  #使用了負載均衡
    
      # Optional SSL. By default is off.
      # List of root certificates for HTTPS server verifications
      #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]
    
      # Certificate for SSL client authentication
      #ssl.certificate: "/etc/pki/client/cert.pem"
    
      # Client Certificate Key
      #ssl.key: "/etc/pki/client/cert.key"

    ./filebeat -e   #啟動filebeat

    logstash的配置

    input {
      beats {
        port => 5044   
      }
    }
    
    output {
      elasticsearch {
        hosts => ["http://192.168.110.130:9200"] #這裏可以配置多個
        index => "query-%{yyyyMMdd}" 
      }
    }

     

    3.7、實例二:elasticsearch作為輸出

    filebeat.yml的配置:

    ###################### Filebeat Configuration Example #########################
    
    # This file is an example configuration file highlighting only the most common
    # options. The filebeat.reference.yml file from the same directory contains all the
    # supported options with more comments. You can use it as a reference.
    #
    # You can find the full configuration reference here:
    # https://www.elastic.co/guide/en/beats/filebeat/index.html
    
    # For more available modules and options, please see the filebeat.reference.yml sample
    # configuration file.
    
    #=========================== Filebeat inputs =============================
    
    filebeat.inputs:
    
    # Each - is an input. Most options can be set at the input level, so
    # you can use different inputs for various configurations.
    # Below are the input specific configurations.
    
    - type: log
    
      # Change to true to enable this input configuration.
      enabled: true
    
      # Paths that should be crawled and fetched. Glob based paths.
      paths:
        - /var/logs/es_aaa_index_search_slowlog.log
        - /var/logs/es_bbb_index_search_slowlog.log
        - /var/logs/es_ccc_index_search_slowlog.log
        - /var/logs/es_dddd_index_search_slowlog.log
        #- c:\programdata\elasticsearch\logs\*
    
      # Exclude lines. A list of regular expressions to match. It drops the lines that are
      # matching any regular expression from the list.
      #exclude_lines: ['^DBG']
    
      # Include lines. A list of regular expressions to match. It exports the lines that are
      # matching any regular expression from the list.
      #include_lines: ['^ERR', '^WARN']
    
      # Exclude files. A list of regular expressions to match. Filebeat drops the files that
      # are matching any regular expression from the list. By default, no files are dropped.
      #exclude_files: ['.gz$']
    
      # Optional additional fields. These fields can be freely picked
      # to add additional information to the crawled log files for filtering
      #fields:
      #  level: debug
      #  review: 1
    
      ### Multiline options
    
      # Multiline can be used for log messages spanning multiple lines. This is common
      # for Java Stack Traces or C-Line Continuation
    
      # The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
      #multiline.pattern: ^\[
    
      # Defines if the pattern set under pattern should be negated or not. Default is false.
      #multiline.negate: false
    
      # Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
      # that was (not) matched before or after or as long as a pattern is not matched based on negate.
      # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
      #multiline.match: after
    
    
    #============================= Filebeat modules ===============================
    
    filebeat.config.modules:
      # Glob pattern for configuration loading
      path: ${path.config}/modules.d/*.yml
    
      # Set to true to enable config reloading
      reload.enabled: false
    
      # Period on which files under path should be checked for changes
      #reload.period: 10s
    
    #==================== Elasticsearch template setting ==========================
    
    
    #================================ General =====================================
    
    # The name of the shipper that publishes the network data. It can be used to group
    # all the transactions sent by a single shipper in the web interface.
    name: filebeat222
    
    # The tags of the shipper are included in their own field with each
    # transaction published.
    #tags: ["service-X", "web-tier"]
    
    # Optional fields that you can specify to add additional information to the
    # output.
    #fields:
    #  env: staging
    
    #cloud.auth:
    
    #================================ Outputs =====================================
    
    
    #-------------------------- Elasticsearch output ------------------------------
    output.elasticsearch:
      # Array of hosts to connect to.
      hosts: ["192.168.110.130:9200","92.168.110.131:9200"]
    
      # Protocol - either `http` (default) or `https`.
      #protocol: "https"
    
      # Authentication credentials - either API key or username/password.
      #api_key: "id:api_key"
      username: "elastic"
      password: "${ES_PWD}"   #通過keystore設置密碼

    ./filebeat -e   #啟動filebeat

    查看elasticsearch集群,有一個默認的索引名字filebeat-%{[beat.version]}-%{+yyyy.MM.dd}

     

     

     

    3.8、filebeat模塊

    官網:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html

    這裏我使用elasticsearch模式來解析es的慢日誌查詢,操作步驟如下,其他的模塊操作也一樣:

    前提: 安裝好Elasticsearch和kibana兩個軟件,然後使用filebeat

    具體的操作官網有:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules-quickstart.html

    第一步,配置filebeat.yml文件

    #============================== Kibana =====================================
    
    # Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
    # This requires a Kibana endpoint configuration.
    setup.kibana:
    
      # Kibana Host
      # Scheme and port can be left out and will be set to the default (http and 5601)
      # In case you specify and additional path, the scheme is required: http://localhost:5601/path
      # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
      host: "192.168.110.130:5601"  #指定kibana
      username: "elastic"   #用戶
      password: "${ES_PWD}"  #密碼,這裏使用了keystore,防止明文密碼
    
      # Kibana Space ID
      # ID of the Kibana Space into which the dashboards should be loaded. By default,
      # the Default Space will be used.
      #space.id:
    
    #================================ Outputs =====================================
    
    # Configure what output to use when sending the data collected by the beat.
    
    #-------------------------- Elasticsearch output ------------------------------
    output.elasticsearch:
      # Array of hosts to connect to.
      hosts: ["192.168.110.130:9200","192.168.110.131:9200"]
    
      # Protocol - either `http` (default) or `https`.
      #protocol: "https"
    
      # Authentication credentials - either API key or username/password.
      #api_key: "id:api_key"
      username: "elastic"  #es的用戶
      password: "${ES_PWD}" # es的密碼
      #這裏不能指定index,因為我沒有配置模板,會自動生成一個名為filebeat-%{[beat.version]}-%{+yyyy.MM.dd}的索引

    第二步:配置elasticsearch的慢日誌路徑

    cd filebeat-7.7.0-linux-x86_64/modules.d
    

    vim  elasticsearch.yml

     

     

     

    第三步:生效es模塊

    ./filebeat modules elasticsearch

    查看生效的模塊

    ./filebeat modules list

     

     

     

    第四步:初始化環境

    ./filebeat setup -e

     

     

     

     

     第五步:啟動filebeat

    ./filebeat -e

    查看elasticsearch集群,如下圖所示,把慢日誌查詢的日誌都自動解析出來了:

     

     到這裏,elasticsearch這個module就實驗成功了

     

    參考

    官網:https://www.elastic.co/guide/en/beats/filebeat/current/index.html

     

    本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※別再煩惱如何寫文案,掌握八大原則!

    網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

    ※超省錢租車方案

    ※教你寫出一流的銷售文案?

    網頁設計最專業,超強功能平台可客製化

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • MySQL 性能優化之慢查詢

    MySQL 性能優化之慢查詢

    性能優化的思路

    1. 首先需要使用慢查詢功能,去獲取所有查詢時間比較長的SQL語句
    2. 其次使用explain命令去查詢由問題的SQL的執行計劃(腦補鏈接:點我直達1,點我直達2)
    3. 最後可以使用show profile[s] 查看由問題的SQL的性能使用情況
    4. 優化SQL語句

    介紹

      數據庫查詢快慢是影響項目性能的一大因素,對於數據庫,我們除了要優化SQL,更重要的是得先找到需要優化的SQL語句

      MySQL數據庫有一個“慢查詢日誌”功能,用來記錄查詢時間超過某個設定值的SQL,這將極大程度幫助我們快速定位到問題所在,以便對症下藥

    至於查詢時間的多少才算慢,每個項目、業務都有不同的要求。
        比如傳統企業的軟件允許查詢時間高於某個值,但是把這個標準方在互聯網項目或者訪問量大的網站上,估計就是一個Bug,甚至可能升級為一個功能缺陷。

      MySQL的慢查詢日誌功能,默認是關閉的,需要手動開啟

    開啟慢查詢功能

    查看是否開啟慢查詢功能

     

     

    參數說明:

    • slow_query_log:是否開啟慢查詢,on為開啟,off為關閉;
    • log-slow-queries:舊版(5.6以下版本)MySQL數據庫慢查詢存儲路徑,可以不設置該參數,系統則會給一個缺省的文件:host_name-slow.log
    • long_query_time:慢查詢閥值,當查詢時間多於設置的閥值時,記錄日誌,單位為秒。

    臨時開啟滿查詢功能

      在MySQL執行SQL語句設置,但是如果重啟MySQL的話會失效。

    set global slow_query_log=on;
    set global long_query_time=1;

    永久性開啟慢查詢

      修改:/etc/my.cnf,添加以下內容,然後重啟MySQL服務

    [mysqld]
    lower_case_table_names=1
    slow_query_log=ON
    slow_query_log_file=/usr/local/mysql/data/chenyanbindeMacBook-Pro-slow.log
    long_query_time=1

     

    查看滿查詢啟動狀態

    演示慢查詢

      為了演示方便,我們讓sql睡眠3秒!

    格式說明:

    • 第一行,SQL查詢執行的具體時間
    • 第二行,執行SQL查詢的連接信息,用戶和連接IP
    • 第三行,記錄了一些我們比較有用的信息,
      • Query_timme,這條SQL執行的時間,越長則越慢
      • Lock_time,在MySQL服務器階段(不是在存儲引擎階段)等待表鎖時間
      • Rows_sent,查詢返回的行數
      • Rows_examined,查詢檢查的行數,越長就越浪費時間
    • 第四行,設置時間戳,沒有實際意義,只是和第一行對應執行時間。
    • 第五行,執行的SQL語句記錄信息

    分析滿查詢日誌

    MySQL自帶的mysqldumpslow

     

     

     

    參數說明:

    • -s, 是表示按照何種方式排序,c、t、l、r分別是按照記錄次數、時間、查詢時間、返回的記錄數來排序,ac、at、al、ar,表示相應的倒敘;
    • -t, 是top n的意思,即為返回前面多少條的數據;
    • -g, 後邊可以寫一個正則匹配模式,大小寫不敏感的;

    MySQL性能fenix語句show profile(重要

    介紹

    • Query Profiler是MySQL自帶的一種query診斷分析工具,通過它可以分析出一條SQL語句性能瓶頸在什麼地方。
    • 通常使用explain,以及slow query log都無法做到精確分析,但是Query profiler卻可以定位出一條SQL執行的各種資源消耗情況,比如CPU、IO等,以及該SQL執行所耗費的時間等。不過該工具只有在MySQL5.0.37以上版本中才有實現
    • 默認的情況下,MySQL的該功能沒有打開,需要自己手動打開

    語句使用

    • show profileshow profiles語句可以展示當前會話(退出session后,profiling重置為0)中執行語句的資源使用情況。
    • show profiles:以列表形式显示最近發送到服務器上執行的語句的資源使用情況,显示的記錄數由變量:profiling_history_size控制,默認15條
    • show profile:只是最近一條語句執行的消息資源佔用信息,默認實現Status和Duration兩列

    開啟Profile功能

    • Profile功能由MySQL會話變量:profiling控制,默認是OFF關閉狀態。
    • 查看是否開啟了Profile功能
    select @@profiling;
    
    show variables like '%profil%';

     

    打開profiling功能

    set profiling=1;

     

    show profile用法

    SHOW PROFILE [type [, type] …… ] [FOR QUERY n] [LIMIT row_count [OFFSET offset]]
    
    type: { ALL | BLOCK IO | CONTEXT SWITCHES | CPU | IPC | MEMORY | PAGE FAULTS | SOURCE | SWAPS }

    本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※教你寫出一流的銷售文案?

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※回頭車貨運收費標準

    ※別再煩惱如何寫文案,掌握八大原則!

    ※超省錢租車方案

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • 【Spring】BeanDefinition&PostProcessor不了解一下嗎?

    水稻:這两天看了BeanDefinition和BeanFactoryPostProcessor還有BeanPostProcessor的源碼。要不要了解一下

    菜瓜:six six six,大佬請講

    水稻:上次我們說SpringIOC容器是一個典型的工廠模式

    • 假如我們把Spring比作一個生產模型的大工廠,那麼.class文件就是原材料。而BeanDefinition就是創建模型的模具。不管是傳統的XML還是後面的註解,Spring在啟動的時候都會創建一個掃描器去掃描指定目錄下的.class文件,並根據文件的註解,實現的接口以及成員變量將其封裝一個個的BeanDefinition。
      • 比較重要的屬性有id,class,構造函數封裝類,屬性封裝類,factoryMethod等
    • 在對象初始化之前Spring會完成BeanDefinition對象的解析並將其裝入List容器beanDefinitionNames中,然後開始遍歷該容器並根據BeanDefinition創建對象

    菜瓜:sodasinei,BeanDefinition我了解了。它是創建bean的模板,類似於java創建對象依賴的class一樣。那還有兩個很長的單詞是啥呢?

    水稻:忽略掉後面老長的後綴,我們看BeanFactory和Bean是不是很親切。PostProcessor被翻譯成後置處理器,暫且我們把它看成是處理器就行

    • BeanFactory是bean工廠,它可以獲取並修改BeanDefinition的屬性,進而影響後面創建的對象。
    • Bean就是Spring的對象,這些個處理器才是真正處理bean對象的各個環節的工序,包括屬性,註解,方法

    菜瓜:有了模糊的概念,不明覺厲

    水稻:來,看demo

    package com.vip.qc.postprocessor;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.MutablePropertyValues;
    import org.springframework.beans.factory.config.BeanDefinition;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * 獲取初始化好的BeanFactory,此時還未進行bean的實例化
     *
     * @author QuCheng on 2020/6/14.
     */
    @Component
    public class BeanFactoryPostProcessorT implements BeanFactoryPostProcessor {
    
        public static final String BEAN_NAME = "processorT";
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            BeanDefinition initializingBeanT = beanFactory.getBeanDefinition(BEAN_NAME);
            MutablePropertyValues propertyValues = initializingBeanT.getPropertyValues();
            String pName = "a";
            System.out.println("BeanFactoryPostProcessor a " + propertyValues.getPropertyValue(pName) + " -> 1");
            propertyValues.addPropertyValue(pName, "1");
        }
    }
    
    
    package com.vip.qc.postprocessor;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.stereotype.Component;
    
    /**
     * @author QuCheng on 2020/6/14.
     */
    @Component
    public class BeanPostProcessorT implements BeanPostProcessor {
    
        public static final String beanNameT = "processorT";
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if (beanNameT.equals(beanName)) {
                ProcessorT processorT = ((ProcessorT) bean);
                System.out.println("BeanPostProcessor BeforeInitialization  a:" + processorT.getA() + "-> 3");
                processorT.setA("3");
            }
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (beanNameT.equals(beanName)){
                ProcessorT processorT = ((ProcessorT) bean);
                System.out.println("BeanPostProcessor AfterInitialization  a:" + processorT.getA() + "-> 4");
                processorT.setA("4");
            }
            return bean;
        }
    
    }
    
    
    package com.vip.qc.postprocessor;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @author QuCheng on 2020/6/14.
     */
    @Component
    public class ProcessorT {
    
        public ProcessorT() {
            System.out.println("ProcessorT 無參構造 a:" + a + "-> 2" );
            a = "2";
        }
    
        private String a;
    
        public String getA() {
            return a;
        }
    
        public void setA(String a) {
            this.a = a;
        }
    
        @Override
        public String toString() {
            return "ProcessorT{" +
                    "a='" + a + '\'' +
                    '}';
        }
    }
    
    // 測試類
    @Test
    public void test() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.vip.qc.postprocessor");
        ProcessorT processorT = (ProcessorT) context.getBean("processorT");
        System.out.println(processorT);
    }
    
    // 結果
    BeanFactoryPostProcessor a null -> 1
    ProcessorT 無參構造 a:null-> 2
    BeanPostProcessor BeforeInitialization a:1-> 3
    BeanPostProcessor AfterInitialization a:3-> 4
    ProcessorT{a='4'}
    • BeanFactoryPostProcessor在對象還未初始化前可以拿到對象的BeanDefinition對其設置屬性值
    • 過程中我們分別對屬性a設置了1,2,3,4的值。最後我們拿到的值為4

    菜瓜:好像看懂了。BeanFactoryPostProcessor可以拿到BeanFactory對象,獲取裏面所有的BeanDefinition並可對其進行干預。BeanPostProcessor其實是在bean已經被創建完成之後進行加工操作

    水稻:沒錯。這是我們自己進行干預的demo。限於篇幅有限,你可以去看一下Spring自己對於這兩個接口的實現源碼。比較重要的推薦下面幾個

    • ConfigurationClassPostProcessor 實現BeanFactoryPostProcessor子接口
      • 完成對@Configuration、@Component、@ComponentScan、@Bean、@Import、@ImportSource註解的搜集和解析
      • @Bean註解會被封裝成所在Bean的BeanDefinition中的factoryMethod屬性中,單獨進行實例化
    • CommonAnnotationBeanPostProcessor 實現 BeanPostProcessor
      • 完成@PostConstruct@PreDestroy@Resource註解的搜集和解析工作
      • @PostConstruct會在對象初始化且屬性渲染完成後進行
      • @Resource註解(參照下面)
    • AutowiredAnnotationBeanPostProcessor 實現 BeanPostProcessor
      • 完成@Autowired@Value註解的搜集和解析工作
      • 在對象初始化完成之後會先進行註解的搜集,然後進行屬性渲染調用populateBean方法,使用策略模式調用實現接口對註解進行解析,有@Autowired和@Value註解會調用getBean方法發起對依賴屬性的注入
    • AbstractAutoProxyCreator的入口類也是實現的BeanPostProcessor

    菜瓜:你放心,我不會看的。這麼複雜的東西,聽着都費勁

    水稻:不愧是你!有機會聊bean的生命周期的時候咱們還會說到這些東西。到時候再刷一遍

     

    總結:

    • BeanDefinition是spring容器創建對象的模板,定義了bean創建的細節
    • BeanFactoryPostProcessor可以拿到整個容器對象,當然也能修改BeanDefinition,所以能直接操作bean的創建
    • BeanPostProcessor執行的時候bean已經創建完成了,我們可以拿到想要的對象進行干預和設值等操作

    本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※超省錢租車方案

    ※別再煩惱如何寫文案,掌握八大原則!

    ※回頭車貨運收費標準

    ※教你寫出一流的銷售文案?

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

  • 要成為合格的女司機,最大的挑戰是什麼?

    要成為合格的女司機,最大的挑戰是什麼?

    可是,要完成這一次挑戰,我必須要有一台車。這次和我一起挑戰的,是全新昂科威28T頂配車型,2。0T發動機搭配9AT變速箱,擁有260匹馬力,還配備了CDC主動懸挂。2。0T發動機符合我對動力跟經濟性的需求。相比以前,全新的9AT變速箱也足夠聰明,足以應付多種路況。

    大家好,我是小喬,說真的,我有點害怕。

    作為一個女司機,我覺得我的駕駛技術算挺好了,但為什麼,我的知名程度,遠遠不如那個長得跟我差不多的Jacky呢?

    到底是“7200干它”成就了他,還是他成就了“7200干它”,這個問題,在我腦海中迴旋,如同先有雞還是先有蛋的生物史拷問,令我疑惑不已。終於,我決定卧薪嘗膽,直接跟他當面對質爆紅的真諦!

    Jacky面對我的質疑,嘴角微微上揚,說到:“既然你誠心誠意想要爆紅,那我就大發慈悲地成全你, 別克SUV強者挑戰之旅,這個對精神以及體力都有極大考驗的活動,就決定讓你參加了!“

    挑戰內容

    挑戰一:體能挑戰:限時挑戰3公里高空棧道。優秀的司機必須擁有強壯的體能!

    挑戰二:耐力測試,獨自跑完4個小時高速全程,鍛煉作為司機的集中力和耐久力。

    挑戰三:膽量挑戰:攀登懸崖天梯,鍛煉司機膽大心細的危急情況處理能力。

    挑戰四:險中求勝:考驗司機越野路面的駕駛能力。

    手拿這份挑戰清單,我感到一絲恐懼,但誓要成為女車神的我,不會輕易認輸!

    可是,要完成這一次挑戰,我必須要有一台車。這次和我一起挑戰的,是全新昂科威28T頂配車型,2.0T發動機搭配9AT變速箱,擁有260匹馬力,還配備了CDC主動懸挂。

    2.0T發動機符合我對動力跟經濟性的需求;相比以前,全新的9AT變速箱也足夠聰明,足以應付多種路況;它的懸架還能主動變化,公路、越野我都能舒舒服服。這一次挑戰,我勢在必得。

    想知道到底Jacky給小喬的挑戰有多可怕?讓小喬這個沒心沒肺的女人都感到驚慌,而她又能否順利完成任務?趕緊點開視頻看一看!

    本站聲明:網站內容來源於http://www.auto6s.com/,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※別再煩惱如何寫文案,掌握八大原則!

    ※教你寫出一流的銷售文案?

    ※超省錢租車方案

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • 大尺寸+高配置 眾泰T800 13.98萬起售

    大尺寸+高配置 眾泰T800 13.98萬起售

    4英寸中控屏第一時間抓住你的眼球,全車的功能基本都集成在裏面,旋鈕式換擋,全液晶儀錶,大面積軟性材料的包裹,檔次感還是營造的不錯,雖然說中控UI界面布局和反應速度還有提升的空間,但結合其價格來看,也能讓你產生“要啥自行車”的感慨了。

    眾泰T800在5月8日晚正式上市,新車共推出五款車型,售價13.98-18.58萬元。T800作為眾泰首款七座中型SUV,其配置同尺寸都達到了品牌的新高度。動力總成為2.0T+6DCT,從某種角度上T800可視作T700的加長版本。

    眾泰T800在外觀設計上可以說擁有相當高的原創度,前臉進氣格柵的面積非常大,採用星空點陣的設計元素,下部格柵採用了中國風的回紋設計,凸顯了整個正臉的設計感。頭燈部分採用了全LED光源,轉向燈則單獨設計在下部,同樣使用了LED,只有最下面的霧燈為鹵素光源,配置上十分厚道。

    T800的整車尺寸為4910x1933x1735mm,軸距2850mm,在目前自主中型七座SUV中算比較大的,側門上用裝飾模擬了出風口的造型,並且還點綴有眾泰漢語拼音首字母“Z”字形的標誌,同時不忘加入一個裝飾燈,這樣的小心思在其它品牌上不多見。還了解到,T800的頂配車型搭載了電吸門,在這個級別中是獨一份的那個。

    進入車內,10.4英寸中控屏第一時間抓住你的眼球,全車的功能基本都集成在裏面,旋鈕式換擋,全液晶儀錶,大面積軟性材料的包裹,檔次感還是營造的不錯,雖然說中控UI界面布局和反應速度還有提升的空間,但結合其價格來看,也能讓你產生“要啥自行車”的感慨了。

    談到眾泰,怎能不提配置,T800的配置水平用“極限”形容毫不過分,全景天窗,360°影像這些只是基本操作,什麼電動腳踏,電吸門,氛圍燈,車聯網,前窗加熱······配置上堆的滿滿噹噹,重要的是,除了入門車型以外,很多配置都實現了全系覆蓋,可以說,T800隻要不買最低配,都可以達到越級體驗。

    T800的2.0T動力,參數上達到了170kW/350N·m,在自主眾多中型七座中屬於動力靠前的那個,匹配六速雙離合變速箱,或許表現上沒有眾多愛信6AT那麼出色,但想想車內那麼豐富的配置,也能理解。前麥弗遜+后多連桿懸架是這個級別的標配,沒有四驅車型有些遺憾。但考慮其“城市SUV”的身份,也不會是什麼大問題。

    對於T800,我們先拋開眾泰眾多的“歷史遺留問題”去看待它。設計有原創,配置高上天,價格有驚喜——這三點已經是很大的亮點。自主品牌中型七座SUV已經很多,但T800還是能找到獨特的競爭力。能不能賣爆款不好說,但還是能吸引部分消費者的眼球,至少我們能看到的,T800已經是眾泰拿出最大的誠意去做的產品。品牌和質量都是需要長時間積累的事情,眾泰想要像其他自主品牌一樣深入民心,還是有很長的路要走。本站聲明:網站內容來源於http://www.auto6s.com/,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※別再煩惱如何寫文案,掌握八大原則!

    網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

    ※超省錢租車方案

    ※教你寫出一流的銷售文案?

    網頁設計最專業,超強功能平台可客製化

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!

  • 7.99萬起,今天上市的大空間6座車真是家用好選擇!

    7.99萬起,今天上市的大空間6座車真是家用好選擇!

    尾部在擾流板、貫穿尾燈的鍍鉻裝飾、黑色塑料擋板的組合下,形成了一種層次感。敦厚的車尾部,更是讓它充滿安全感。仿木材料的中控台,還有一個大尺寸豎直中控屏幕,以及是手感極佳的平底式運動方向盤,它在簡約風格中加入一點運動的元素。

    “一輛車究竟是多少座位最好?”,除了自己,還有配偶,加上父母以及自己的孩子,或許還是雙胞胎,這樣看來起碼是6座車型,另外一般而言緊湊型轎車後排乘坐3成年人是比較難受的事情。

    正是為了滿足用戶這種需求,比亞迪宋MAX前期推出了7座車型,如今更是推出了6座車型,官方指導價為:7.99-12.99萬。那麼它表現如何呢?

    6座,就是它最大的不同

    6座版本的比亞迪宋max依然有着4680*1810*1680mm的車身尺寸,以及是能夠帶來充足空間表現的2785mm軸距。並且在座椅配置上一樣保留了7座車型的優點,諸如第三排寬大保護作用大的頭枕、座椅採用打孔真皮材質。

    它採用了2+2+2的6座布局,獨立第二排座椅可以帶來比7座車型更舒適的體驗,加上獨立的可調節座椅扶手,座椅包裹性更佳。中部通道寬度為190mm,乘客進出第三排更為優雅。

    在實際的乘坐它表現讓人滿意,前排以及第二排座椅包裹性極佳,並且頂配車型上前排座椅還具備主副駕座椅通風加熱、第二排預警限力安全帶等配置。唯獨是第三排由於車身結構問題坐墊與地板距離較近,腿部支撐較弱,中長途旅行中更適合乘坐小孩子或者是身材嬌小的成年人。

    設計以及配置,和7座車型無異

    6座版本的宋MAX在設計上和7座車型保持了一致,犀利大燈帶有的鍍鉻裝飾條以及LED日間行車燈和大尺寸進氣格柵橫向裝飾條連成一體,拉寬了視覺寬度。這個“大嘴”配上這兇狠犀利的大燈,就形成了它’Dragon Face”設計的基本元素。

    4680mm車身長度的它在逐漸下滑的懸浮式車頂襯托下,顯得車身相當修長,低矮的腰線則降低了它的視覺重心,讓它整體有着一種動感、衝勁。

    尾部在擾流板、貫穿尾燈的鍍鉻裝飾、黑色塑料擋板的組合下,形成了一種層次感。敦厚的車尾部,更是讓它充滿安全感。

    仿木材料的中控台,還有一個大尺寸豎直中控屏幕,以及是手感極佳的平底式運動方向盤,它在簡約風格中加入一點運動的元素。

    生產實力強大、自產自銷的比亞迪能夠有效控製成本,所以它在配置配備上相當慷慨,標配了ESp車身穩定系統、17英寸鋁合金輪轂、自動空調、遠程啟動等等。又或者是一些“跨級般”的配置,諸如選配的LED自動大燈。

    操控,或許可以說沉穩自信?

    比亞迪宋MAX全系車型採用前麥弗遜后扭力梁懸架,處於這個級別的主流水平。動力方面搭載了最大輸出功率154馬力的1.5T直噴發動機,匹配的是6MT或者6速雙離合變速箱。

    在日常使用中它整體表現出同價位中較高的水準,應付城市使用這套渦輪增壓+雙離合的動力總成顯得游刃有餘,能夠有效地控制轉速在1500rpm之中,需要提速的時候154馬力的底蘊讓超車動作比較從容,而且它1750rpm就開始進入240牛米的最大輸出扭矩區間,相比競爭對手們最大扭矩輸出更早。只是低速在1、2擋切換時會有絕大多數雙離合變速箱的小不足——輕微頓挫感。

    據比亞迪官方稱,比亞迪宋max有着前奔馳底盤調校工程師漢斯柯克參与調校,讓它更注重舒適性,所以宋max駕駛起來有着厚重的行駛質感。能夠過濾掉路面各種細碎振動的同時,還有讓人安心的高速穩定性。受制於全系搭配的液壓轉向助力,宋max轉向略有虛位,並不可以說是精準,幸好較強的懸架支撐帶來了較小的車身傾側,扳回了一城。

    總的來說,比亞迪宋max相比同價位對手有着不少的優勢,諸如更佳的底盤表現、更豐富的配置以及更出色的外觀,這恰恰迎合如今消費者開始接受這些“體面”MpV的用戶趨勢。6座車型,則是滿足了消費者不同的乘坐需求,多一種選擇未嘗不是一種好事。本站聲明:網站內容來源於http://www.auto6s.com/,如有侵權,請聯繫我們,我們將及時處理

    【其他文章推薦】

    ※教你寫出一流的銷售文案?

    ※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

    ※回頭車貨運收費標準

    ※別再煩惱如何寫文案,掌握八大原則!

    ※超省錢租車方案

    ※產品缺大量曝光嗎?你需要的是一流包裝設計!