Javaj basics and summary of Java work

5 years of experience and technical summary of java back-end
1. Introduction I have 
  graduated for more than 5 years. In these 5 years, I am especially grateful to the technical management staff for their respect and the help of colleagues. I have learned a lot. During the year, I went through some detours and encountered some problems. I also suffered from being a firefighter as a developer but often for system maintenance and release. So I decided to sort out what I learned and share it with everyone. 
  After a year I realized that there were many misunderstandings before, such as: I 
  prefer to collect, often collect all kinds of materials and videos to fill the hard drives, and then watch the capacity not act with satisfaction. 
  I don’t pay much attention to the basics. I always feel that there are many basic things that I don’t need to look at. In fact, there are many things I don’t understand. There must be a reason for any result in computer programs. Don’t just use ignorance of the principle, that is from the processing plant. Now ide view code is so convenient, ctrl+click to enter JDK to view implementation details. 
  It’s a long way to go. When the computer foundation is not solid, I always want to build architecture, make distributed, and engage in big data. 
  Do not pay attention to performance, only to realize the function, whether sql query can be optimized, whether there is a magical algorithm, whether large objects should be cleared. 
  No importance is attached to scalability, tight coupling between modules, common methods are not extracted into tool classes, and the calling relationship is chaotic. 
  …… 
  This article does not focus on these, so only a small part is listed, and I will enter the topic below.

2. Basic Grammar

 2.1 Java class initialization order
  This is the class initialization order  in all cases. If there is no definition in the actual class, skip it: parent class static variable-parent class static code block-child class static code block-parent class non-static variable- — Parent class non-static code block — parent class constructor — child class non-static variable — child class non-static code block — child class constructor 
 2.2 Value transfer and reference transfer 
  may be dismissive of many people, I think I have been working for a year, are you still unfamiliar with these? But this is not the case. Are everything in the JDK familiar? Starting with the simplest example, what do you think are the elements in fatherList after the code in the figure below is executed? 
 
  This is an example of the most basic value passing and reference passing. You think it’s so simple and you are already eager to challenge yourself. Then please see below. StringBuffer is easy to understand, but when you execute it again, you find out if it is what you expected Is the output different? Isn't String a reference type? How could this be? If you can't understand, then please look at the implementation source code of String to understand the implementation principle of its allocation in memory. 
 
 2.3 Use of collections 
  This part is used by almost everyone, and everyone is familiar with it. The picture below is from the Internet, for everyone to review. But using the characteristics of the collection to make clever combination applications can solve many complex problems of optimization. Set non-repeatability, List order, Map key-value pairs, SortSet/SortMap order, I use these cleverly in many complex businesses in my work. I will not post it when it comes to confidential company information. The code is out. The longer you work, the more ingenious you will find these sums. 
  
 2.3 Exception handling 
  1. It is very easy to look at try, catch, and finally. If combined with transaction propagation, it will become extremely complicated. 
  2.finally does not necessarily have to be executed, return handles the situation in catch/finally (it is recommended to try it yourself). 
  3. In catch, you can continue to throw custom exceptions (and pass the exceptions to the control layer step by step, use the aspect to capture the exceptions, and return them to the caller). 
 2.4 The idea of ​​object-oriented When 
  mentioning object-oriented, everyone knows abstraction, encapsulation, inheritance, and polymorphism. But how much do you know from actual work experience, let alone how to use estimates in a project. 
  Common opportunities each need to establish a base class. For example, each control layer method may need to obtain a login user id through security, which is used to operate different data according to different users. An application layer base class can be abstracted to achieve Get the protect method of id. In the same way, the DAO layer can use generics to extract a base class that contains additions, deletions, modifications, and queries. 
  Polymorphic Override: The reference variable of the base class can not only point to the instance object of the base class, but also point to the instance object of its subclass. If it points to the instance object of the subclass, the method called should be the method of the running object. . It is very common in strategy mode. 
  When it comes to object-oriented, it is inevitable to talk about design patterns. In work, a similar strategy pattern (a little more complicated) written by a technical master, very cleverly solves the same method of various businesses, and realizes orders I really admire the decoupling of work orders and business. I think a lot of interviews will ask about the singleton mode. If you haven't understood it yet, take a look.

