Fourth, the introduction and DI in Spring IoC

A, and inversion control concept of dependency injection

1.1, the control inverted relationship with dependency injection

The core IoC dependency injection is designed to provide a simpler mechanism to set the component dependencies (commonly referred to as the object of collaborators), and manage the entire life cycle of these dependencies. IoC can be divided into dependency injection and dependent lookup , dependency injection DI is, so when talking about IoC DI always talk about, but when talking about IoC, not always talking about DI (dependent lookup is also a form of IoC)

1.2, the control inverted type
  • Dependent lookup comprising dependent pulling (dependency pull, DL), and context-dependent lookup (contextualized dependecy lookup, CDL) two types
  • Dependency injection comprising constructors and setter dependency injection in two ways
1.3, the pull-dependent
  • The JNDI : (the Naming and Directory Interface the Java, the Java Naming and Directory Interface)
    dependent on the following exemplary pull
public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/app-conytext.xml");
        DneOrderService dneOrderService = (DneOrderService) context.getBean("dneOrderService");
        //或者
        DneOrderService dneOrderService = context.getBean("dneOrderService",DneOrderService.class);
}
15387780-56a565d805f2fa5c.png
By pulling JNDI lookup dependency .png
1.4, context-dependent lookup

Find depend on the context-dependent pull difference is that queries directly from the container (such as a code Map), rather than from a registry (spring configuration file)
please reference P24

15387780-bf99c15a635c7b93.png
Context-dependent lookup .png
1.5, dependency injection manner
Constructor injection
    private DneMqProductMapper dneMqProductMapper;
    public DneMqProductServiceImpl(DneMqProductMapper dneMqProductMapper) {
        this.dneMqProductMapper = dneMqProductMapper;
    }
setter injection
    private DneMqProductMapper dneMqProductMapper;
    public void setDneMqProductMapper(DneMqProductMapper dneMqProductMapper) {
        this.dneMqProductMapper = dneMqProductMapper;
    }

In Spring, also supports the injection type is called a field injection (field injection), and will be introduced later mentioned @Autowire

1.6, and injected Find
  • Use the injection easier than using the Find
  • Injection can be freely used IoC completely separate class, to find always dependent on the container class and interface definitions.
  • 基于查找的解决方案比基于注入放入解决方案更复杂
  • 使用注入对组件代码没有影响,而查找会与之进行交互
  • 只要可以使用依赖注入,就尽量使用依赖注入
1.7、Spring 中的控制反转
15387780-7b1c8c215ccc7661.png
Spring的依赖注入机制.png

二、Spring中的依赖注入

2.1、Bean与BeanFactory
  • Spring依赖注入的核心是BeanFactory接口,它负责管理组件,包括依赖项以及它们的生命周期。
  • bean用于引用由容器管理的任何组件,某种程度上遵守JavaBean规范(但不是必须的)
  • BeanFactory可以通过编程方式配置,但通常做法是通过外部配置文件
2.2、BeanFactory的实现

bean的实现(详情请参考书P29)

15387780-902249f955c2cc15.png
image.png

BeanFactory的实现

15387780-4a068a67f21c7d9a.png
image.png

XML配置文件的实现

15387780-cdf669e4c133e6de.png
image.png

2.3、ApplicationContext及其配置

ApplicationContext:ApplicationContext 接口是对 BeanFactory 的一个拓展,除了DI ,还包括事务,AOP服务、国际化(Internationalization:i18n)、应用程序事件处理等。

Spring配置选项(见书P31 ~ P)
  • 使用注解(从jdk5和Spring 2.5开始)
  • 使用xml配置文件
  • 基本配置描述
  • 声明Spring组件(GenericXmlApplicationContext)
  • 使用Java配置(@Bean,@Configuration,@ImportResource,@Resource(name=""),@Inject,AnnotationConfigurationApplicationContext)
  • 使用setter注入(Spring 2.5以后,支持 p 名称空间)
  • 使用构造函数注入(Spring 3.1开始支持 c 名称空间,@Value)
  • Using field injection (note the single responsibility principle: that a class make a thing, high cohesion)
  • The use of injection parameters
  • Injection of simple values
  • Value by using injection SpEL
  • Bean injection unit in the same XML
  • Inject collection
  • Use the injection method
    • Find Method Injection
    • Find Notes injection method
    • Alternative methods
    • When to use alternative methods
  • Learn bean named
    • bean alias name
    • Bean using annotation configuration naming
  • Learn bean instantiation mode
    • Examples of mode selection
    • Implementation bean scopes
2.4 Analytical dependencies
2.5, and automatic assembly disposed bean bean inheritance
  • Automatic assembly mode
  • When using automatic assembly

Third, the configuration Spring application context


Above are from the "Spring 5 Advanced Programming fifth edition of" Tsinghua University Press Chapter III introduced in Spring IoC and DI

Reproduced in: https: //www.jianshu.com/p/7ce0bd43e0a5

Guess you like

Origin blog.csdn.net/weixin_33755557/article/details/91272768