首页 > 其他分享 >对IoC容器和Bean的学习笔记

对IoC容器和Bean的学习笔记

时间:2023-08-05 22:34:09浏览次数:32  
标签:容器 container Spring bean Bean beans configuration IoC metadata

What We Mean by "Spring"

The term "Spring" means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say "Spring", they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself.

Introduction

IoC,the Inversion of Control. IoC is also known as dependency injection (DI). (依赖注入实现了控制反转,为此Spring设计了The IoC Container这项技术)

It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.

这段描述是关于"依赖注入"(Dependency Injection,DI)的解释。依赖注入是软件工程中的一种设计模式,它通过构造函数参数、工厂方法的参数或者在对象实例构造或从工厂方法返回后设置的属性来定义对象的依赖关系。容器在创建对象时注入这些依赖。这个过程本质上是对象本身通过直接构造类或使用服务定位器模式等机制来控制其依赖项的实例化或定位的反转(因此得名“控制反转”)。

传统上,对象可能会直接实例化或定位其所依赖的对象,导致紧耦合,并且难以修改或测试单个组件。依赖注入通过将依赖管理的责任从对象本身移交给外部容器或框架来解决这个问题。

应用依赖注入,对象可以更具可重用性和可维护性,因为它们只关注履行自己的职责,而依赖关系从外部进行注入。这样可以更好地分离关注点,并且可以更容易地修改依赖关系,而无需修改依赖对象。

通过依赖注入实现的控制反转还有助于测试,因为在单元测试期间,可以轻松地使用模拟对象替代依赖项,实现对各个组件的隔离测试。

总体而言,依赖注入是一种强大的技术,通过将依赖的创建和管理从依赖对象本身解耦,促进模块化、松耦合和可测试的软件设计。

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container.

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Container Overview

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.

2023-07-20-19-08-30-image

Configuration Metadata

The configuration metadata is represented in XML, Java annotations, or Java code. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.

XML-based metadata is not the only allowed form of configuration metadata. Other forms of metadata with the Spring container, see:

XML-based configuration metadata configures these beans as <bean/> elements inside a top-level <beans/> element. Java configuration typically uses @Bean-annotated methods within a @Configuration class.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="..." class="...">
		<!-- collaborators and configuration for this bean go here -->
	</bean>

	<bean id="..." class="...">
		<!-- collaborators and configuration for this bean go here -->
	</bean>

	<!-- more bean definitions go here -->

</beans>

Instantiating a Container

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- services -->

	<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
		<property name="accountDao" ref="accountDao"/>
		<property name="itemDao" ref="itemDao"/>
		<!-- additional collaborators and configuration for this bean go here -->
	</bean>

	<!-- more bean definitions for services go here -->

</beans>

The property name element refers to the name of the JavaBean property, and the ref element refers to the name of another bean definition.

This linkage between id and ref elements expresses the dependency between collaborating objects.

daos.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="accountDao"
		class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
		<!-- additional collaborators and configuration for this bean go here -->
	</bean>

	<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
		<!-- additional collaborators and configuration for this bean go here -->
	</bean>

	<!-- more bean definitions for data access objects go here -->

</beans>

Composing XML-based Configuration Metadata

Often, each individual XML configuration file represents a logical layer or module in your architecture.

Use one or more occurrences of the <import/> element to load bean definitions from another file or files. The following example shows how to do so:

<beans>
	<import resource="services.xml"/>
	<import resource="resources/messageSource.xml"/>
	<import resource="/resources/themeSource.xml"/>

	<bean id="bean1" class="..."/>
	<bean id="bean2" class="..."/>
</beans>

The Groovy Bean Definition DSL

beans {
	dataSource(BasicDataSource) {
		driverClassName = "org.hsqldb.jdbcDriver"
		url = "jdbc:hsqldb:mem:grailsDB"
		username = "sa"
		password = ""
		settings = [mynew:"setting"]
	}
	sessionFactory(SessionFactory) {
		dataSource = dataSource
	}
	myService(MyService) {
		nestedBean = { AnotherBean bean ->
			dataSource = dataSource
		}
	}
}

Using the Container

By using the method T getBean(String name, Class<T> requiredType), you can retrieve instances of your beans.

// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);

// use configured instance
List<String> userList = service.getUsernameList();

Indeed, your application code should have no calls to the getBean() method at all and thus have no dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides dependency injection for various web framework components such as controllers and JSF-managed beans, letting you declare a dependency on a specific bean through metadata (such as an autowiring annotation).

Bean Overview

Beans are created with the configuration metadata that you supply to the container (for example, in the form of XML <bean/> definitions).

Class只是Class,但是如果配置到了XML的<bean/>,那么它就成为了the Spring Bean。

BeanDefinition:

  • A package-qualified class name: typically, the actual implementation class of the bean being defined.

  • Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).

  • References to other beans that are needed for the bean to do its work. These references are also called collaborators or dependencies.

  • Other configuration settings to set in the newly created object — for example, the size limit of the pool or the number of connections to use in a bean that manages a connection pool.

