In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how Lombok quickly build JavaBean and log output, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Quickly build JavaBean and log output
When we write the base class JavaBean, when we define the member variables, we have to add constructors, setter/getter methods and toString methods. Although development tools such as eclipse have an one-click generation strategy, they are still tedious for us, and when we add or delete member variables to this class, we have to add or delete setter/getter methods artificially. These repetitive operations greatly stimulate programmers who are on the verge of development collapse, so lombok arises at the historic moment to help us solve the problem of "laziness".
The old JavaBean was like this.
Today's JavaBean is like this.
Preparation before using Lombok
Add maven dependency to ①, or download the corresponding jar package https://projectlombok.org/download from the official website.
Org.projectlombok lombok 1.16.16
② installs plug-ins for tools such as eclipse
Move lombok.jar to the installation directory of eclipse
Add the following line at the end of the eclipse.in file-javaagent:D:\ install\ jee-oxygen\ eclipse\ lombok.jar
Restart eclipse
Operation Guide:
Constructor, log and other annotations must be annotated on the class. When setter/getter and other methods are annotated on the class, they work on all member variables, and when annotated on member variables, only this variable works.
The specific role of the notes:
@ NoArgsConstructor / / empty parameter constructor @ AllArgsConstructor / / full parameter constructor @ Data / / set,get,toString and other methods @ Accessors (chain=true) / / chain style access, new Dept (). SetName ("cmj") .setDeptno (1000L). SetDb_source ("db01")
So the question is, what should we do when we need to operate in the getter method, such as when the age field is null, I need to return a number of 20?
In fact, lombok is still very powerful, when there is a conflict between annotations and getter and other methods, the annotation of this variable will lose its effect, and it will be our custom method.
@ Slf4j
Annotated on the class, which is equivalent to
Private final Logger logger = LoggerFactory.getLogger (XXX.class)
NonNull: can help us avoid null pointers.
Use lombok:
Import lombok.NonNull; public class NonNullExample extends Something {private String name; public NonNullExample (@ NonNull Person person) {super ("Hello"); this.name = person.getName ();}
Do not use lombok:
Public class NonNullExample extends Something {private String name; public NonNullExample (@ NonNull Person person) {super ("Hello"); if (person = = null) {throw new NullPointerException ("person");} this.name = person.getName ();}}
Cleanup: automatically calls the close () method for us.
Use @ Cleanup:
Import lombok.Cleanup;import java.io.*;public class CleanupExample {public static void main (String [] args) throws IOException {@ Cleanup InputStream in = new FileInputStream (args [0]); @ Cleanup OutputStream out = new FileOutputStream (args [1]); byte [] b = new byte [10000]; while (true) {int r = in.read (b); if (r =-1) break Out.write (b, 0, r);}
Do not use @ Cleanup:
Import java.io.*; public class CleanupExample {public static void main (String [] args) throws IOException {InputStream in = new FileInputStream (args [0]); try {OutputStream out = new FileOutputStream (args [1]); try {byte [] b = new byte [10000] While (true) {int r = in.read (b); if (r =-1) break; out.write (b, 0, r) }} finally {if (out! = null) {out.close ();} finally {if (in! = null) {in.close () } IDEA installs the lombok plug-in using the lombok output log
Pom.xml adds dependency org.projectlombok lombok true 1.16.18 using @ Slf4j annotation
Use the info () method of the log object
@ RestController@Slf4jpublic class PaymentController {@ Autowired private PaymentService paymentService; @ GetMapping (value = "/ payment/get/ {id}") public CommonResult getById (@ PathVariable ("id") long id) {Payment result = paymentService.getPaymentById (id); log.info ("query result:" + result); if (result successful null) {return new CommonResult (200, "query success", result) } else {return new CommonResult, "not found, ID:" + id,null);}
The above is all the content of the article "how to quickly build JavaBean and log output by Lombok". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.