- Spring的類包必須已經放在Spring的類容器下面
- 應用程序應當爲Spring提供完備的Bean的配置信息
- Bean的類都已經放在Spring的類容器下面
- Bean的實現類
- Bean的屬性信息 比如:數據源的連接數,用戶名和密碼等等。
- Bean的依賴關係 Spring根據依賴關係配置完成Bean之間的裝配
- Bean的行爲配置 比如:生命週期範圍以及生命週期各個過程的回調函數等
- Bean的創建方式定義 主要說明是通過構造器還是工廠方法來構造Bean
接下來是他們之間的相互關係:
- 在配置文件中使用import來導入所需的配置文件。
- 將多個配置文件構造爲一個數組,然後傳遞給ApplicationContext實現加載多個配置文件。
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]
{"Spring-Common.xml","Spring-Connection.xml","Spring-ModuleA.xml"});
<beans .....>
<import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="module/Spring-Module.xml"/>
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext(“Spring-All-Module.xml”);
<beans>//Bean定義的開始和結束
<import resource=“resource1.xml” />//導入其他配置文件Bean的定義
<import resource=“resource2.xml” />
<bean id=“bean1” class=“***”></bean>
<bean name=“bean2” class=“***”></bean>
<alias alias=“bean3” name=“bean2” />//alias用於定義Bean的別名
</beans>
- Import標籤可以放在Beans標籤下的任何位置,沒有順序關係。
- bean3和bean2是同一個Bean,bean3是bean2的別名。
<?xml version="1.0" encoding="UTF-8" ?>
<beans
<!--標準命名空間-->
xmlns="http://www.springframework.org/schema/beans"
<!--xsi標準命名空間,用於指定自定義命名空間的Schema文件-->
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!—aop表示自定義命名空間,aop是該命名空間的簡稱,而後面是命名空間的全稱。必須在xsi命名空間爲其指定的命名空間對應的Schema文件-->
xmlns:aop="http://www.springframework.org/schema/aop"
<!—下面這四行代碼,是爲每個命名空間指定具體的schema文件-->
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop-3.0.xsd">
<!—默認命名空間的配置 -->
<bean id = "app" class="cn.lovepi.App" />
<!—aop命名空間的配置 -->
<aop:config>
<aop:pointcut id="mycut" expression="execution(* cn.love*(..))"/>
</aop:config>
</beans>
- 首先我們定義了一個默認命名空間,他沒有空間名,用於Spring Bean的定義。
- 接下來我們命名了一個xsi命名空間,這個命名空間用於爲每個文檔中命名空間指定相對應的schema的樣式文件。是標準組織定義的標準命名空間。
- 我們還命名了一個aop的命名空間,這個命名空間是Spring配置aop的命名空間,是用戶自定義的命名空間。
- 指定命名空間的名稱,需要指定命名空間的縮類名和全名
- 指定命名空間的schema文檔樣式文件的位置,用空格或回車行來進行分割。
- xml解析器可以獲取schema文件,並對文檔進行格式合法性驗證
- 在開發環境下,IDE可以用schema文件來對文檔編輯器進行誘導功能。
- spring-beans-3.0.xsd:Spring3.0最主要的配置文件,主要是用於配置Bean
- spring-aop-3.0.xsd:aop配置定義的schema
- spring-tx-3.0.xsd:聲明式事物配置定義的Schema
- spring-mvc-3.0.xsd:Spring3.0當中新增的
- spring-util-3.0.xsd:是爲簡化某些複雜的標準配置而提供的Schema
- spring-jee-3.0.xsd:是爲簡化J2EE中EJB等功能的配置而提供的Schema
- spring-jdbc-3.0.xsd:爲Spring內接數據庫而提供的Schema,3.0新增
- spring-jms-3.0.xsd:jms配置的Schema
- spring-lang-3.0.xsd:增加了對動態語言的支持,爲集成動態語言而定義
- spring-oxm-3.0.xsd:配置對象xml映射到schema,3.0新增
- spring-task-3.0.xsd:任務調度的Schema
- spring-tool-3.0.xsd:爲集成Schema一些有用工具而提供的Schema