Java interview wrong question set 1

Notes on SpringBoot:

1.@SpringBootApplication:
This annotation is used to declare springboot to perform some necessary configuration for the program, equivalent to
@Configuration: declare that this is a configuration file, equivalent to an xml configuration file
@EnableAutoConfiguration: automatic configuration function,
@ComponentScan component scanning can Automatically discover and assemble some
integrated versions of the above three annotations for some beans .

2.@ResponseBody: Indicates that the result returned by this method is directly written into the responsebody, which is generally used when obtaining data asynchronously, and is used to build a restful-style api.
After using @RequesMapping, the return value is usually parsed as a jump path. After adding @ResponseBody, it will not be parsed as a jump path and will be directly written into the http response body.
For example, to obtain json data asynchronously, after adding @responsebody, the json data will be returned directly. This annotation is generally used in conjunction with @RequestMapping.

3.@Controller: Used to define the control class. In the spring project, the controller is responsible for forwarding the url request passed by the user to the corresponding service interface, which is the service layer. Annotations are generally placed in the class of the controller layer and used in conjunction with @RequestMapping.

4.@RestController: Used to mark control layer components, such as actions. Equivalent to the integration of @Controller and @ResponseBody.

5.@RequestMapping: Provide routing information, responsible for the mapping of URLs to specific functions in the Controller.

6.@EnableAutoConfiguration: Spring Boot auto-configuration: automatically configure your Spring application according to the jar dependencies you add.
You can add @EnableAutoConfiguration or @SpringBootApplication annotations to a @Configuration class to select automatic configuration. If you find that specific auto-configuration classes that you don't want are applied, you can use the exclusion attribute of the @EnableAutoConfiguration annotation to disable them.

7.@ComponentScan: Indicates that this type of scanning component is automatically discovered. Personal understanding is equivalent to that if you scan annotated classes such as @Component, @Controller, @Service, @Repository, etc., and register them as Beans, you can automatically collect all Spring components, including @Configuration classes. We often use the @ComponentScan annotation to search for beans and import them in conjunction with the @Autowired annotation. Can automatically collect all Spring components, including @Configuration classes.

8.@ImportResource: used to load the xml configuration file. @Component, @Controller, @Service, and @Repository are
used to register classes as components.
@component has different annotations in the service layer, controller layer, and dao layer (same order as above) in web development according to the MVC three-tier architecture. But the effect is the same.

9. @Autowired and @Resource are used to realize automatic assembly of beans.
10. @PathVariable gets parameters

servlet life cycle

One, loading and instantiation

In the first stage, we know that the Servlet container is responsible for loading and instantiating the Servlet. When a Servlet container starts or detects that the container needs this Servlet to respond to the first request, it will create a Servlet instance. After the Servlet container is started, it must know where the required Servlet class is, so the Servlet container can load the Servlet class from the local file system, remote file system or other network services through the class loader. When it is successfully loaded , The container will create an instance of the Servlet.

Two, servlet initialization

In the second stage, after the Servlet is instantiated, the container will call the init() method to initialize the object. This is to allow the Servlet object to complete some initialization tasks before processing the client request, such as establishing a database connection and obtaining configuration information. For all Servlet instances, the init() method can only be called once. During initialization, the Servlet instance can use the ServletConfig object prepared for it by the container to obtain initialization parameter information from the configuration information of the Web application.

Three, client request processing

In the third stage, the Servlet container calls the service() method in the Servlet to process the request. It should be noted that the init() method must be executed successfully before the service() method is called. In the service() method, the Servlet instance obtains the relevant information and request information of the client through the ServletRequest object. After it processes the request, it will call the method of the ServletResponse object to set the response information. During the execution of the service() method, if an error occurs, the Servlet instance can throw ServletException or UnavailableException. If the UnavailableException indicates that the instance is permanently unavailable, the Servlet container will call the destroy() method of the instance. Any subsequent requests for this instance will receive an HTTP 404 response sent by the container. If the UnavailableException indicates that the instance is temporarily unavailable, any request to the instance during the temporarily unavailable time period will receive an HTTP 503 response sent by the container.

Four, servlet destruction

In the final stage, when the servlet container detects that the servlet instance should be removed from the service, the container will call the destroy() method of the instance so that the instance can release the resources it uses and save the data to the persistent storage device. Once the memory needs to be released or the container is closed, the container will call the destroy() method of the Servlet instance. After the destroy() method is called, the container will release the Servlet instance, and then this instance will be recycled by the Java garbage collector. If the Servlet is needed to process the request again, the Servlet container will create a new Servlet instance. During the entire life cycle of a Servlet, the creation of a Servlet instance and the init() and destroy() methods of the instance can only be carried out once. When the initialization is complete, the Servlet container will save the instance in memory, by calling its The service() method serves the received request.

flow chart:
Insert picture description here

SpringMVC process

