spring underlying framework of knowledge summary

table of Contents

1, spring principle structure

 

 

9, what is the inversion of control (IOC), alias dependency injection

9. What is the Spring IOC container?

10, Beanfactory and Application contexts What is the difference?

 

12, Spring supports the interpretation of the scope of several bean

13, singleton bean Spring Framework is thread safe?

14, bean life cycle

15, annotation assembly (@Required annotation, @ Autowired annotation, @ Qualifier annotation)

16, spring in bean to resolve circular dependencies

Solution

17, Spring MVC framework

18, spring design patterns used


1, spring principle structure

aop technology

Spring AOP technology in a dynamic proxy mode design mode. Just InvocationHandler jdk dynamic proxy interface provided by all methods proxied object InvocationHandler takeover by the actual processing tasks.

9, what is the inversion of control (IOC), alias dependency injection

In Spring IOC takes advantage of the powerful Java reflection mechanism to achieve. The so-called dependency injection i.e. the dependencies between components at runtime determined by the container. Wherein there are two methods dependency injection, injection by the constructor, set by implanting method.

9. What is the Spring IOC container?

The entire life cycle of Spring IOC container-managed bean objects. To store the bean its underlying concurrenthashmap.

10, BeanFactory and Application contexts What is the difference?

BeanFactory:

Spring which is the lowest level of interface, providing a simple function of the container, only provide examples of objects and object functionality get;

BeanFactory at startup will not go instantiation Bean, from the container when there is the will to take the Bean to instantiate;

ApplicationContext:

Application context, inheritance BeanFactory interface, it is a more advanced each container of Spring

ApplicationContext when you start to put all Bean all instantiated.

Concrete realization (ClassPathXmlApplicationContext)

 

12, Spring supports the interpretation of the scope of several bean

Spring framework supports the following five bean scope:

  • Singleton:  the bean only one instance (default) Spring ioc each container.

  • prototype: a bean definition can have multiple instances.

  • request: every http request creates a bean, this scope is only valid in the case of Spring ApplicationContext web-based.

  • session: a HTTP Session, the definition to a bean. Only valid in the case of Spring ApplicationContext web-based.

  • global-session: a global HTTP Session, the definition to a bean. Only valid in the case of Spring ApplicationContext web-based.

13, singleton bean Spring Framework is thread safe?

No, singleton Spring bean frame is not thread safe. But basically bean used to implement dependency injection, modifying the operation does not exist, so the problem is relatively unsafe thread does not exist.

Why bean is a singleton?

Only when instantiated once initialized, improve efficiency;

It reduces memory overhead

14, bean life cycle

Lifetime of an object: create (instantiate - initialization) - Use - destruction, Spring provides a number of external interfaces that allow developers of these three processes (instantiation, initialization, destruction) before and after doing some operations. 
In the Spring Bean, instantiate is to open up space for the bean objects (concrete can be understood as calling the constructor), initialization is the initialization of the property, which is property injection (constructor may also have to initialize a statement attribute, but does not belong this part), and the injection is injected into the property by property setter method (either way or annotation bean configuration property attribute ways, its essence is through the property setter method implementation).

15, annotation assembly (@Required annotation, @ Autowired annotation, @ Qualifier annotation)

@Required Note: the bean specify an id (byname), assembled injection

@Autowired Note: The type of automatic assembly (byType)

@Qualifier Note: use with @Autowired notes, to avoid confusion bean

16, spring in bean to resolve circular dependencies

Dependency injection little attention appears circular dependency:
dependency between sequential Bean: BeanA -> BeanB -> BeanA
example:

@Component
public class CircularDependencyA {
 
    private CircularDependencyB circB;
 
    @Autowired
    public CircularDependencyA(CircularDependencyB circB) {
        this.circB = circB;
    }
}
@Component
public class CircularDependencyB {
 
    private CircularDependencyA circA;
 
    @Autowired
    public CircularDependencyB(CircularDependencyA circA) {
        this.circA = circA;
    }
}

Run SpringBootApplication:

@SpringBootApplication
@ComponentScan("com.example.circulardependency.constructor")
public class CirculardependencyApplication {

    public static void main(String[] args) {
        SpringApplication.run(CirculardependencyApplication.class, args);
    }
}

An error

BeanCurrentlyInCreationException: Error creating bean with name 'circularDependencyA': Requested bean is currently in creation: Is there an unresolvable circular reference?

Solution

Circular dependency occurs because of design problems, the best approach is to redesign .

In the actual development, often reinvent the wheel is not allowed, so there are several remedies.

1. switch to setter injection method (recommended)

Constructor with different injection, setteris injected as needed, and allows for the dependent objects null;

@Component
public class CircularDependencyA {
 
    private CircularDependencyB circB;
 
    @Autowired
    public void setCircB(CircularDependencyB circB) {
        this.circB = circB;
    }
 
    public CircularDependencyB getCircB() {
        return circB;
    }
}
@Component
public class CircularDependencyB {
 
    private CircularDependencyA circA;
 
    private String message = "Hi!";
 
    @Autowired
    public void setCircA(CircularDependencyA circA) {
        this.circA = circA;
    }
 
    public String getMessage() {
        return message;
    }
}

Add unit tests:

@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan("com.example.circulardependency.setter")
public class CirculardependencyApplicationTests {
    @Bean
    public CircularDependencyB getDependencyB() {
        return new CircularDependencyB();
    }

    @Bean
    public CircularDependencyA getDependencyA() {
        CircularDependencyA circularDependencyA = new CircularDependencyA();
        circularDependencyA.setCircularDependencyB(getDependencyB());
        return circularDependencyA;
    }

    @Test
    public void contextLoads() {
        System.out.println("Hello world.");
        CircularDependencyA circularDependencyA = getDependencyA();
        System.out.println(circularDependencyA.getCircularDependencyB().getMessage());
    }
}

2. @Lazy comment

@LazyLazy Initialization. In the present embodiment, after CircularDependencyA will first build is completed, then build CircularDependencyB, breaking the dependency circle.

@Component
public class CircularDependencyA {
 
    private CircularDependencyB circB;
 
    @Autowired
    public CircularDependencyA(@Lazy CircularDependencyB circB) {
        this.circB = circB;
    }
}

3. 使用ApplicationContextAware, InitializingBean

ApplicationContextAware acquisition SpringContext, for loading bean; InitializingBean defines the operation after setting the Bean property.

@Component
public class CircularDependencyA implements InitializingBean, ApplicationContextAware {
    private CircularDependencyB circB;
    private ApplicationContext context;
    
    @Override
    public void afterPropertiesSet() throws Exception {
        this.circB = context.getBean(CircularDependencyB.class);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public CircularDependencyB getCircularDependencyB() {
        return circB;
    }
}

Principle: Life cycle Spring Bean with about: first instantiated, then call setApplicationContext (), afterPropertiesSet (); therefore, CircularDependencyB has been loaded after completion, and then load CircularDependencyA.
Found CircularDependencyB has not been loaded, loading CircularDependencyB first (calling the constructor, since at this time the CircularDependencyA instantiated, it is possible to smoothly load), set properties circB, CircularDependencyA loading is completed if, CircularDependencyA to instantiate call afterPropertiesSet ().
 

17, Spring MVC framework

Front and back pages of data processing request

18, spring design patterns used

https://blog.csdn.net/Dwade_mia/article/details/78883045

Published 468 original articles · won praise 95 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_37909508/article/details/100048645