Spring family barrel 100 questions, often meet questions summary, altogether divided into three upper middle and lower, this is the first chapter!
How can Spring Boot implement hot deployment?
Use DevTools to start hot deployment, add the DevTools library, and set spring.devtools.restart. enabled to true in the configuration file. Using the Intellij Idea editor, check auto compile or manual recompile. 109. What is the difference between JPA and Hibernate? Jpa stands for Java Persistence API, is the Java Persistence interface specification, hibernate belongs to the concrete implementation of JPA.
What is the Spring Boot core configuration file?
- The configuration files are in.properties and.yml formats, and the main difference is the calligraphy style.
- Properties: Spring.rabbitmq. port=5672
- The yML configuration is as follows: Spring: RabbitMQ: port: 5672
- The yML format does not support @propertysource annotation import.
Third, why use Spring Boot?
- Configuration is simple
- Independent operation
- Automatically.
- No code generation and XML configuration
- Providing Application Monitoring
- Easy to get started
- Improve development efficiency
What is Spring Boot?
Spring Boot is for Spring and is used to simplify the initial setup and development process of new Spring applications.
Five, what does @Autowired do?
@autowired can annotate class member variables, methods and constructors, complete the work of automatic assembly, through the use of @Autowired to eliminate set/ GET methods.
What does @requestMapping do?
Map HTTP requests to appropriate classes/methods.
What are the components of Spring MVC?
- Front controller DispatcherServlet.
- Mapping controller HandlerMapping.
- Processor Controller.
- ModelAndView ModelAndView.
- The ViewResolver ViewResolver.
What is the spring MVC workflow?
- Spring MVC first sends the request to the DispatcherServlet.
- The DispatcherServlet queries one or more HandlerMapping to find the Controller handling the request.
- The DispatcherServlet then submits the request to the corresponding Controller.
- After the Controller does its business logic, it returns a ModelAndView.
- Dispathcher queries one or more ViewResolver view parsers to find the view object specified by the ModelAndView object.
- The view object is responsible for rendering back to the client.
What about transaction isolation in Spring?
Spring has five isolation levels, the default being ISOLATION_DEFAULT (using database Settings), and the other four are consistent with database isolation levels:
ISOLATION_DEFAULT: Use the isolation level set by the underlying database, I use whatever the database is set to;
ISOLATIONREADUNCOMMITTED: The read is not committed, the lowest isolation level, before the transaction is committed, can be read by other transactions (magic read, dirty read, unrepeatable read);
ISOLATIONREADCOMMITTED: Committed reads. A transaction can only be read by other transactions after it is committed. Default level of SQL Server.
ISOLATIONREPEATABLEREAD: repeatable to ensure that the same data is read repeatedly with the same value at the beginning of the transaction. It is forbidden to read data not committed by other transactions (which will cause phantom reads). Default level of MySQL. ISOLATION_SERIALIZABLE: Serialization, the most expensive and reliable isolation level that prevents dirty reads, unrepeatable reads, and phantom reads.
Dirty read: Indicates that one transaction can read data that has not yet been committed in another transaction. For example, A transaction may attempt to insert record A before the transaction commits, and then another transaction may attempt to read record A.
Non-repeatable read: the same data is read multiple times in a transaction.
Phantom read: When multiple queries within the same transaction return different result sets. For example, the same transaction A has N records in the first query, but n+1 records in the second query under equal conditions. This is like hallucination. Phantom reading occurs when another transaction adds or deletes or modifies data in the result set of the first transaction. When the data content of the same record is modified, all data rows become more or less.
What are the implementation methods of Spring transactions?
Declarative transactions: Declarative transactions are also implemented in two ways, XML configuration file-based and annotated (with @Transaction annotations on the class).
Encoding: Provides encoding for managing and maintaining transactions.
What are the methods for Spring to automate bean assembly?
No: default value, indicating that there is no auto-assembly and that assembly should be done using an explicit bean reference.
ByName: It injects object dependencies based on the bean name.
ByType: it injects object dependencies byType. Constructors: Injecting dependencies through constructors requires a number of parameters to be set.
Autodetect: The container is first assembled using Autowire via constructors, or, if not, byType autoassembly.
How many scopes does Spring support for beans?
Spring supports five scopes, as follows:
Singleton: There is only one instance of a bean in the Spring IOC container. Beans exist in singleton mode, which is the system default;
Prototype: Creates a new example each time a bean is called from the container. GetBean () is equivalent to a new bean () operation;
Request: a bean is created for each HTTP request; Session: The same HTTP session shares a bean instance;
Global-session: Used for portlet containers, since each portlet has a separate session, globalSession provides a global HTTP session.
Note: Use of the Prototype scope should be considered carefully because there is a significant performance overhead associated with frequent bean creation and destruction.
Are spring beans thread safe?
Beans in Spring are singleton by default. The Spring framework does not encapsulate singleton beans in multiple threads. In fact, most of the time spring beans are stateless (such as dao classes), all beans are safe to some extent, but if beans are stateful (such as View Model objects), it is up to the developer to keep them thread-safe. The simplest is to change the scope of the bean and change “singleton” to “Prototype” so that the request bean is equivalent to new bean (), so thread-safe.
Stateful is data storage.
Stateless means no data is saved.
What are the common injection methods of Spring?
- Setter property injection
- Constructor injection
- Annotation injection
What are the main modules of Spring?
Spring Core: The most basic part of the framework, providing IOC and dependency injection features.
Spring Context: A context wrapper built on top of the core wrapper that provides a framework for accessing objects.
The Spring DAO: Data Access Object provides the JDBC abstraction layer.
Spring AOP: Provides a section-oriented programming implementation that lets you customize interceptors, pointcuts, and so on.
Spring Web: Provides integrated features for Web development, such as file uploads, Ioc container initialization using Servlet Listeners, and Web ApplicationContext.
Spring Web MVC: The MVC package in Spring provides an implementation of the Model-View-Controller (MVC) for Web applications.
Explain what AOP is.
Aop is a technology that realizes unified maintenance of program functions through precompilation and dynamic proxy at run time. In simple terms, it is the programming idea of unified handling of a certain “slice” (class) of problems, such as unified handling of logs, exceptions, etc.
17. Explain what ioc is.
Ioc: Inversionof Control (英 文 : Inversionof Control) is the core of spring. For the spring framework, spring is responsible for controlling the life cycle of objects and the relationship between objects. Simply put, control refers to the control the current object has over its internal members; Inversion of control means that this control is not managed by the current object, but by another (class, third party container).
Why use Spring?
Spring provides IOC technology that allows the container to manage dependencies for you, eliminating the need to create and manage dependencies yourself and making it easier to decouple programs.
Spring provides transaction support to make transaction operations more convenient.
Spring provides slice-oriented programming, which makes it easier to handle certain kinds of problems.
More convenient framework integration, Spring can easily integrate with other frameworks, such as MyBatis, Hibernate, etc.
How does the Java Web avoid SQL injection?
Use a pre-prepared PreparedStatement.
Use regular expressions to filter out special characters.
What is the difference between Spring MVC and Struts?
Interception level: Struts2 is class-level interception; Spring MVC is method level interception.
Data independence: The methods of Spring MVC are basically independent, with the request and response data exclusively. The request data is obtained through parameters, and the processing results are returned to the framework through ModelMap. The methods do not share variables. Although struts2 methods are also independent, but all its action variables are shared, which does not affect the operation of the program, but brings some trouble to our coding and reading program.
Interceptor mechanism: Struts2 has its own interceptor mechanism. Spring MVC uses a separate AOP approach, which results in a larger struts2 configuration file than Spring MVC.
Ajax support: Spring MVC integrates Ajax, all of which is easy to use with a @responseBody annotation; Struts2 typically requires installing plug-ins or writing your own code.
What are the four scopes of JSP?
Page: Represents objects and properties associated with a page.
Request: Represents objects and attributes associated with a request made by the client. A request may span multiple pages and involve multiple Web components; Temporary data that needs to be displayed on the page can be placed in this scope.
Session: Represents objects and attributes associated with a session between a user and the server. Data related to a user should be stored in the user’s own session.
Application: Represents objects and properties associated with the entire Web application, which is essentially a global scope that spans the entire Web application and includes multiple pages, requests, and sessions.
What built-in objects does a JSP have? What are the effects?
JSP has nine built-in objects:
Request: Encapsulates a client request that contains parameters from a GET or POST request;
Response: Encapsulates the server’s response to the client.
PageContext: This object is used to get other objects;
Session: The object that encapsulates the user’s session;
Application: encapsulates the object of the server running environment;
Out: Outputs the output stream object that the server responds to.
Config: indicates the configuration object of the Web application.
Page: The JSP page itself (equivalent to this in Java programs);
Exception: An object that encapsulates the exception thrown by the page.
23. How does session work?
Session works as follows: After the client logs in, the server creates a corresponding session. After the session is created, the server sends the SESSION ID to the client, and the client stores the SESSION ID in the browser. In this way, every time the client accesses the server, it will bring the sessionID with it. After the server gets the SessionID, it will find the corresponding session in memory and then work properly.
24. If the client disables cookies, can session be implemented?
Yes, the session only relies on the cookie to store the session ID. If the cookie is disabled, you can add the session ID to the URL to ensure the normal operation of the session.
What is JavaConfig?
The Spring JavaConfig is a product of the Spring community that provides a pure Java method for configuring the Spring IoC container. So it helps to avoid using XML configuration. The advantages of using JavaConfig are:
(1) Object-oriented configuration. Because the configuration is defined as a class in JavaConfig, users can take full advantage of Java’s object-oriented capabilities. One configuration class can inherit from another, override its @bean methods, and so on.
(2) Reduce or eliminate XML configuration. The benefits of externalizing configuration based on dependency injection principles have been proven. However, many developers do not want to switch back and forth between XML and Java. JavaConfig provides developers with a pure Java way to configure a Spring container that is similar in concept to XML configuration. Technically, it is possible to configure the container using only the JavaConfig configuration class, but in practice many people think that mixing JavaConfig with XML is ideal.
(3) Type safety and refactoring friendly. JavaConfig provides a type-safe way to configure the Spring container. Thanks to Java 5.0 support for generics, beans can now be retrieved by type rather than by name, without any casts or string-based look-ups.
What is the principle of Spring Boot automatic configuration?
@enableAutoConfiguration, @Configuration, and @conditionalonClass are at the heart of automatic Configuration.
@enableAutoConfiguration imports the auto-configuration classes defined in meta-INF/Spring. factories into the container.
Filter valid auto-configuration classes.
Each automatic configuration class is combined with the corresponding xxxproperties.java to read configuration files for automatic configuration
How do you understand the Spring Boot configuration loading order? In Spring Boot, you can load configurations in the following ways.
1) Properties file;
2) YAML files;
3) System environment variables;
4) Command line parameters;
And so on…
Twenty-seven, what is YAML?
YAML is a human-readable data serialization language. It is usually used for configuration files. Compared to properties files, YAML files are more structured and less confusing if we want to add complex properties to a configuration file. You can see that YAML has hierarchical configuration data.
28. What are the advantages of YAML configurations?
YAML is now a very popular configuration file format, and you can see YAML configurations on both the front and back ends. What are the advantages of a YAML configuration over a traditional Properties configuration?
Orderly configuration, which is critical in some special scenarios, supports arrays, the elements of which can be either primitive data types or objects, but YAML has another disadvantage over properties configuration files, The @propertysource annotation simply does not support importing custom YAML configurations.
What is Spring Boot?
Spring Boot is a sub-project of Spring open source organization. It is a one-stop solution for Spring components. It mainly simplifies the difficulty of using Spring, saves heavy configuration, and provides various initiators for developers to get started quickly.
Over the years, Spring has become more complex as new features have been added. Just by visiting the Spring. IO/Projects page, we can see the different features of all the Spring projects that we can use in our application.
If we had to start a new Spring project, we had to add build paths or Maven dependencies, configure the application server, and add Spring configuration.
So starting a new Spring project takes a lot of effort, because we now have to do everything from scratch.
Spring Boot is the solution to this problem. Spring Boot is already built on top of the existing Spring framework. With Spring boot, we avoided all the boilerplate code and configuration that we had to do before.
Therefore, Spring Boot can help us use existing Spring functionality more robustly with less effort.
Ok, there are three articles in total, the first one is finished, and the second one will be updated tomorrow!
(beg attention, don’t white whoring I) three support once thank you!