3. Multithreading

 3.1 Thread safety 
  is a commonplace issue, but it is indeed a high-prone area of ​​problems and bugs. The thread synchronization problem does not need to be written separately, I must be clear to everyone, I suggest Baidu if you are not familiar. 
 3.1.1 Thread safety issues 
  1. If there is a synchronization operation in the code, special attention should be paid to shared variables (this can generally be realized) 
  2 multiple operations can modify the same data in the data table. (This is easy to be overlooked. Business A may operate table a, and business B may also operate table a. Even if services A and B are in different modules and methods, thread safety issues will be caused. For example, if one person accesses the business A interface, Another person accesses the business B interface, and each business request in the web is processed by a separate thread, and thread safety issues will arise). 
  3. Use of unsafe types, such as StringBuffer, StringBuild, HashTable, HashMap, etc. In my work, I have encountered someone doing list remove in a for loop. Although the compiler does not report an error and the program can run, the results are predictable. 
  4. Spring beans are singleton by default. If there are class variables, you must be very careful (in general, no one uses class variables in the control layer, business layer, DAO layer, etc.). If you use it, it is recommended to use the final type, for example Log, gson, etc.). 
  5. Multiple systems share the database. This is actually similar to the distributed system. The 
  user repeats the problem (even if there are restrictions on reading from the database in the code, the problem cannot be solved.) 
 3.1.2 Thread-safe solution 
  Use safe ones where synchronization is required Types of. 
  JDK lock mechanism, lock, tryLock, synchronized, wait, notify, notifyAll, etc. 
  Concurrent toolkit, in dealing with some problems, who knows who uses it. It is strongly recommended to view the source code! 
  The data table is locked. (Unless the access frequency of a certain table is extremely low, it is not recommended to use it) 
  Involving distributed, use middleware technology such as zookeeper to solve. 
 3.2 Asynchronous and 
  asynchronous usage scenarios do not affect the main thread, and slow response services. For example, IO operations, third-party services (SMS verification code, app push, cloud storage upload, etc.). 
  If there are many asynchronous tasks, you need to use task queues. Task queues can be implemented at the code level or redis (the advantage is too obvious). 
 3.3 There are many
  articles on multithreaded communication  , so I won't go into details here. 
  1. Shared variable mode (shared files, global variables, semaphore mechanism, etc.) 
  2. Message queue mode 
  3. Busy, etc., lock mechanism 
 3.4 Multi-threaded realization 
  1. Integrate Thread class, rewrite (here rewrite refers to override ) The run method calls the start method to execute. 
  2. Implement Runable interface, implement run method, create thread object with Runable instance. 
  3. Implement the Callable interface, implement the call method, FutureTask wraps the callable interface, FutureTask object creates thread object, common language asynchronous operation, it is recommended to use anonymous inner class, which is convenient to read and use. 
  The additional points to be explained are: 
  1. Understand the join method of thread; 
  2. Don't think that volitate is thread safe (if you don't understand the reason, it is recommended to look at the memory allocation strategy at jvm runtime); 
  3. After the sleep time slice is over, it is not guaranteed to get the cpu immediately. 
  4. ThreadLocal can maintain a copy of variables for each thread, and is often used to exchange space for time in multiple threads. 
  5. Java learning exchange QQ group: 589809992 Small chats are forbidden, please do not enter!

4. Open source framework

4.1 Hibernate and Mybatis 
  believe that every java programmer is familiar with these, so I will not go into details here. 
  The main points that need to be explained are as follows: 
  1. Hibernate first level cache (built-in session cache), second level cache (can be assembled with sessionFactory cache), second level cache will cause concurrency problems. 
  2. Understanding of hibernate lazy loading principle. 
  3. Hibernate's get, load methods, sava, persist, savaOrUpdate method difference 
  4. The session rebuilds the association relationship but does not synchronize and update with the database   
  5. Hibernate session association relationship: detached object, persistent object 
  6. Spring data integration, Annotation mode configures attributes and entities. 
  7. Mybatis plugin. 
  8. Paging query (database). 
  9. Connection pool technology 
4.2 Spring IOC 
  4.1.1 Spring bean 
    1. Bean injection annotation method is convenient and easy to read, referencing third parties (database connection, database connection pool, JedisPool, etc.) adopts the configuration file method. 
    2. Bean scope: Singleton, prototype, request, session, global session 
    3. Bean life cycle: as shown in the figure below (picture from the Internet): 
    

