當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
Spring Bean 定义
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Spring Bean 定义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                轉載自? ?Spring Bean 定義
Bean 定義
被稱作 bean 的對象是構成應用程序的支柱也是由 Spring IoC 容器管理的。bean 是一個被實例化,組裝,并通過 Spring IoC 容器所管理的對象。這些 bean 是由用容器提供的配置元數據創建的,例如,已經在先前章節看到的,在 XML 的表單中的 定義。
bean 定義包含稱為配置元數據的信息,下述容器也需要知道配置元數據:
-  
如何創建一個 bean
 -  
bean 的生命周期的詳細信息
 - bean 的依賴關系
 
上述所有的配置元數據轉換成一組構成每個 bean 定義的下列屬性。
| class | 這個屬性是強制性的,并且指定用來創建 bean 的 bean 類。 | 
| name | 這個屬性指定唯一的 bean 標識符。在基于 XML 的配置元數據中,你可以使用 ID 和/或 name 屬性來指定 bean 標識符。 | 
| scope | 這個屬性指定由特定的 bean 定義創建的對象的作用域,它將會在 bean 作用域的章節中進行討論。 | 
| constructor-arg | 它是用來注入依賴關系的,并會在接下來的章節中進行討論。 | 
| properties | 它是用來注入依賴關系的,并會在接下來的章節中進行討論。 | 
| autowiring mode | 它是用來注入依賴關系的,并會在接下來的章節中進行討論。 | 
| lazy-initialization mode | 延遲初始化的 bean 告訴 IoC 容器在它第一次被請求時,而不是在啟動時去創建一個 bean 實例。 | 
| initialization 方法 | 在 bean 的所有必需的屬性被容器設置之后,調用回調方法。它將會在 bean 的生命周期章節中進行討論。 | 
| destruction 方法 | 當包含該 bean 的容器被銷毀時,使用回調方法。它將會在 bean 的生命周期章節中進行討論。 | 
Spring 配置元數據
Spring IoC 容器完全由實際編寫的配置元數據的格式解耦。有下面三個重要的方法把配置元數據提供給 Spring 容器:
-  
基于 XML 的配置文件。
 -  
基于注解的配置
 - 基于 Java 的配置
 
你已經看到了如何把基于 XML 的配置元數據提供給容器,但是讓我們看看另一個基于 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.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>你可以查看?Spring Hello World 實例?來理解如何定義,配置和創建 Spring Beans。
關于基于注解的配置將在一個單獨的章節中進行討論。刻意把它保留在一個單獨的章節,是因為我想讓你在開始使用注解和 Spring 依賴注入編程之前,能掌握一些其他重要的 Spring 概念。
總結
以上是生活随笔為你收集整理的Spring Bean 定义的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 3000元电脑配置推荐2022清单(30
 - 下一篇: 4k对电脑配置的要求(4k对电脑配置)