In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I. important concepts of Spring IoC1.1
1) inversion of control (Inversion of control)
Inversion of control is a way to generate or obtain specific objects through description (through xml or annotations in java) and through third parties.
Control inversion IoC (Inversion of Control) means that the control of creating an object is transferred. In the past, the initiative and timing of creating an object were controlled by yourself, but now this power is transferred to a third party, such as an IoC container, which is a factory dedicated to creating objects. It gives you whatever objects you want. With the IoC container, the dependency relationship will change, and the original dependency relationship will be gone. They all rely on the IoC container, through the IoC container to establish the relationship between them.
Control inversion is a reversal of the way of obtaining dependent objects. Normally, the application actively creates dependent objects, realizes the management of dependent objects, and creates the control of dependent objects in the hands of the application. What objects the application needs, take the initiative to create this object, this is the positive situation. After implementing the control inversion, the IoC container realizes the creation and management of the dependent object. What kind of object the application needs, the IoC container creates the object according to the requirements, and the application only passively receives and uses the object. The management control of the creation of the dependent object is transferred from the application to the IoC container, which realizes the control inversion.
2) dependency injection (Dependency Injection)
Another way to express the inversion of control is to let the dependency of the calling class on the implementation class of an interface be injected by a third party (container or collaboration class) to remove the dependency of the calling class on the implementation class of an interface.
3) Beanfacory and ApplicationContext
Spring describes the dependency relationship between Bean and Bean through configuration files, instantiates Bean and establishes the dependency relationship between Bean using the reflection function of Java language. Spring's IoC container provides services such as Bean instance caching, life cycle management, Bean instance proxy, event publishing, resource loading and so on.
Beanfacory is the core interface of Spring framework, which provides the configuration mechanism of advanced IoC. Beanfacory makes it possible to manage different java objects, and ApplicationContext (application context) is based on Beanfacory and provides more reference-oriented functions. Beanfacory is the IoC container, and because ApplicationContext is built on Beanfacory, we also call ApplicationContext the IoC container.
Main functions of IoC container
Dynamically create and inject dependent objects.
Manage the lifecycle of objects.
Map dependencies.
The way to implement the IoC container
Dependent lookup.
Dependency injection.
Three ways of dependency injection
Constructor injection. Etter injection. Interface injection.
The difference between injection and assembly
Injection is the way in which parameters are assigned to a class when it is instantiated.
Assembling defines the relationship between bean and bean.
Overview of Assembly bean:
Based on the configuration in xml.
Based on the configuration in the annotations.
Based on the java class configuration.
Based on Groovy DSL configuration.
Bean scope:
Singleton (singleton): this is the default option for which Spring generates only one instance of Bean throughout the application.
Prototype: each time you inject it, or when you get Bean through the Spring IoC container, Spring creates a new instance for it.
Session (session): used in web applications, where Spring creates only one instance during a session.
Request: used in web applications, where Spring creates an instance in one request, but different requests create different instances. 1.2 based on configuration in xml
1) four types of automatic assembly
ByName: automatically match according to the name.
ByType: automatically match according to the type.
Construtor: automatically matches according to the constructor.
Autodetect: choose byType or construtor according to bean's introspection mechanism.
2) the relationship between Bean
Inherit; depend on; quote. 1.3 Annotation-based configuration
1) use annotations to define bean
Component: used to annotate all classes. Repository: used to annotate the Dao implementation class. Service: used to annotate the Service implementation class. Controller: used to annotate the controller implementation class.
2) automatic assembly
Autowired: dependency injection of Bean through the @ Autowired annotation.
The required property of @ Autowired: used to specify whether a matching Bean must be found.
@ Qualifier, specify the name of the Bean.
Profile: used to switch development environments.
Spring EL: a more flexible injection method that can build complex expressions at runtime, access object properties, object method calls, and so on.
Action
Use bean id to reference bean.
Calls the methods of the specified object and accesses the properties of the object.
To do the math.
Provide regular expressions for matching.
Collection configuration. 2. Aspect-oriented programming (Aspect Oriented Programming)
Overview: AOP technology uses "crosscutting" technology to unravel the interior of encapsulated objects, encapsulate common behaviors that affect multiple classes into a reusable module, and name it "Aspect", or aspect. The so-called "aspect" simply means that the logic or responsibility that has nothing to do with the business but is jointly invoked by the business module is encapsulated in order to reduce the repetitive code of the system and reduce the coupling between the modules. and conducive to future maneuverability and maintainability.
2.1 AOP related Concepts
Aspect (Aspect): the modularization of a concern implementation that may otherwise crosscut multiple objects. Transaction management is a good example of crosscutting concerns in J2EE applications. Aspects are implemented using Spring's Advisor or interceptor.
Joinpoint: an explicit point during the execution of a program, such as a call to a method or a specific exception being thrown.
Advice: actions performed by the AOP framework at a specific join point. Various types of notifications include "around", "before", and "throws" notifications. Notification types are discussed below. Many AOP frameworks, including Spring, use interceptors as notification models to maintain a chain of interceptors "around" join points. Four advice are defined in Spring: BeforeAdvice, AfterAdvice, ThrowAdvice and DynamicIntroductionAdvice.
Pointcut (Pointcut): specifies a collection of join points that a notification will be raised. The AOP framework must allow developers to specify pointcuts: for example, using regular expressions. Spring defines the Pointcut interface to combine MethodMatcher and ClassFilter. It can be clearly understood by the name that MethodMatcher is used to check whether the method of the target class can be applied to this notification, while ClassFilter is used to check whether the Pointcut should be applied to the target class.
Introduction: add a method or field to the notified class. Spring allows the introduction of new interfaces to any notified object. For example, you can use an introduction to make any object implement the IsModified interface to simplify caching. To use Introduction in Spring, you can implement notification through DelegatingIntroductionInterceptor and configure the interface to be implemented by Advice and proxy classes through DefaultIntroductionAdvisor.
Target object (Target Object): an object that contains a connection point. Also known as the notified or proxied object. POJO .
AOP agent (AOP Proxy): an object created by the AOP framework that contains notifications. In Spring, the AOP agent can be a JDK dynamic agent or a CGLIB agent.
Weaving: assembles aspects to create a notified object. This can be done at compile time (for example, using the AspectJ compiler) or at run time. Spring, like other pure Java AOP frameworks, completes weaving at runtime.
Before advice: a notification executed before a join point (join point), but this notification does not prevent execution before the join point (unless it throws an exception).
Post notification (After advice): a notification that is executed when a connection point exits (whether it is a normal return or an abnormal exit).
Around Advice: a notification, such as a method call, that surrounds a join point. This is the most powerful type of notification. A surround notification can complete custom behavior before and after a method call. It also chooses whether to continue execution of the join point or simply return their own return value or throw an exception to end execution.
After returning advice: a notification executed after a join point (join point) completes normally, for example, a method returns normally without throwing any exceptions.
Notification after throwing an exception (After throwing advice): a notification that is executed when a method throws an exception and exits. 2.2 there are four ways to implement Spring AOP:
Using proxyFactoryBean and corresponding interfaces to implement AOP
Configure AOP using XML
Use @ AspectJ annotations to drive sections
Using AspectJ to inject multi-section 2.3section
There is an order attribute in aspect, and the number of the order attribute is the order in which the crosscutting concerns are in.
Spring defaults to the order in which aspect is defined as the weaving order. III. Spring transaction Management 3.1 transaction Manager
Spring does not manage transactions directly, but provides a variety of transaction managers, which delegate the responsibility of transaction management to transactions of the relevant platform framework provided by persistence mechanisms such as Hibernate or JTA. The interface of Spring transaction manager is org.springframework.transaction.PlatformTransactionManager. Through this interface, Spring provides corresponding transaction managers for various platforms such as JDBC, Hibernate and so on.
3.2 definition of transaction attributes
1) Communication behavior
Spring defines seven propagation behaviors, and the following are common types:
PROPAGATION_REQUIRED: indicates that the current method must be running in a transaction. If the current transaction exists, the method will run in that transaction. Otherwise, a new transaction will be started
PROPAGATION_SUPPORTS: indicates that the current method does not require a transaction context, but if there is a current transaction, the method will run in that transaction
PROPAGATION_MANDATORY: indicates that the method must be run in a transaction. If the current transaction does not exist, an exception will be thrown.
2) isolation level
The isolation level defines the extent to which one transaction may be affected by other concurrent transactions.
ISOLATION_DEFAULT: use the default isolation level of the back-end database
ISOLATIONREADUNCOMMITTED: the lowest isolation level that allows uncommitted data changes to be read, which may result in dirty, phantom, or unrepeatable reads.
ISOLATIONREADCOMMITTED: allows reading of committed data from concurrent transactions, prevents dirty reads, but phantom or unrepeatable reads can still occur
ISOLATIONREPEATABLEREAD: the result of multiple reads to the same field is consistent, unless the data is modified by its own transaction, which can prevent dirty reading and non-repeatable reading, but phantom reading can still occur.
ISOLATION_SERIALIZABLE: the highest isolation level, fully compliant with the ACID isolation level, ensuring that dirty, non-repeatable, and phantom reads are prevented, and the slowest transaction isolation level, as it is usually achieved by fully locking transaction-related database tables
3) read-only: by making the transaction read-only, you can give the database a chance to apply the optimizations it deems appropriate.
4) transaction timeout: a transaction timeout is a timer for a transaction, and if the transaction is not finished within a certain period of time, it will be rolled back automatically instead of waiting for it to end.
5) rollback rules: these rules define which exceptions cause the transaction to roll back and which do not.
3.3 differences between programmatic and declarative transactions
Spring provides support for programmatic and declarative transactions, which allow users to precisely define the boundaries of transactions in code, while declarative transactions (based on AOP) help users decouple actions from transaction rules.
To put it simply, programmatic transactions invade the business code, but provide more detailed transaction management; while declarative transactions, based on AOP, can not only play the role of transaction management, but also do not affect the specific implementation of the business code.
Author: Yao Yuan
Source: Yixin Institute of Technology
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.