4.3 The
  basic concepts of Spring AOP  : focus, aspect, pointcut, joinpoint, advice, weave, and introduction. 
  Spring AOP supports 5 types of notifications, namely MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor, and IntroductionInterceptor (the name is too long). The 
  implementation methods are as follows: 
  1. Agent-based AOP 
  2. Aspects driven by @Aspect annotations. (Strongly recommended: good readability, easy maintenance, easy expansion, fast development) 
  3. Pure POJO aspect. 
  4. Injection aspect. 
4.4 Srping transaction 
 4.4.1 The
  concept of transaction propagation  : certain operations need to ensure atomicity. If there is an error in the middle, the transaction needs to be rolled back. If a transaction is rolled back, then the actions of the transaction in the method that calls the transaction are transaction propagation. 
  Transaction propagation properties: 
  1. PROPAGATION_REQUIRED-support the current transaction, if there is no current transaction, create a new transaction. This is the most common choice. 
  2. PROPAGATION_SUPPORTS-supports the current transaction, if there is no transaction currently, it will be executed in a non-transactional manner. 
  3. PROPAGATION_MANDATORY-support the current transaction, if there is no current transaction, throw an exception. 
  4. PROPAGATION_REQUIRES_NEW-create a new transaction, if there is a transaction currently, suspend the current transaction. 
  5. PROPAGATION_NOT_SUPPORTED-Perform the operation in a non-transactional manner. If there is a transaction currently, the current transaction is suspended. 
  6. PROPAGATION_NEVER-Execute in a non-transactional manner. If there is a transaction currently, an exception will be thrown. 
  Transaction isolation level: 
  1. ISOLATION_DEFAULT: This is the default isolation level of a PlatfromTransactionManager, using the database default transaction isolation level. The other four corresponding to the isolation level of JDBC 
  2. ISOLATION_READ_UNCOMMITTED: This is the lowest isolation level of the transaction, allowing it An outside transaction can see the uncommitted data of this transaction. This isolation level will produce dirty reads, non-repeatable reads and phantom reads. 
  3. ISOLATION_READ_COMMITTED: Ensure that the data modified by one transaction can be read by another transaction after it is committed. Another transaction cannot read the uncommitted data of the transaction 
  4. ISOLATION_REPEATABLE_READ: This transaction isolation level can prevent dirty reads and cannot be read repeatedly. However, phantom reading may occur. In addition to ensuring that one transaction cannot read the uncommitted data of another transaction, it also guarantees to avoid the following situations (non-repeatable read). 
  5. ISOLATION_SERIALIZABLE This is the most expensive but most reliable transaction isolation level. Transactions are processed as sequential execution. In addition to preventing dirty reads and non-repeatable reads, phantom reads are also avoided. 
4.5 Other Spring technology stack 
  spring boot lightweight startup framework 
  Spring security user authority management, implement UserDetailsService according to roles and users, and perform custom authority management. 
  Spring task code-level timing tasks, annotation methods, it is very convenient to use. It should be noted that if an exception occurs in a certain timed task without processing it, the subsequent timed task will become invalid. If each task is independent of each other, you can simply surround it with try and catch (I have suffered from this disadvantage before). 
  Spring
  MVC is a simple and clear MVC framework for defining entities and attributes in spring data annotations  . Value passing types such as url passing by value, array passing by value, object passing by value, object array, etc., upload/download file types require attention. 
  spring restful pay attention to the naming, very strict naming requirements. 
  It is very convenient to use spring shell command line to execute commands, fire fighting, import and export data, and make reports.  

5. Web Foundation

 5.1 web container startup 
  1. web.xml loading order: listener -> filter -> servlet 
  2. webt container startup process, java novices are very afraid of configuration files, understanding these will help to familiarize themselves with configuration files
 5.2 Servlet, Interceptor, Listener, Filter 
  Servlet receives the request and returns the response, the most primitive web service processing class. 
  Interceptor can implement custom interceptor of HandlerInterceptor interface, which can be used in scenarios such as logging, permission checking, performance monitoring, general behavior, etc. It is essentially AOP. 
  Listener is often used for vertical functions such as counting online people. 
  Filter The filter changes requset before requesting the interface to process the business, and changes the response before responding to the user after the business is processed. If some data is not encrypted, it is easy to cheat with a packet capture tool and filter. 
 5.3 web project structure 
  5.3.1 mvn structure 
  Familiar with several common mvn project structures, mvn can be automatically generated, so I won’t go into details here. 
  5.3.2 mvn package management 
  1. Try to have several versions in one file for easy management. 
  2. The spring milestone package solves the spring package conflict problem. 
  3. The mvn dependency: tree command analyzes all package dependencies, and encloses conflicts in the pom file 
  5.3.3 Version control 
  1. Git, svn, etc. 
  2. Code conflict resolution 
  3. Branch management. 
  After a stable version is online, if you develop new features on this basis, you must create a new branch, submit the code on the new branch, and finally merge the branch when the new version is released. Modify the operating environment bug. Switch to the main branch for modification. 
