Spring (03) Spring IOC Overview

Spring (03) Spring IOC Overview

Spring core programming ideas catalog: https://www.cnblogs.com/binarylei/p/12290153.html

1. Spring IoC-dependent lookup

Find the name according to Bean 1.1

  • Find real-time

    User user = (User) beanFactory.getBean("user");
  • Find delayed

    bean configuration is as follows:

    <bean id="user1" class="com.binarylei.spring.ioc.bean.User" lazy-init="true"/>
    <bean id="user2" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
        <property name="targetBeanName" value="user1"/>
    </bean>

    It will delay loading bean by bean getting ObjectFactory, only manually invoke getObject method will initialize the User object. The User is set to lazy-init = "true" effect will be more obvious, Spring will be initialized by default to all non-lazy loaded bean.

    ObjectFactory<User> objectFactory = (ObjectFactory) beanFactory.getBean("user2");
    User user = objectFactory.getObject();

Thinking: the ObjectFactory and FactoryBean are by getObject create objects, they have what difference does it make?

  1. ObjectFactory: it belongs lazy loading bean, only manually invoke getObject will initialize the bean.

  2. FactoryBean: When both load FactoryBean will call getObject initialize the object, not a lazy loading bean. If you want to get FactoryBean Spring in itself, can be loaded prior bean name &sign.

    FactoryBean<User> factoryBean = (FactoryBean<User>) beanFactory.getBean("&user");

Find according to the type of Bean 1.2

  • Individual objects Bean

    User user = beanFactory.getBean(User.class);
  • A collection of objects Bean

    Map<String, User> userMap = ((DefaultListableBeanFactory) beanFactory)
        .getBeansOfType(User.class);

Thoughts: BeanFactory.getBean (User.class), if the return multiple objects, Spring will be how to deal, how to solve?

Spring container when a plurality of User objects, Spring throws an exception NoUniqueBeanDefinitionException, the processing time can be labeled by primary.

<bean id="user1" class="com.binarylei.spring.ioc.bean.User" primary="true"/>

According to Bean 1.3 Find Name Type +

User user = beanFactory.getBean("user", User.class);

Find 1.4 based on Java annotations

Map<String, Object> beanMap = ((DefaultListableBeanFactory) beanFactory)
    .getBeansWithAnnotation(Component.class);

2. Spring IoC dependency injection

There UserManager class, how will users inject it?

@Data
public class UserManager {
    private BeanFactory beanFactory;
    private SuperUser superUser;
    private List<User> users;
} 

According to Bean name inject 2.1

  • Individual objects Bean

    <bean id="userManager" class="com.binarylei.spring.ioc.bean.UserManager">
        <property name="superUser" ref="user"/>
    </bean>
  • A collection of objects Bean

    <bean id="userManager" class="com.binarylei.spring.ioc.bean.UserManager">
        <property name="users">
            <util:list>
                <ref bean="user1"/>
                <ref bean="user2"/>
            </util:list>
        </property>
    </bean>

The type of injection Bean 2.2

<bean id="userManager" class="com.binarylei.spring.ioc.bean.UserManager" 
      autowire="byType"/>

2.3 Bean object built into the container

As Environment object.

Injecting a non-target Bean 2.4

As BeanFacory UserManager injected into the normal. If by beanFactory.getBean (BeanFactory.class) NoSuchBeanDefinitionException will throw an exception.

2.5 injection type

  • Real-time injection
  • Delayed implantation

2.6 Injection mode

  • Manual mode - configuration or programming mode, pre-arranged injection rules
    • XML meta-information resources
    • Java annotations configuration meta-information
    • API configuration meta-information
  • Automatic Mode - implementer provides automatic correlation dependent manner, built according to the rules of injection
    • Autowiring (automatic binding): no, byName, byType, constructor

3. Spring IoC dependence sources

  • Custom Bean
  • Bean container built-in objects
  • Built-dependent container

4. Spring IoC configuration meta information

  • Bean Custom Configuration
    • XML-based file
    • Based Properties Files
    • Based on Java annotations
    • Based on Java API (panel discussion)
  • IoC container configuration
    • XML-based file
    • Based on Java annotations
    • Based on Java API (panel discussion)
  • External attribute configuration
    • Based on Java annotations, @ Value Notes

5. Spring IoC container

  • Spring BeanFactory is IoC container bottom.
  • ApplicationContext BeanFactory is a superset of features with the application.

ApplicationContext IoC container in addition to the role, as well as to provide:

  • Aspect Oriented (AOP)
  • Meta configuration information (Configuration Metadata)
  • Resource Management (Resources)
  • Events (Events)
  • Internationalization (i18n)
  • Annotations (Annotations)
  • Environment 抽象(Environment Abstraction)
public interface ApplicationContext extends EnvironmentCapable,
     ListableBeanFactory, HierarchicalBeanFactory, MessageSource, 
    ApplicationEventPublisher, ResourcePatternResolver {
}

6. Spring Application Context

ApplicationContext

https://docs.spring.io/spring/docs/5.2.2.RELEASE/spring-framework-reference/core.html#beans-introduction

7. Spring IoC container life cycle

  • start up
  • run
  • stop
@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        prepareRefresh();
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
        prepareBeanFactory(beanFactory);
        try {
            postProcessBeanFactory(beanFactory);
            invokeBeanFactoryPostProcessors(beanFactory);
            registerBeanPostProcessors(beanFactory);
            initMessageSource();
            initApplicationEventMulticaster();
            onRefresh();
            registerListeners();
            finishBeanFactoryInitialization(beanFactory);
            finishRefresh();
        } catch (BeansException ex) {
            destroyBeans();
            cancelRefresh(ex);
            throw ex;
        } finally {
            resetCommonCaches();
        }
    }
}

8. face questions Featured

Question 1: What is the Spring IoC container?

Question 2: BeanFactory and FactoryBean?

  • BeanFactory is IoC container bottom.
  • FactoryBean is to create a way of Bean help achieve complex initialization logic.

Question 3: Spring IoC container prepared to do what when to start?

IoC configuration meta-information read and parse, IoC container lifecycle, Spring event publishing, internationalization, more answers will discuss each topic in subsequent chapters


The intentions of recording a little bit every day. Perhaps the content is not important, but the habit is very important!

Guess you like

Origin www.cnblogs.com/binarylei/p/12296907.html