Java Web Technology Experience Summary

  1. Interface authorization authentication, use interceptor (HandlerInterceptorAdapter), refer to: Chapter 5 Detailed Explanation of Handler Interceptor-Follow Kai Tao to learn SpringMVC. Note: It is recommended to use Filter to implement the functions that can be implemented by the filter in the servlet specification, because HandlerInteceptor can only be used in the Spring Web MVC environment, so Filter is the most common and should be used first

  2. The meaning of "@Autowired" is: <T> Map<String, T> getBeansOfType(Class<T> var1) throws BeansException; defined by the ListableBeanFactory interface , that is, get all instances of type T and store them in a Map: key collection is the package name of these instances, and the value set is the instances themselves.

  3. The interface layer should achieve the following goals (reference: SpringMVC4.1 Controller Layer Best Practices):

  • Unified response body and request body, avoiding the way Map and List are used as parameters or response results (especially when parameters are wrapped in Map, this kind of code sometimes looks really frustrating)

  • Unified error message

  • Unified request data verification

  • Unified interface exception capture

  1. When building a table in MySQL, you can consider it in the following order

  • table name, and remarks

  • Field definitions, and remarks, pay attention to defining fields in the following order !

    • id, primary key, auto increment

    • Defined in order according to the frequency of use of the fields (why is the order of the fields important:);

    • For nullable fields, provide default values ;

    • Modification time (mtime)

    • Creation time (ctime)

  • Indexes and constraints (strong recommendation: how to deal with concurrency (1): about data indexing)

    • Unique key (for a table with a unique constraint, the insert ... on duplicate statement should be used when inserting data, refer to the insert on duplicate syntax)

    • normal index

    • composite index

  1. Using the @Async annotation, you can limit all methods of a class or a method for asynchronous processing , which belongs to the encapsulation of thread pool technology. Refer to the official document: Annotation Support for Scheduling and Asynchronous Execution. In addition, the CSDN blog post is also good: Spring asynchronous task processing, configuration and use of @Async.

  • The implementation class corresponding to the <task:executor/> tag is ThreadpoolExecutor

    • pool-size (core-size and max-size): When an asynchronous request arrives, if the current number of threads is less than core-size, a new thread will be started to provide services; if the current number of threads reaches core-size, the new request will be Put it into the Blockqueue; if the BlockQueue is also full, start a new thread to provide services until the number of threads in the thread pool reaches max-size;

    • queue-capacity: the size of the BlockQueue

    • keep-alive: The part of the thread that exceeds the core-size, the time that the thread survives in the thread pool after the task is completed;

    • rejection-policy: After the threads in the thread pool exceed max-size, the processing policy for new service requests (ignoring, throwing exceptions, or changing to synchronous calls).

  • The size of the thread pool: It can be estimated according to Little's law (the average number of tasks in the queue is equal to the product of the entry rate and the average residence time).

  • It is necessary to verify that @Async does work through log printing. There may be three pits: (1) the internal call cannot be asynchronous; (2) the scanning cannot be repeated, to ensure that the class where the method annotated with @Async is located is only scanned once by Component-Scan; (3) Must be public, not public static. The reference articles are as follows:

    • Spring's two task scheduling Scheduled and Async

    • Spring Async not working

  1. The xxxAware interface in Spring is a common mode in the spring framework, and is often used to assign values ​​to spring-managed beans through the setXxx methods provided by such interfaces during the spring startup phase. The Aware interface feels a bit like a listener, callback function, or observer pattern. During the startup process, Spring will check whether each bean implements certain xxxAware interfaces. If it finds one, it will call the corresponding method to provide the bean with corresponding information. See stackoverflow answer: BeanNameAware and BeanFactoryAware. Common xxxAware interfaces are: BeanNameAware, BeanClassLoaderAware, and BeanFactoryAware.

  2. What are the aspects of software development at work? Agile development is not savage development. The most critical feature is small steps and fast running. As a developer, you still need to consider the following aspects:

  • Requirements review, output requirements document

  • Design review, output design document

  • Code development (try to use best practices, pay attention to writing basic unit tests)

    • Basic function development

    • logging

    • exception handling

    • Entry check

    • return result generation

    • Asynchronous task processing

  • Code review: own review => senior developer review; core code needs team review

  • test

    • Functional testing (normal case, abnormal case)

    • Performance test (stress test)

    • Stability test

  • release

    • Risk assessment (scope of impact, consequences)

    • Rollback plan (emergency plan, service downgrade, etc.)

    • Grayscale release (traffic grayscale, user grayscale, regional grayscale, etc.)

  1. You cannot use a new instance in spring, otherwise you cannot preset behaviors during mockito testing; to use other components in spring, you must use xml or annotations, and the spring container is responsible for assembly.

  2. For unit testing, the Mockito framework is recommended for three reasons: (1) it can focus on the object under test; (2) it does not need to establish complex bean dependencies; (3) the DSL descriptive language is simple and easy to understand. Reference article: Anti-pattern classic - Mockito design analysis In project development, it is recommended to use Mockito and Assertj to write unit tests.

  3. To implement timed tasks, you can use the Quartz framework, refer to: Spring scheduling tool Quartz cron expression format

 

Author: Du Qi
Link: https://www.jianshu.com/p/86e915d616c4
Source : Jianshu

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292199&siteId=291194637