Click for PDF manual V2.0 – > interview | springboot | springcloud | programming ideas
1. Define configuration file information
Sometimes we put some variables into the YML configuration file for unified management
For example,
The picture
Use @ConfigurationProperties instead of @Value
Method of use
Define the entities for the corresponding fields
@data // Specify the prefix @ConfigurationProperties(prefix = "developer") @Component public class DeveloperProperty {private String name; private String website; private String qq; private String phoneNumber; }Copy the code
@data // Specify the prefix @ConfigurationProperties(prefix = "developer") @Component public class DeveloperProperty {private String name; private String website; private String qq; private String phoneNumber; }Copy the code
Inject the bean when you use it
@RestController @RequiredArgsConstructor public class PropertyController { final DeveloperProperty developerProperty; @GetMapping("/property") public Object index() { return developerProperty.getName(); }}Copy the code
2. Replace @autowired with @requiredargsconstructor
We all know that there are three ways to inject a bean (set injection, constructor injection, and annotation injection), and Spring recommends that we inject the bean in the constructor way
Let’s look at what the code looks like after it’s compiled
The picture
RequiredArgsConstructor: Provided by Lombok
3. Code modularization
Alibaba Java development manual said that each method of code should not exceed 50 lines (I remember correctly), in the actual development we should be good at splitting their interface or method, so that a method only deal with a logic, maybe in the future a function will be used, use.
The picture
4. Throw exceptions instead of returning
When writing business code, different information is often returned according to different results. Minimize the return, which will appear messy code
counter-examples
The picture
Is case
The picture
5. Reduce unnecessary DB
Minimize database queries as much as possible
For example,
Delete a service (has been removed or not on the shelf can be deleted), before looking at the code written by others, we will first query the record according to the ID, and then make some judgments
counter-examples
The picture
Is case
The picture
6. Do not return NULL
counter-examples
The picture
Is case
The picture
Avoid unnecessary null Pointers when calling methods elsewhere
7. if else
Not too much if else if, you can try policy mode instead
Reduce the Controller business code
Business code as far as possible in the service layer for processing, later maintenance is easy to operate and beautiful
counter-examples
The picture
Is case
The picture
9. Use your Idea
Up to now, enterprises on the market have basically used IDEA as a development tool
Let me give you a little example
Idea will judge our code and make reasonable suggestions
Such as:
The picture
It recommends that we use the lanbda format instead, and click replace
The picture
Read the source code
GitHub stars:>1000 is a great open source project, and you can learn a lot from it.
11. Design patterns
23 design patterns, to try to use the idea of design patterns in the code, write code that is standard and beautiful but also lofty haha.
12. Embrace new knowledge
For programmers like us who have worked for a few years, I think we should learn more knowledge beyond our own cognition. Instead of cruD every day, we should use more difficult knowledge if we have the chance. If we don’t have the chance (the project is traditional), we can do more relevant demo exercises after work
13. Basic issues
The map traverse
HashMap<String, String> map = new HashMap<>(); map.put("name", "du"); for (String key : map.keySet()) { String value = map.get(key); } map.forEach((k, v) -> { }); For (map.entry <String, String> Entry: map.entryset ()) {}Copy the code
Optional sentence empty
Public List<CatalogueTreeNode> getChild(String pid) {if (v.ispty (pid)) {pid = BasicDic.TEMPORARY_DIRECTORY_ROOT; } CatalogueTreeNode node = treeNodeMap.get(pid); return Optional.ofNullable(node) .map(CatalogueTreeNode::getChild) .orElse(Collections.emptyList()); }Copy the code
recursive
When recursing with large amounts of data, avoid new objects in recursive methods and try passing objects as method parameters
annotation
Class interface method annotations more complex method annotations should be written and written clearly, sometimes write comments not for others but for yourself
14. Determine if the element exists
HashSet instead of list, the code that determines whether an element exists
ArrayList<String> list = new ArrayList<>(); For (int I = 0; i < list.size(); i++) if ("a".equals(elementData[i])) return i;Copy the code
It can be seen that its complexity is On, and the underlying hashSet uses hashMap as a data structure for storage, and all elements are put into the key of map (i.e. linked list).
HashSet<String> set = new HashSet<>(); Int index = hash(a); return getNode(index) ! = nullCopy the code
So the complexity is O1.
Source: blog.csdn.net/weixin_44912855/article/details/120866194
Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:
Build a complete project from Springboot+ ElasticSearch + Canal
- Video tutorial: www.bilibili.com/video/BV1Jq…
- A complete development documents: www.zhuawaba.com/post/124
- Online demos: www.zhuawaba.com/dailyhub
【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system
- Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
- Complete development document front end: www.zhuawaba.com/post/18
- Full development documentation backend: www.zhuawaba.com/post/19
- Online demos: www.markerhub.com/vueadmin
【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching
- Full 200 – minute video tutorial: www.bilibili.com/video/BV1PQ…
- Full development documentation: www.zhuawaba.com/post/17
- Online demos: www.markerhub.com:8084/blogs
If you have any questions, please come to my official account [Java Q&A Society] and ask me