mysql tutorial column introduces what you must master about Spring.
Hello everyone! I am an enthusiastic member of the Chaoyang people.
The Spring framework is a must-ask question during interviews. What exactly does it contain? Let's take a look. This is also a question I often ask in interviews, and it also reflects a programmer's ability to understand the framework.
Spring is a lightweight framework that aims to Improve the development efficiency of developers and the maintainability of the system.
What we generally call the Spring Framework is the Spring Framework. It is a collection of many modules. Using these modules can easily assist us in development. These modules are Core Containers, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Tools, Messaging, and Testing modules. For example, the Core component in Core Container is the core of all Spring components, Beans components and Context components are the basis for implementing IOC and DI, and AOP components are used to implement aspect-oriented programming.
6 features of Spring:
The picture below corresponds to the Spring 4.x version. The Portlet component of the Web module in the latest 5.x version has been abandoned and added WebFlux components for asynchronous reactive processing.
IOC (Inversion Of Controll, Inversion of Control) is a design idea, that is The control of manually created objects in the program is handed over to the Spring framework. IOC is also used in other languages and is not specific to Spring. The IOC container is the carrier used by Spring to implement IOC. The IOC container is actually a Map (key, value), and various objects are stored in the Map.
Leave the interdependencies between objects to be managed by the IOC container, and the IOC container completes the injection of the objects. This can greatly simplify application development and free applications from complex dependencies. The IOC container is like a factory. When we need to create an object, we only need to configure the configuration file/annotations without thinking about how the object is created. In actual projects, a Service class may have hundreds or even thousands of classes as its bottom layer. If we need to instantiate this Service, we may have to figure out the constructors of all the underlying classes of this Service every time, which may confuse people. Drive crazy. If you use IOC, you only need to configure it and reference it where needed, which greatly increases the maintainability of the project and reduces the difficulty of development.
In the Spring era, we usually configured beans through XML files. Later, developers felt that using XML files to configure beans was not good, so Spring Boot annotation configuration gradually became popular.
##AOP(Aspect-Oriented Programming, aspect-oriented programming ) can encapsulate the logic or responsibilities (such as transaction processing, log management, permission control, etc.) that have nothing to do with the business but are commonly called by the business modules, so as to reduce the duplication of code in the system, reduce the coupling between modules, and have Conducive to future scalability and maintainability.
Spring AOP is based on dynamic proxy. If the object to be proxied implements a certain interface, then Spring AOP will use JDK dynamic proxy to create the proxy object; but for objects that do not implement the interface, it cannot be used. JDK dynamic proxy instead uses CGlib dynamic proxy to generate a subclass of the proxy object as a proxy. Of course, you can also use AspectJ. Spring AOP has integrated AspectJ. AspectJ should be regarded as the most complete AOP framework in the Java ecosystem. After using AOP, we can abstract some common functions and use them directly where they are needed, which can greatly simplify the amount of code. We need to add new functions conveniently and improve the scalability of the system. AOP is used in scenarios such as logging function, transaction management, and permission management.Most of the time we do not use multi-threading in the system, so few people pay attention to this issue. Singleton beans have threading problems, mainly because when multiple threads operate the same object, writing operations to the non-static member variables of this object will cause thread safety issues.
There are two common solutions:
Bean life cycle in Spring?
Talking about this issue, we have to mention the previous eras of Model1 and Model2 without Spring MVC.
**Model1 era:** Many back-end programmers who learned Java late may not have been exposed to JavaWeb application development in the Model1 mode. In Model1 mode, the entire Web application is almost entirely composed of JSP pages, and only a small number of JavaBeans are used to handle database connection, access and other operations. In this mode, JSP is both the control layer and the presentation layer. Obviously, there are many problems with this model. For example, control logic and performance logic are mixed together, resulting in extremely low code reuse rate; another example is that the front-end and back-end are interdependent, making testing difficult and development efficiency extremely low.
Model2 Era: Friends who have studied Servlet and done related demos should understand the development model of Java Bean (Model) JSP (View) Servlet (Controller). This is the early Java Web MVC development model. Model is the data involved in the system, that is, dao and beans; View is used to display the data in the model, just for display; Controller sends user requests to Servlet for processing, returns the data to JSP and displays it to the user.
There are still many problems in the Model2 mode. The degree of abstraction and encapsulation of Model2 is far from enough. When using Model2 for development, it is inevitable to reinvent the wheel, which greatly reduces the maintainability and usability of the program. Reusability. As a result, many MVC frameworks related to Java Web development came into being, such as Struts2. However, because Struts2 is relatively cumbersome, with the popularity of Spring lightweight development framework, the Spring MVC framework appeared in the Spring ecosystem. Spring MVC is currently the best MVC framework. Compared with Struts2, Spring MVC is simpler and more convenient to use, has higher development efficiency, and Spring MVC runs faster.
MVC is a design pattern, and Spring MVC is an excellent MVC framework. Spring MVC can help us develop a more concise Web layer, and it is naturally integrated with the Spring framework. Under Spring MVC, we generally divide back-end projects into Service layer (processing business), Dao layer (database operations), Entity layer (entity classes), and Controller layer (control layer, returning data to the front-end page).
The simple schematic diagram of Spring MVC is as follows:
##What design patterns are used in the Spring framework?
@Bean annotation usage example:
@Configurationpublic class AppConfig { @Bean public TransferService transferService() { return new TransferServiceImpl(); }}复制代码
The above code is equivalent to the following XML configuration:
<beans> <bean id="transferService" class="com.common.TransferServiceImpl"/></beans>复制代码
The following example cannot be passed@ Implemented by Component annotation:
@Beanpublic OneService getService(status) { case (status) { when 1: return new serviceImpl1(); when 2: return new serviceImpl2(); when 3: return new serviceImpl3(); }}复制代码
我们一般使用@Autowired注解去自动装配bean。而想要把一个类标识为可以用@Autowired注解自动装配的bean,可以采用以下的注解实现:
在TransactionDefinition接口中定义了五个表示隔离级别的常量:
**ISOLATION_DEFAULT:**使用后端数据库默认的隔离级别,Mysql默认采用的REPEATABLE_READ隔离级别;Oracle默认采用的READ_COMMITTED隔离级别。
**ISOLATION_READ_UNCOMMITTED:**最低的隔离级别,允许读取尚未提交的数据变更,可能会导致脏读、幻读或不可重复读。
**ISOLATION_READ_COMMITTED: **Allow reading of data that has been submitted by concurrent transactions, which can prevent dirty reads, but phantom reads or non-repeatable reads may still occur
**ISOLATION_REPEATABLE_READ:**For the same field The results of multiple reads are consistent, unless the data is modified by the own transaction itself, which can prevent dirty reads and non-repeatable reads, but phantom reads may still occur.
**ISOLATION_SERIALIZABLE: **The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one after another in order, so that there is no possibility of interference between transactions. In other words, this level can prevent dirty reads, non-repeatable reads and phantom reads. But this will seriously affect the performance of the program. Normally this level is not used.
Eight constants representing transaction propagation behavior are defined in the TransactionDefinition interface.
**PROPAGATION_REQUIRED: **If a transaction currently exists, join the Transaction; if there is currently no transaction, create a new transaction.
PROPAGATION_SUPPORTS: If a transaction currently exists, join the transaction; if there is currently no transaction, continue running in a non-transactional manner.
PROPAGATION_MANDATORY: If a transaction currently exists, join the transaction; if there is no transaction currently, throw an exception. (mandatory: mandatory).
##PROPAGATION_REQUIRES_NEW: Create a new transaction , if a transaction currently exists, suspend the current transaction.
PROPAGATION_NOT_SUPPORTED: Run in non-transactional mode. If a transaction currently exists, the current transaction will be suspended.
PROPAGATION_NEVER: Run in non-transactional mode, throwing an exception if a transaction currently exists.
Other situations:
PROPAGATION_NESTED: If a transaction currently exists, create a transaction to run as a nested transaction of the current transaction; if there is currently no transaction, Then this value is equivalent to PROPAGATION_REQUIRED.
I hope you can master these contents and continue to support me, thank you.
More related free learning recommendations: mysql tutorial(Video)
The above is the detailed content of collect! What Spring must master. For more information, please follow other related articles on the PHP Chinese website!