This is the 30th day of my participation in the August Challenge
What’s the difference between BeanFactory and ApplicationContext
A BeanFactory can be thought of as a factory class that contains a collection of beans. The BeanFactory contains the definition of a bean to instantiate when a client request is received.
BeanFactory can also generate relationships between collaborating classes when instantiating objects. This frees up the configuration of the bean itself and the bean client. BeanFactory also includes control of the bean lifecycle, calling client-side initialization methods and destruction methods.
On the surface, the Application Context, like a bean Factory, has bean definitions, bean association Settings, and the ability to distribute beans on request. But the Application Context provides additional functionality on top of that.
Text messages that support internationalization are provided
Unified resource file reading method
Events for beans that have been registered in listeners
The life cycle of Spring beans
The life cycle of Spring beans is straightforward. When a bean instance is initialized, a series of initialization operations need to be performed to reach a usable state. Similarly, when a bean is not being called, it needs to be destructed and removed from the bean container.
The Spring Bean Factory is responsible for managing the life cycle of beans that are created in the Spring container. The Bean life cycle consists of two sets of call back methods.
The callback method invoked after initialization.
Destroys the previously called callback method.
The Spring framework provides four ways to manage bean lifecycle events:
InitializingBean and DisposableBean callback interfaces
Other Aware interfaces for specific behaviors
The Custom init() and destroy() methods are in the Bean configuration file
@postConstruct and @PreDestroy annotation methods
How is Spring IOC implemented* * * *
Spring in the org. Springframework. Beans bag and org. Springframework. Context bag formed the basis of the Spring framework IoC container.
The BeanFactory interface provides an advanced configuration mechanism that makes it possible to configure any type of object. The ApplicationContex interface extends the BeanFactory (which is a subinterface) by adding features such as easier integration with Spring’s AOP, There are also mechanisms for handling Message Resources (for internationalization), event propagation, and application-layer ad-hoc configurations, such as WebApplicationContext for Web applications.
Org. Springframework. Beans. Factory. The BeanFactory is the realization of a Spring IoC container, used for packaging and management mentioned above all kinds of beans. The BeanFactory interface is the core interface of the Spring IoC container.
Said that Spring AOP
Faceted programming, in our applications, often involves doing things unrelated to the core business, such as logging the execution times of all update* methods, who did them, and so on.
With Spring’s AOP technology, you can accomplish this requirement without modifying the update* code.
Implementation principles of Spring AOP
There are two main types of dynamic proxies in Spring AOP, JDK dynamic proxies and CGLIB dynamic proxies. JDK dynamic proxies receive proxied classes through reflection and require that the proxied classes implement an interface. At the heart of JDK dynamic proxies are the InvocationHandler interface and Proxy class.
If the target class does not implement the interface, Spring AOP chooses to use CGLIB to dynamically proxy the target class. CGLIB
Code Generation Library (CGLIB) is a Code Generation Library that dynamically generates subclasses of a class at runtime. Note that CGLIB is dynamically proxyed by inheritance, so if a class is marked final, it cannot be dynamically proxyed using CGLIB.
Dynamic Proxy (Cglib and JDK)
Both JDK dynamic proxy classes and delegate classes need to implement the same interface. This means that only classes that implement an interface can use Java dynamic proxy. However, it turns out that not every class you encounter will implement an interface for you. Therefore, this mechanism cannot be used for classes that do not implement interfaces. CGLIB implements dynamic proxies for classes.
Spring transaction implementation
1. Coding method
Programmatic transactions refer to transactions implemented by code, similar to JDBC programming for transaction management.
2. Declarative transaction management
Declarative transaction management can be implemented in two ways: based on XML configuration files; The other is annotating @Transaction on business methods to apply Transaction rules to business logic
Underlying principles of Spring transactions
A. Divide the processing unit — IOC
Since spring solves the problem of performing local transactions on a single database, the implementation uses IOC in Spring to divide the transaction unit. The various configurations for transactions (setting up transaction managers, propagation characteristics and isolation mechanisms for transactions) are placed in the IOC container.
B. AOP intercepts classes that need to be transacted
Spring transaction processing module is done by AOP functionality of declarative transaction processing, specific operation (such as transaction configuration and read, transaction object abstraction), use TransactionProxyFactoryBean interface to use AOP functionality, to generate the proxy agent object, The interception of proxy methods is completed through the TransactionInterceptor, weaving transaction processing functions into the interception method. Read the IOC container transaction configuration properties and convert them to Spring transactions
Need internal data structures (TransactionAttributeSourceAdvisor), into TransactionAttribute said data objects.
C, transaction processing implementation (transaction generation, commit, rollback, suspend)
Spring delegates to a specific transaction handler implementation. Implements an abstraction and adaptation. Suitable specific transaction processor: DataSource DataSource support, hibernate DataSource transaction support, JDO DataSource transaction support, JPA, JTA DataSource transaction support. This support is all by design
PlatformTransactionManager, AbstractPlatforTransaction series of transaction support. A series of TransactionManagers are provided for common data source support.
D, in combination with
PlatformTransactionManager implements TransactionInterception interface, let its combining with TransactionProxyFactoryBean, form a Spring declarative transaction processing system of design.
How do I customize the annotation implementation
Creating custom annotations is similar to creating an interface, except that the interface keyword of the annotation begins with the @ sign.
Annotation methods cannot take parameters;
Annotation methods return values that are limited to primitive types, Strings, Enums, annotations, or arrays of these types.
Annotation methods can have default values;
Annotations themselves can contain meta-annotations that are used to annotate other annotations.
Spring MVC runs the process
1. Spring MVC submits all requests to a DispatcherServlet, which delegates the actual processing of the request to other modules in the application system.
2.DispatcherServlet queries one or more HandlerMapping and finds the Controller that processes the request.
3.DispatcherServlet please request submission to target Controller
4. The Controller will return a ModelAndView after business logic processing
Dispathcher queries one or more ViewResolver view parsers to find the view object specified by the ModelAndView object
6. View objects are responsible for rendering back to the client.
Spring MVC startup process
Spring MVC Servlet is configured load-on-startup in the web. XML file, so the application starts
The Spring MVC is initialized with the contextConfigLocation configured in the HttpServletBean
Property to the Servlet, and then create the WebApplicationContext in the FrameworkServlet,
The DispatcherServlet is initialized from the XML file in the CLASspath configured with contextConfigLocation
The overall Spring MVC component.
Why Netty
- API simple to use, low development threshold;
- Powerful, preset a variety of encoding and decoding functions, support a variety of mainstream protocols;
- With strong customization capability, the communication framework can be flexibly extended through ChannelHandler;
- High performance. Compared with other mainstream NIO frameworks in the industry, Netty has the best overall performance.
- Mature and stable, Netty has fixed all the JDK NIO bugs it has found, and business developers no longer need to worry about NIO bugs;
6) Active community, short version iteration cycle, found bugs can be fixed in time, at the same time, more new functions will be added;
- Experienced large-scale commercial application test, quality has been verified. It has been successfully used in the Internet, big data, online games, enterprise applications, telecom software and many other industries, proving that it can fully meet the commercial applications of different industries.
Because of these advantages, Netty is becoming the framework of choice for Java NIO programming.
This section describes the Netty application scenario
Build various Java middleware with high performance and low latency, such as MQ, distributed service framework, ESB message bus, etc. Netty is mainly used as a basic communication framework to provide high performance and low latency communication services.
The basic communication framework of public or private protocol stack, for example, the asynchronous and high-performance WebSocket protocol stack can be built based on Netty;
For applications in various fields, such as big data and games, Netty is used as a high-performance communication framework for data distribution, transmission, and summary of internal modules to achieve high-performance communication between modules.
The native NIO has an epoll bug in JDK 1.7
It causes the Selector to poll for nothing, eventually causing the CPU to be 100%. It was claimed that the issue was fixed in JDK 1.6 update18, but the issue still exists in JDK 1.7, although the BUG is less likely to occur and not completely resolved.
What is TCP sticky/unpack
1. If the data to be sent is larger than the remaining space of the TCP send buffer, packet unpacking will occur.
2. If the data to be sent is larger than MSS (maximum packet length), TCP unpacks the data before transmission.
3. If the data to be sent is smaller than the size of the TCP send buffer, TCP sends out the data that has been written into the buffer for several times, causing sticky packets.
4. The application layer of the receiving end does not timely read the data in the receiving buffer, and sticky packets will occur.
TCP sticky/unpack solution
1. The sender adds a header to each packet, which should contain at least the length of the packet. In this way, after receiving data, the receiver can know the actual length of each packet by reading the length field in the header of the packet.
2. The sender encapsulates each packet into a fixed length (if not enough, it can be filled by adding 0), so that the receiver automatically splits each packet by reading the data of a fixed length from the receiving buffer each time.
3. Boundaries can be set between packets, such as adding special symbols, so that the receiver can separate different packets through this boundary.
Netty threading model
First, Netty uses EventLoop to handle read and write events on a connection, and all requests on a connection are guaranteed to be processed in an EventLoop, where there is only one Thread. Thus, all events on a connection are executed in only one thread. An EventLoop group contains multiple Eventloops, which can be thought of as a thread in the Reactor thread model, and an EventLoop group is similar to an ExecutorService
Talk about Netty’s zero copy
Zero copy means that the CPU does not need to consume resources for copying data between memory during computer operations. It usually refers to the way in which computers send files directly to the network in the Kernel Space without copying the contents of the files to the User Space.
Netty internal execution process
- Netty receives and sends bytebuffers using DIRECT BUFFERS, which use out-of-heap DIRECT memory for Socket reading and writing without the need for secondary copy of byte BUFFERS. If traditional HEAP BUFFERS are used for Socket reads and writes, the JVM copies the HEAP Buffer into direct memory before writing it to the Socket. The message is sent with an extra memory copy of the buffer compared to direct out-of-heap memory.
Need white pixie Java learning materials including “JVM, Netty, Mysql, Mybatis, Redis, Dubbo, Nginx, design mode” and other 10G information package, you can see my home page or private blog
Clocking updates to Java articles 30/100 days
** Everyone can like, favorites, follow and comment on me **