Naming Beans

In XML-based configuration metadata, you use the id attribute, the name attribute, or both to specify bean identifiers.

Aliasing a Bean outside the Bean Definition

<alias name="fromName" alias="toName"/>

Instantiating Beans

If you use XML-based configuration metadata, you specify the type (or class) of object that is to be instantiated in the class attribute of the <bean/> element.

Instantiation with a Constructor

<bean id="exampleBean" class="examples.ExampleBean"/>

<bean name="anotherExample" class="examples.ExampleBeanTwo"/>

Instantiation with a Static Factory Method

<bean id="clientService"
	class="examples.ClientService"
	factory-method="createInstance"/>
public class ClientService {
	private static ClientService clientService = new ClientService();
	private ClientService() {}

	public static ClientService createInstance() {
		return clientService;
	}
}

Instantiation by Using an Instance Factory Method

<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
	<!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="clientService"
	factory-bean="serviceLocator"
	factory-method="createClientServiceInstance"/>
public class DefaultServiceLocator {

	private static ClientService clientService = new ClientServiceImpl();

	public ClientService createClientServiceInstance() {
		return clientService;
	}
}

In Spring documentation, "factory bean" refers to a bean that is configured in the Spring container and that creates objects through an instance or static factory method.

Determining a Bean’s Runtime Type

The recommended way to find out about the actual runtime type of a particular bean is a BeanFactory.getType call for the specified bean name.

参考资料:

The IoC Container https://docs.spring.io/spring-framework/reference/core/beans.html

标签:容器,container,Spring,bean,Bean,beans,configuration,IoC,metadata
From: https://www.cnblogs.com/df888/p/17608772.html

相关文章

  • Spring源码之XML文件中Bean标签的解析1
    读取XML文件,创建对象xml文件里包含Bean的信息,为了避免多次IO,需要一次性读取xml文件中所有bean信息,加入到Spring工厂。读取配置文件newClassPathResource("applicationContext.xml")ClassPathResource是Spring封装的一个类型;Resource接口:可以读取相关资源文件的内容获得......
  • Vue封装一个瀑布流图片容器组件
    说在前面......
  • 参考示例之“复制对象|拷贝对象|BeanUtils工具类学习”
    //设置需要拷贝的字段Set<String>targetSet=newHashSet<>();targetSet.addAll(Arrays.asList("totalRefund","actualAdvertisingCost","expensesOfTaxation"));//调用拷贝方法copyProperties(com......
  • springboot 关于servlet容器配置修改 组件注册 容器切换 使用外部tomcat
    1.嵌入式Servlet容器配置修改1.通过全局配置文件修改可以通过server.xxx来进行web服务配置,没有带服务器名称的则是通用配置通过带了具体的服务器名称则是单独对该服务器进行设置,比如server.tomcat.xxx就是专门针对tomcat的配置2.通过WebServerFactoryCus......
  • day124 - 基于注解管理bean
    基于注解管理bean注解和XML配置文件一样,注解本身并不能执行,注解本身仅仅只是做一个标记,具体的功能是框架检测到注解标记的位置,然后针对这个位置按照注解标记的功能来执行具体操作。本质上:所有一切的操作都是Java代码来完成的,XML和注解只是告诉框架中的Java代码如何执行。S......
  • day 122 - bean的作用域,生命周期,工厂模式
    bean的作用域在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围singleton(默认)在IOC容器中,这个bean的对象始终为单实例在ioc容器初始化时创建对象prototype这个bean在IOC容器中有多个实例在获取bean时创建对象<!--scope设置bean的作用域:......
  • java--并发容器 ConcurrentMap
    在JDK1.4以下只有Vector和Hashtable是线程安全的集合(也称并发容器,Collections.synchronized*系列也可以看作是线程安全的实现)。从JDK5开始增加了线程安全的Map接口ConcurrentMap和线程安全的队列BlockingQueue(尽管Queue也是同时期引入的新的集合,但是规范并没有规定一定是线程安全......
  • Spring 容器里 Bean 生命周期中可扩展的 SpringBoot 接口
    Gitee:Demo源码1.ApplicationContextInitializer这是整个spring容器在刷新之前初始化ConfigurableApplicationContext的回调接口。@Slf4jpublicclassTestApplicationContextInitializerimplementsApplicationContextInitializer{@Overridepublicvoidi......
  • 记录一下【docker compose发布】 docker容器间通信
    踩坑:在网上找的帖子说是在dockercompose文件中的network下添加 1:直接报错, 2:然后又去查找说要在compose文件底部,和service同级添加network的声明,添加后还是不能通信,然后使用命令dockernetworkls查看发现新增了两个网络sub_test,pub_test 3:重新查找,使用已有网络需......
  • Spring-1-透彻理解Spring XML的Bean创建--IOC
    学习目标上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,实现IOC和DI,今天具体来讲解IOC能够说出IOC的基础配置和Bean作用域了解Bean的生命周期能够说出Bean的实例化方式一、Bean的基础配置问题导入问题1:在<bean>标签上如何配置别名......