5.4 Http request 
  5.4.1 Request methods 
  post, get, put, head, delete, copy, move, connect, link, patch, the most commonly used are the first 4 or 5. 
  5.4.2 Request headers.
  Commonly used request headers for status codes  include Accept (special use when downloading files), Accept-Charset (set utf-8 character set), Content-Type (json and other configurations) and other 
  commonly used response headers include Content- Type, Content-Type, Content-Length, etc. are more front-end and will not be detailed.

6. System Architecture

  There are not too many contacts, and only server master-slave backup is currently used. Nginx reverse proxy configuration. 
  Multiple projects nginx configure 
  Spring Mvc to interact with json data and configure servlet for json conversion. 
  Encapsulate the return value to 
  customize the RunEnvironmentException (status code, reason), overwrite the original Exception, the aspect ExceptionHandler captures the Exception and encapsulates it in the return value (loose coupling between the front and back ends). 
  The headache of users repeats (continuous and fast clicks) submits the problem, the front end Limiting the symptoms but not the root cause; the back-end uses sessonid to implement it in the aspect, but also requires front-end storage, and adds sessionId to all request data. Finally, it is stored in jedis, and the interface name + user name is used as the key. The time can be set separately for different keys according to different interfaces. This not only ensures the repeated submission problem, but also avoids the problem of malicious requests. At the same time, the request interval can be customized. (At the beginning, I was worried that the redis cache read and write time delay would cause the restriction to become invalid. Later, I found that I was too concerned. For a general small system, after performance testing, it was found that even if the request frequency was increased by 100 times, the restriction would not become invalid.) 
  testNg unit test, Performance testing, coverage testing. 
  Aspect management date and authority. Caching etc.

7. Nosql

  1. Redis's java library Jedis. 
  Jedispool configuration. 
  The task queue and cache are used in the project. 
  2. The neo4j graph database 
  handles social networking and recommendation

8. Server

  Linux operating system is familiar with centos as an example: 
  common simple commands: ssh, vim, scp, ps, gerp, sed, awk, cat, tail, df, top, shell, chmod, sh, tar, find, wc, ln, | 
  Directory structure details: /etc/, ~/, /usr/, /dev/, /home/, /etc/init.d/ 
  Server: jdk, tomcat, nginx, mysql, jedis, neo4j startup and configuration (special instructions It’s the damn firewall. After nginx started, I couldn’t access it. I couldn’t find the reason for one afternoon, and finally found that it was a firewall problem.) 
  Monitor server status (cpu, disk, memory), locate pid, log view 
  nginx load balancing, reverse proxy , Configuration 
* *Automated deployment scripts are 
  simple shell script writing to avoid a lot of human labor. 
  In the monitoring system, the code throws fatal exceptions and automatically sends emails, and the system indicators continue to be high and automatically send emails. 
   
9. Database related 
  
10. Third-party interface docking

10.1 Payment interface There are many 
  WeChat payment pits, and it took nearly two weeks to complete the WeChat payment. Too many places need to be configured in the WeChat background. 
  The Alipay payment module took only 2 days to complete. 
10.2 The push interface 
  defines tags and aliases for users. Note that tags and aliases need to be updated synchronously when data is updated. If you don't use asynchronous implementation (user experience is a good card) 
10.3 Cloud storage and 
  upload a large number of files to the cloud (Qiniu Cloud), pay attention to creating a bucket 
10.4 A
  very simple third-party interface for SMS verification  , introduce dependencies, and call directly. You need to set up templates in the third-party background, and pay attention to limiting the number of user visits. 
10.5 Mail 
  is very simple, small functions, tools. 
Time is limited, so many technology stacks are written first. For code writing and algorithm skills, I will take time to share.

Guess you like

Origin blog.csdn.net/a159357445566/article/details/109250462