Spring点滴二:Spring Bean

本文详细介绍了Spring框架中的核心概念——Bean的定义、配置和管理方式。Bean作为Spring IoC容器管理的对象,其创建过程依赖于配置元数据,如XML中的<bean>定义。文章还探讨了Bean的属性,包括id、class、scope等,并举例说明了如何定义带有初始化方法和销毁方法的Bean。

Spring Bean:

   被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理。bean是一个被实例化,配置、组装并由Spring Ioc容器管理对象。

   官网API:A Spring IoC container manages one or more beans. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions.

   翻译:一个Spring IoC容器管理一个或多个beans。这些benas的创建是由配置元数据提供到容器里。例如:XML 表单中<bean>的定义

 

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 定义一个bean -->
    <bean id="...." class="....">
    </bean>
</beans>

<bean>标签包含属性详解:

 id/name 用来指定bean唯一标识符,在基于XML配置元数据中,通过id或那么来指定bean唯一标识符    
 class 该属性是强制性的,用来指定bean所对应的Java POJO类
 scope 用来指定bean对象的作用域
 constructor-arg 用来注入依赖关系的
 properties 用来注入依赖关系的
 autowiring mode 用来注入依赖关系的
 lazy-initialization mode 该bean是延迟初始化的,告诉Spring容器该bean不是在容器启动时初始化的而是第一次被请求时才初始化
 initialization 方法 该bean被初始化时要调用的方法
 destruction 方法 该bean被销毁时要调用的回调方法

 

   

 

 

 

 

 

 

 

 

 

这个配置文件中有不同的 bean 定义,包括延迟初始化,初始化方法和销毁方法的:

<?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
    http://www.springframework.org/schema/beans/spring-beans.xsd">

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

   <!-- A bean definition with lazy init set on -->
   <bean id="..." class="..." lazy-init="true">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with initialization method -->
   <bean id="..." class="..." init-method="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- A bean definition with destruction method -->
   <bean id="..." class="..." destroy-method="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

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

</beans>

 

   

转载于:https://www.cnblogs.com/sishang/p/6565546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值