Spring IoC / AOP underlying principle

1.IoC

Spring official quoted text: This chapter covers the Spring Framework implementation of the

Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a

Process whereby Objects the DEFINE Their the Dependencies, that IS, at The OTHER Objects They Work with, only
through constructor arguments The, arguments The to A Factory's Method,, or the Properties that are the SET ON at The
Object instance the After IT IS Constructed or returned from A Factory's Method,. at The Container the then injects
Those Dependencies When IT Creates The the bean. This Process IS Fundamentally The inverse, hence The
name Inversion of Control (the IoC), of The the bean Itself Controlling The instantiation or LOCATION of ITS
Dependencies by the using Direct Construction of classes, or A mechanism SUCH the Locator-Service AS
pattern.
"inversion of control (the IoC)" is also referred to as "dependency injection (the DI)", is a definition of an object-dependent process, only the objects
and structural parameters, the factory method parameters, properties or factory methods object instance return related. The bean container to create
inject these dependencies time. This process is a reverse process, so named for the Dependency Inversion, create an object instance by
Constructor or service positioning mechanism it provides to achieve.
IoC biggest advantage is the "decoupling."

1.1 initialization process vessel
new new ClassPathXmlApplicationContext ();
the ContextLoaderListener / the DispatcherServlet -> the WebApplicationContext
initialization flow ApplicationContext container is mainly composed refresh AbstractApplicationContext class
implementation. The general process of: performing subsequent processing (eg: context: propertyPlaceholder BeanFactory is an object
, etc.) -> register context (Context) in bean-> bean is registered intercept processor (AOP related) -> on initialization
hereinafter message (id is initialized messgeSource international bean object) -> initialize event multicast (processing event monitor
to listen, as ApplicationEvent, etc.) -> initialization theme resource (SpringUI theme ThemeSource) -> register custom
-defined listener -> instantiate all non-lazy-init the singleton instance -> publish corresponding event (lifecycle interfaces associated implementation
life cycle events like release)
in the spring, the process of building a container are synchronized. Synchronization is done to ensure that the process of building container, not
multi-threaded resource conflicts arise.

Construction of the BeanFactory. ApplicationContext BeanFactory is the parent interface. It is spring frame
top-level container factory object. BeanFactory bean can only manage objects. No other features. Such as: aop management section,
propertyplaceholder loading and so on. Construction of BeanFactory function is managed bean objects.
Creation bean object management BeanFactory.
postProcessBeanFactory - load the configuration content BeanFactory can not handle. Such as:
propertyplacehodler load.
invokeBeanFactoryPostProcessors - will step on the content loaded as a bean container can manage the
registration to an ApplicationContext object. In essence, the bottom loaded content postProcessBeanFactory
packaged into a container ApplicationContext bean objects can be managed.
registerBeanPostProcessors - continue to complete the last step of the registration operation. Bean object configuration file configuration
are created and the registration is complete.
initMessageSource - i18n, internationalization. Initialization international news sources.
initApplicationEventMulticaster - register event Multicast Listener. As ApplicationEvent event. It is spring
framework observer pattern implementation mechanism.
onRefresh - initialization theme resource (ThemeSource). View topic information spring framework.
registerListeners - Create a listener and register.
finishBeanFactoryInitialization - initial configuration appearing in all the lazy-init = false bean object.
And bean object must be a singleton.
finishRefresh - the last step. To release the final event. Lifecycle listener events. spring container defines a green
life cycle interface. The container can be achieved initialization start calling, calling the recovery of resources before the container destroyed. Lifecycle interface.

Over 1.2 containers / vessels Sons conceptual
  Spring framework allows containers to create a plurality of application contexts. It is recommended that the container has a parent-child relationship. May be
provided by a parent container setParent ConfigurableApplicationContext method defined in the interface. Once the parent
-child relationship, you can get all of the parent container resources other than PropertyPlaceHolder by child container, the parent container is not
able to obtain any resource sub-container (of similar types in Java inheritance).
A typical container father and son: When spring and springmvc used simultaneously. ContextLoaderListener created
container is the parent container, container DispatcherServlet create a child container.
Ensure a JVM, only one container tree tree structure. Parent container resources can be accessed through the sub-container.

