Spring (2) of the basic usage IOC and AOP

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_38060977/article/details/102756659

一 IOC / DI

1 Introduction

Reversing the direction of access to resources . The traditional approach requires resources to find resources to the container component lookup request initiated. In response, the timely return of the container resource. But after the application of IOC, is 容器主动the resource 推送to the component that it manages, the components to be to do is select an appropriate way to accept resources. this behavior is also known as a passive form to find

The DI (the Dependency Injection) - the IOC 一种实现手段: component i.e. with some predefined manner (e.g.,: the setter methods) from the receiving container resources into IOC relative terms, this formulation is more direct (resources vessel injection assembly. way: injecting property, constructor injection

2 injection manner dependent

  1. Autowired / resource and the like (which may additionally have properties, constructors, marked on sertter)
  2. injection setter method (xml using the property tag, the method is actually invoked sertter)
  3. Constructor injection
  4. Static / instance factory (only found inside xml usage)

3 Spring container

Before reading the configuration Bean Bean instance is created in Spring IOC container, it must be instantiated only after the container is instantiated, we can obtain Bean instance from the IOC container and use.

Spring provides two types of IOC container to achieve.

  • BeanFactory: basically IOC container.
  • ApplicationContext: provides more advanced features is a subinterface of BeanFactory.
  • BeanFactory infrastructure Spring framework, Spring for itself; ApplicationContext for developers using the Spring framework, almost all of the applications are directly BeanFactory ApplicationContext ** ** rather than the bottom. Either way use, configuration files are the same

4 IOC implementation process

Reference: https://blog.csdn.net/q982151756/article/details/80291998

springThe IoCvessel inversion process to achieve control and dependency injection may be divided into two stages:

  • Container start-up phase
  • BeanExamples stage

These two phases, the IoCcontainer were made by the following things:

Talk about the process of BeanFactroy

1. container start-up phase

1. Load profile information, packaged into the BeanDefinition
2. All process BeanFactoryPostProcessor(before object creation, object definitions may be modified)

2.Bean instantiation stage

Various Aware Interface
Reference: https://blog.csdn.net/q982151756/article/details/80299547
summary points:
1. How to realize the dependency injection container is provided Aware interface to the object has been generated it?
by BeanPostProcessor. because BeanPostProcessor会处理所有的对象实例. Therefore, at a certain step of the process is instantiated, will find the container prior to the container registered in the spring container ApplicationContextAwareProcessorof this BeanPostProcessor implementation class, then it will call postProcessBeforeInitialization () method, 检查并设置Aware its dependencies.

Two AOP

AOP 1 of the related concepts

  • Aspect (section): Aspect declare the class declaration similar to Java, Aspect will be included in some Pointcut and corresponding Advice.
  • Joint point (connection point): indicates clearly defined points in the program, including typical method calls, and the like and performing access class members exception handler block itself can also nest other joint point.
  • Pointcut (the cut point): represents 一组 joint point, or a combination of these joint point by logic or by wildcard, regular expressions, etc. together, which defines a corresponding local Advice to occur.
  • The Advice (Enhanced): Advice defines procedures Pointcut point which defines the particular operation to be done, it is distinguished by before, after and before each around a joint point, after or instead of the code execution.
  • Target (audiences): weaving the audience Advice ..
  • Aspect process and other objects together and create Adviced object of: Weaving (weaving)

See:
Basic Concepts
point cut usage, etc.

Dynamic proxies in 2 srping

2.1 jdk dynamic proxy

Achieved by creating a subclass interface
See: https://blog.csdn.net/q982151756/article/details/80586894

2.2 cglib

CGLIB (based on ASM的字节码生成库dynamically created) a target class 子类, and then return the object of the subclass, which is enhanced object.
Achieved by the technique of dynamic byte code: dynamic status byte code technology changes the internal logic is implemented to generate a class of new class byte code by converting
the basic usage see: https://www.cnblogs.com/xrq730/ p / 6661692.html
summary
1. interception can define different strategies (via CallbackFilter)
2.final method can not be the agent, will be part of the method of the Object agent
internal call proxy 3. the method will be, the entire agent (different from jdk dynamic agent)
4.spring AOP ie used cglib, each call class are also not agent (the special treatment)

3,4 points principle

public class CarProxy  implements MethodInterceptor {
    private Object target;

    CarProxy(Object target){
        this.target = target;
    }

    @Override
    public Object intercept(Object object, Method method, Object[] objects, MethodProxy proxy) throws Throwable {
        System.out.println("Before Method Invoke");
//object是代理类(增强类,子类); method是父类的method; objects是参数,没什么好说的;proxy是代理类的method
//        proxy.invokeSuper(object, objects);一般都是这么调的,所以全程一直被代理
//        proxy.invoke(object,objects);//有异常
//        System.out.println(object);
//        System.out.println(method);
        method.invoke(target,objects);//类似jdk动态代理的实现,spring aop使用cglib也类似这样。这样内部方法互相调用就不会被代理
        System.out.println("After Method Invoke");

        return object;
    }

}

Principle Reference: Cglib dynamic proxy implementation principle

Reference material

  1. spring series
  2. Notes Introduction

Guess you like

Origin blog.csdn.net/m0_38060977/article/details/102756659