1. The user sends a request to the front controller DispatcherServlet
2. The front controller calls the mapping processor MappingHandler after receiving the request
3. The mapping processor finds specific processor information according to the xml configuration or annotations and returns it to the front controller
4. Front controller Call the adapter HandlerAdapter
5. The adapter finds the specific processor controller to execute and returns the result to the front controller
6. After the front controller receives the result, it passes the result to the view parser (the result of 6 is some models and views)
7. Analysis After passing the data to the front controller
8. The front controller responds to the user

JDBC connection database steps

1. Register and load the database driver :
Class.forName("com.mysql.jdbc.Driver"); After successful loading, an instance of the Driver class will be registered in the DriverManager class.

2. Get database connection :
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root";
Connection con = DriverManager.getConnection(url, username, password) ;

3. Create a Statement statement object :
To execute a SQL statement, you must obtain an instance of java.sql.Statement. Statement instances are divided into the following three types:
1. Execute static SQL statements. Usually achieved through Statement instance.
2. Execute dynamic SQL statements. Usually implemented through PreparedStatement instances.
3. Execute database stored procedures. Usually implemented through CallableStatement instances.
• Specific implementation:
Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement(sql);
CallableStatement cstmt = con.prepareCall("{CALL demoSp(?, ?)}");

4. Write and execute SQL statements
. The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate and execute
1. ResultSet executeQuery(String sqlString): executes SQL statements that query the database and returns a ResultSet object.
2. int executeUpdate(String sqlString): used to execute INSERT, UPDATE or DELETE statements and SQL DDL statements, such as: CREATE TABLE and DROP TABLE, etc.
3. execute(sqlString): used to execute multiple result sets and multiple updates Statements that count or a combination of the two.

5. Processing the result set
There are two situations:
1. The number of records affected by this operation is returned when the update is performed.
2. The result of executing the query is a ResultSet object.
• ResultSet contains all rows that meet the conditions in the SQL statement, and it provides access to the data in these rows through a set of get methods.
• Use the access method of the ResultSet object to get data:

6. Close the database connection :
• After the operation is completed, all used JDBC objects must be closed to release JDBC resources. The closing sequence is the opposite of the declaration sequence:
1. First close the requestSet
2. Then close the preparedStatement
3. Finally close the connection object connection

The json data passed from the front-end to the back-end, what kind of annotation is received by the back-end interface

@Pathvarible("id"): used to receive the parameters passed directly after the url (xxx.com/id=1)
@RequestParam("id"): used to receive the parameters passed as parameters (xxx.com?id=1) )

What is the difference between wait() and sleep()

1. The wait() method is that when the thread executes the wait() method, it will release the current lock, then give up the CPU and enter the waiting state. And you can call the notify() method or notifyAll() method to notify other threads that are waiting. The notify() method only wakes up one thread (the first thread in the waiting queue) and allows him to acquire the lock. The notifyAll() method wakes up all threads waiting for this object and allows them to compete for the lock.

2. The sleep() method is a static method of the Thread class. It is used by a thread to control its own process. It will suspend the execution of this thread for a period of time, and give the execution opportunity to other threads. When the timer expires, this thread will Automatically wake up.

The lock mechanism is different : both are methods to make the thread suspend execution. Since the main function of the sleep() method is to pause the execution of the thread for a period of time, it will automatically resume when the time is up, and does not involve communication between threads. Therefore, calling the sleep() method will not release the lock. The wait() method is different. When the wait() method is called, the thread will release the lock it occupied, so that other synchronized data in the object where the thread is located can be used by other threads.

The use area is different : the wait() method must be used in the synchronization control method and the synchronization code block, and the sleep() method can be used anywhere. The sleep() method must catch exceptions, while wait(), notify(), notifyAll() do not need to catch exceptions. In the process of sleep, other objects may call his interrupt(), resulting in InterruptedException. Since sleep does not release the lock flag, it is easy to cause deadlock problems. Therefore, in general, it is recommended to use the wait() method.

Execution order of code blocks

Static code block——>Non-static code block——>Constructor——>Ordinary code block The
static code block is executed only once with the loading of the class, and the non-static code block is executed once when the class is loaded. The
constructor is used in the class Used to define the state of initialization when the object is created

What happened between the browser input url and the browser response

After we enter the URL press Enter, DNS server resolves the current URL via ip this URL; to look after the IP, the browser will initiate a tcp connection request to the server, the request includes three-way handshake, as follows:
first Secondary handshake: When the link is established, the client browser will send a syn packet to the server, and enter the SYS_SENT state, waiting for the server's confirmation;

The second handshake: After the server receives the syn packet, it must confirm the syn of the client and send an ack packet between syn and ack. At this time, the server enters the SYN_RECV state. At this time, the server receives the client after it is passively opened. Syn and the status of sending ack;

The third handshake: After the client receives the syn+ack packet from the server, it sends an acknowledgement packet ack to the server. After the packet is sent, the client and server enter the ESTABLISHED (tcp connection successful) state, and the third handshake is completed.
When the three-way handshake is over, the client and server have established a connection. At this time, the tcp protocol is disconnected, and the default index.html page under the server is accessed, and the accessed resource file is called to display the corresponding content.

Guess you like

Origin blog.csdn.net/gsy_csdn1/article/details/114900349