1.3 p domain / c-domain
introduces p (property tag) domain after Spring2.0, introduction of c (constractor-arg tag) after Spring3.1
domain. You can simplify the configuration file, and configure property of the constructor-arg.

Lookup Method-1.4
Lookup-Method Once applied, Spring Framework CGLIB technique automatically uses the specified type to create a dynamic
subtype, automatically and abstract methods. Dynamically data dependency injection preparation.
In efficiency, than direct custom subtypes slow. Relatively more common. Lookup-method can only provide square
method returned object can be achieved dynamic objects returned.
In the factory method is difficult to use a customized time.
It is an application template. Extended factory method.
The: method returns the object type is factory interface type. And different versions of the application object that is returned may not be used in conjunction with.
To avoid the development of multiple factory class.

2 AOP

Aspect Oriented Programming, the underlying principle is the dynamic proxy implementation. If you cut the policy objectives interface, use
JDK dynamic proxy technology; no interface is used to generate dynamic proxy CGLIB technology.
In the business environment, the use of the interface is very high, mainly in the analysis of how Spring JDK dynamic generation of
management techniques to generate dynamic proxy objects. Main code JdkDynamicAopProxy, AdvisedSupport,
DefaultAdvisorChainFactory, ReflectiveMethodInvocation class.

3. AOP-expression The commonly used Pointcut
AOP development, there are a few very important concept, which has a concept called "cut-off point." Representative informed cut into the generations
that position point code execution flow.
Cut point is generally defined by the expression. spring framework will be an analytical expression by SpringEL. There are many expressions
defined ways. Corresponding to different analysis results.

3.1 execution expression
syntax: execution (the package name class name return type method name (parameter list).)
As:
Execution (java.lang.String com.xxx.service.AService.test (java.lang.Integer))
in the test method has com.xxx.service.AService type, and the parameters Integer, String return type when
increasing section.
execution (* com.xxx.AService. * (.. ))
by any method com.xxx.AService types, returns the result of any type, not limited to the parameter table, have increased section.
Application: The most commonly used. It is relatively the most common. Performed according to standard methods, the definition of tangent point. Such as: transaction processing, Japan
Chi processing.

3.2 target expression
to the target object is defined as a way of expression cut-off point.
Syntax: target (package name interface name.)
Such as: target (com.xxx.IA) - all realized its class IA interface as the target object proxy will automatically
increase woven into notice, implement the aspect.
Application: a configuration provides for a specific interface. Such as: Log. Affiliated logical time you log in to perform the
album is more and more. In the different business processes, the subsidiary logic is different. Such as: electricity supplier, it may be time to log in, the
need to execute cart merge.

3.3 this expression
implements an interface proxy object, will be used as cut-off point. And target are very similar.
Syntax: this (package name interface name.)
As: this (com.xxx.IA) - If the proxy object implements IA Proxy interface, as a connection point.
Application: proxy configuration for a specific offer. Fine particle size than the target cut point. Because the target object can be more
realized. Proxy object can be a plurality of interfaces for a certain target audience to achieve, provide a specific cut-off point. Such as: silver
Login row, the type of bank account very much. And cross. Such as: a debit card, credit card, debit card loan, credit
note repayment cards. It can provide a cut-off point for the repayment of the interface, record keeping and other loan information.

3.4 within expression
to the package as a target, defined cut point.
Syntax: within (. * Package name) - interface or on behalf of any type in the package are as cut-off point.
Application: a tangent point for providing a packet, a size coarser than target. Such as: a package All interfaces are required
to execute a subsidiary logic. Such as: orders in the electronic business platform. Service orders may require a specific logic (timestamp school
inspection, inventory check, etc.), these logical, is not required to provide other business lines in the cut surface.

3.5 args expression
to certain parameters of the standard as defined cut point.
Syntax: args (type, type) - represents the parameter table method to meet the requirements of the time, as the cut-off point. Parameter table are
sequential.
Application: mainly used in the calibration parameters. Such as: login time must pass two string parameters (login name and secret
code). Args can be defined. This is achieved with the execution. Such as: Execution (.. * XXXX * Login (..))
args (String, String). It is to use the lowest frequency of expression

 

Guess you like

Origin www.cnblogs.com/Anemia-BOY/p/11271837.html