lombok


Lombok

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.


Table of contents

Install

在idea上使用

Install Lombok

注意:如果編譯出現錯誤Error:java: Lombok annotation handler class lombok.javac.handlers.HandleData failed on...,檢查你的版本,確保不是官網教程示例的版本0.9.2,替換成1.18.0,或者是RELEASE

  1. 新建一個person類設置一些屬性,加入lombok的jar后,我們在person類上加入@Data注解,現在只需要了解它可以為類屬性生成get,set方法,發現有提示而且並沒有報錯,也就是至少jar成功被依賴了,但是在單元測試中,發現new出person對象無法調用get,set等方法,因為lombok雖然會在編譯時將你的注解轉化為實際對應的代碼執行,但是在寫代碼的時候,idea並不知道@Data可以帶來什么,所以需要加入插件

  2. 需要安裝idea的lombok插件(需要梯子)idea -> file ->setting -> plugin ->browser repos 輸入lombok 選擇lombok 然后 install
    安裝完成后restart idea

  3. idea 重啟后需要配置注解處理器
    同樣我們在Settings設置頁面,我們點擊Build,Execution,Deployment-->選擇Compiler-->選中Annotation Processors,然后在右側勾選Enable annotation processing即可,完成后,需要再次重啟idea,不要和上一步的重啟合並成一步

  4. 這個時候發現@Data注解在菜單欄點擊View-->Tool Windows-->Structure,便可以看到類中所有的方法了這些都是lombok幫我自動生成的。
    person類的方法實現了

也可以調用了

使用

  • @Slf4j的使用

    需要額外依賴


    org.apache.logging.log4j
    log4j-slf4j-impl
    2.11.0

    如果沒有配置log4j2配置文件

    執行到log.error()的時候,會提示

    ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2

    大意就是需要配置文件,不然會啟用默認的配置

    一般是要將日志輸出到日志文件中的

    在resoures下新建文集log4j2.xml

    內容如下

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Configuration status="WARN">
    
    <properties>
        <property name="LOG_HOME">log</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{H:m:s.S} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    
        <!--臨時日志生成-->
        <!--<File name="log" fileName="log/test.log" append="true">
            <PatternLayout pattern="%d{H:m:s.S} [%t] %-5level %logger{36} - %msg%n"/>
        </File>-->
    
        <RollingRandomAccessFile name="log" fileName="${LOG_HOME}/test.log"
                                 filePattern="${LOG_HOME}/test-%d{yyyy-MM-dd}.log">
            <!--輸出到文件夾中去-->
            <PatternLayout pattern="%d{y-M-d H:m:s.S} [%t] %-5level %logger{36} - %msg%n" />
            <!--<HTMLLayout pattern="%d{y-M-d H:m:s.S} [%t] %-5level %logger{36} - %msg%n" />-->
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="log"/>
        </Root>
    </Loggers>
    </Configuration>
    
  • @Data的使用

@Data的使用

新建people類
為了測試data官方文檔中說明的
get,toString,equal對於靜態屬性與非靜態屬性的區分
set,constructor對於final非final的區分

注解說明

Data

官方文檔:
Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
翻譯過來就是加入@Data會為類的所有非靜態屬性生成get方法,以及對應非靜態屬性的toString()方法,也會為類的所有沒有final修飾的屬性生成set方法和對應的構造器G


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM