Spring簡單總結part1

 

Spring簡單總結

1. Spring的概述(抄的百科) 

Spring的核心是控制反轉(IoC)和麪向切面(AOP)。簡單來講,Spring是一個分層的"一站式" 輕量級開源框架 spring

分層:EE發中三層結構.(一站式:Spring框架提供了EE開發每一層的解決方案.) express

              * WEB: Spring MVC. apache

              * 業務層: SpringBean管理. 編程

              * 持久層: SpringJdbcTemplate, orm模塊用於整合其餘的持久層框架. 數組

2. Spring的優勢

方便解耦,簡化開發 app

Spring就是一個大工廠,能夠將全部對象建立和依賴關係維護,交給Spring管理 框架

AOP編程的支持 dom

Spring提供面向切面編程,能夠方便的實現對程序進行權限攔截、運行監控等功能 ide

聲明式事務的支持 測試

只須要經過配置就能夠完成對事務的管理,而無需手動編程

方便程序的測試

SpringJunit4支持,能夠經過註解方便的測試Spring程序

方便集成各類優秀框架

Spring不排斥各類優秀的開源框架,其內部提供了對各類優秀框架(如:StrutsHibernateMyBatisQuartz等)的直接支持

下降JavaEE API的使用難度

Spring JavaEE開發中很是難用的一些APIJDBCJavaMail、遠程調用等),都提供了封裝,使這些API應用難度大大下降

3. Spring的開發包

3.1 開發包的目錄結構

docs       :     Spring的開發文檔

libs:        Spring的開發包.

schema:  約束文檔.

3.2 HelloWorld必備包

context          beans            core        expression

org.apache.commons.logging  +  log4j

4. HelloWorld案例

4.1 準備配置文件

src目錄下,建立名爲 applicationContextxml文件 (注意,文件圖標會變成小樹葉)

4.2 引入配置文件的約束文件

按如下步驟:

 windowèpreferenceè搜索catalogèaddèfile systemè查找約束文件èkey typeèSchema Locationèkeyè原有內容後添加約束文件名èOKè至此,引入約束文件成功

4.3 在配置文件中添加約束

打開applicationContext.xm文件è添加<beans></beans>節點è切換到design視圖è右鍵èedit namespaceèaddèxsièOKè繼續addèspecify new NamespaceèLocation HintèBrowserè選擇上一步引入的約束文件èOKèprefix能夠爲空ènamespace的值可 複製Location Hint中最後一個」/」前的內容èOKè從新打開applicationContext.xml,測試,有提示!OK

最終效果:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns="http://www.springframework.org/schema/beans"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

</beans>

4.4 準備實體類

public class Car {

       private String name;                    //準備屬性

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       @Override

       public String toString() {

              return "Car [name=" + name + "]";

       }

}

4.5 在配置文件中進行配置

<bean name="car" class="cn.xiaoge.domain.Car">

              <property name="name" value="QQ"></property>

       </bean>

4.6 在測試類中進行測試

@Test

       public void fun1(){

              //1. 先加載配置信息,其中會將配置文件中配置的bean初始化

ClassPathXmlApplicationContext  ac =

new ClassPathXmlApplicationContext("applicationContext.xml");

              //2. 獲取相應的對象

Car car = (Car) ac.getBean("car");

//3. 測試屬性注入是否成功

              System.out.println(car);

       }

5. 屬性注入

5.1 構造方法的屬性注入

<bean name="em" class="cn.xiaoge.domain.Employee" >

              <!--

                     利用構造方法爲屬性賦值

                     name:  構造方法的參數名

                     value: 構造方法中該參數的值

                     ref:   構造方法中參數爲引用類型時的值

                     type:  構造方法中參數的數據類型

                     index: 該參數在構造方法的參數列表中的索引

               -->

              <constructor-arg name="name" value="xiaoxiao" type="String"></constructor-arg>

              <constructor-arg name="car" ref="car" ></constructor-arg>

       </bean>

5.2 Set方法的屬性注入

<bean name="em" class="cn.xiaoge.domain.Employee">

             

              <!-- value主要負責基本數據類型的賦值操做.會覆蓋構造方法中的賦值 -->

              <property name="name" value="xiaogezi" />

       <!-- ref主要負責引用數據類型的賦值操做;另外,該引用類型的類必須在spring配置文件中定義 -->

              <property name="car" ref="car"></property>

             

       </bean>

5.3 p名稱空間的注入

首先,在約束頭中添加:

       xmlns:p=http://www.springframework.org/schema/p

其次,添加屬性值

<bean name="car" class="cn.xiaoge.domain.Car" p:name="QQ">

</bean>

5.4 SpEL的屬性注入

       SpEL:  Spring Expression Language

       //注意要爲bean設置id,這樣才能在el表達式中用#{Beanid}

<bean id="carInfo" class="cn.itcast.spring.demo6.CarInfo">

       </bean>

      

       <!-- SpEL的方式的屬性注入 -->

       <bean id="car2" class="cn.itcast.spring.demo6.Car2">

              <property name="name" value="#{carInfo.carName}"/>

              <property name="price" value="#{carInfo.calculatorPrice()}"/>

       </bean>

       <bean id="employee" class="cn.itcast.spring.demo6.Employee">

              <property name="name" value="abc "/>

              <property name="car2" value="#{car2}"/>

       </bean>

5.5. 爲各種集合注入

<!-- 集合注入 -->

       <bean name="mylist" class="cn.xiaoge.domain.MyList">

              <!-- List集合注入 -->

              <property name="list"  >

                     <list>

                            <value>123</value>

                            <ref bean="car"/>

                     </list>

              </property>

              <!-- set集合注入 -->

              <property name="set">

                     <set>

                            <value>xiaogezi</value>

                            <ref bean="car"/>

                     </set>          

              </property>

              <!-- 爲數組注入 -->

              <property name="arr">

                     <array>

                            <value>123</value>

                            <ref bean="car"/>

                     </array>

              </property>

              <!-- map注入 -->

                     <property name="map">

                     <map>

                            <entry key="name" value="xiaoge"></entry>

                            <entry key="car" value-ref="car"></entry>

                     </map>

              </property>

       <!-- Properties 注入 -->

              <property name="prop">

                     <props>

                            <prop key="username">123</prop>

                            <prop key="password">1234</prop>

                     </props>

              </property>

    </bean>

相關文章
相關標籤/搜索