JAVAEE——spring01:介紹、搭建、概念、配置詳解、屬性注入和應用到項目


一、spring介紹

  1.三層架構中spring位置

  

  2.spring一站式框架

  正是因為spring框架性質是屬於容器性質的.

  容器中裝什么對象就有什么功能.所以可以一站式.

  不僅不排斥其他框架,還能幫其他框架管理對象.

  aop支持、ioc思想、spring jdbc、aop 事務、junit 測試支持

 

二、spring搭建

  1.導包

  

  

  日志包:com.springsource.org.apache.commons.logging-1.1.1.jar

  可選:com.springsource.org.apache.log4j-1.2.15.jar(老版本要導入的,導入可以保證一定能運行)

 

  2.創建一個對象

public class User {
    private String name;
    private Integer age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

 

  3.書寫配置注冊對象到容器

  位置任意(建議放到src下)
  配置文件名任意(建議applicationContext.xml)

  導入約束:

  

  然后編輯applicationContext.xml,加入<beans></beans>后切換帶設計視圖

  

  進入編輯后點擊add,導入xsi

  

  添加完xsi后,再次點擊add,指定一個新的命名空間

  

  然后選擇剛剛導入的xsd

  

  點擊OK,回到剛剛的頁面,設置命名空間的名字(可以直接復制location Hint的前半段),prefix空着即可

  

  點擊OK,顯示為下面的界面,就說明導入成功了。

  

  書寫applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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-4.2.xsd ">

    <!-- 將User對象交給spring容器管理 -->
    <bean  name="user" class="cn.itcast.bean.User" ></bean>
    
</beans>

 

  4.代碼測試

    @Test
    public void fun1(){    
        //1 創建容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2 向容器"要"user對象
        User u = (User) ac.getBean("user");
        //3 打印user對象
        System.out.println(u);
    }

 

三、spring概念

  1.思想

   1.1 ioc

  

   1.2 di

  

 

  2.applicationContext&BeanFactory

   2.1 BeanFactory接口

   spring原始接口.針對原始接口的實現類功能較為單一;

   BeanFactory接口實現類的容器.特點是每次在獲得對象時才會創建對象。

   2.2 ApplicationContext

   每次容器啟動時就會創建容器中配置的所有對象.並提供更多功能。

   從類路徑下加載配置文件:ClassPathXmlApplicationContext

   2.3 結論

   結論:web開發中,使用applicationContext. 在資源匱乏的環境可以使用BeanFactory.

 

四、spring配置詳解

  1.Bean元素

    <!-- 將User對象交給spring容器管理 -->
    <!-- Bean元素:使用該元素描述需要spring容器管理的對象
            class屬性:被管理對象的完整類名.
            name屬性:給被管理的對象起個名字.獲得對象時根據該名稱獲得對象.  
                    可以重復.可以使用特殊字符.
            id屬性: 與name屬性一模一樣. 
                    名稱不可重復.不能使用特殊字符.
            結論: 盡量使用name屬性.
      -->
    <bean  name="user" class="cn.itcast.bean.User" ></bean>

 

  2.Bean元素進階

   2.1 scope屬性

   singleton(默認值):單例對象.被標識為單例的對象在spring容器中只會存在一個實例

   prototype:多例原型.被標識為多例的對象,每次再獲得才會創建.每次創建都是新的對象.整合struts2時,ActionBean必須配置為多例的.

   request:web環境下.對象與request生命周期一致.

   session:web環境下,對象與session生命周期一致.

   2.2 生命周期屬性

   init-method:配置一個方法作為生命周期初始化方法.spring會在對象創建之后立即調用.

   destory-method:配置一個方法作為生命周期的銷毀方法.spring容器在關閉並銷毀所有容器中的對象之前調用.

    <bean  name="user" class="cn.itcast.bean.User"
         init-method="init" destroy-method="destory" ></bean>

 

  3.spring創建對象的方式

    <!-- 創建方式1:空參構造創建(重點)  -->
    <bean  name="user" class="cn.itcast.bean.User"
         init-method="init" destroy-method="destory" ></bean>
         
    <!-- 創建方式2:靜態工廠創建 (了解)
          調用UserFactory的createUser方法創建名為user2的對象.放入容器
     -->
    <bean  name="user2" 
        class="cn.itcast.b_create.UserFactory" 
        factory-method="createUser" ></bean>
        
    <!-- 創建方式3:實例工廠創建 (了解)
         調用UserFactory對象的createUser2方法創建名為user3的對象.放入容器
     -->
    <bean  name="user3" 
        factory-bean="userFactory"
        factory-method="createUser2" ></bean>
        
    <bean  name="userFactory" 
        class="cn.itcast.b_create.UserFactory"   ></bean>

 

  4.spring的分模塊配置

    <!-- 導入其他spring配置文件 -->
    <import resource="cn/itcast/b_create/applicationContext.xml"/>

 

五、spring屬性注入

  1.注入方式

   1.1 set方法注入(重中之重)

    <!-- set方式注入: -->
    <bean  name="user" class="cn.itcast.bean.User" >
        <!--值類型注入: 為User對象中名為name的屬性注入tom作為值 -->
        <property name="name" value="tom" ></property>
        <property name="age"  value="18" ></property>
        <!-- 引用類型注入: 為car屬性注入下方配置的car對象 -->
        <property name="car"  ref="car" ></property>
    </bean>
    
    <!-- 將car對象配置到容器中 -->
    <bean name="car" class="cn.itcast.bean.Car" >
        <property name="name" value="蘭博基尼" ></property>
        <property name="color" value="黃色" ></property>
    </bean>

   1.2 構造函數注入(重點)

    <!-- 構造函數注入 -->
<bean name="user2" class="cn.itcast.bean.User" >
    <!-- name屬性: 構造函數的參數名 -->
    <!-- index屬性: 構造函數的參數索引 -->
    <!-- type屬性: 構造函數的參數類型-->
    <constructor-arg name="name" index="0" type="java.lang.Integer" value="999"  ></constructor-arg>
    <constructor-arg name="car" ref="car" index="1" ></constructor-arg>
</bean>

   1.3 p名稱空間注入

<!-- p名稱空間注入, 走set方法
    1.導入P名稱空間  xmlns:p="http://www.springframework.org/schema/p"
    2.使用p:屬性完成注入
        |-值類型: p:屬性名="值"
        |-對象類型: p:屬性名-ref="bean名稱"
 -->
    <bean  name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car"  >
    </bean>

   1.4 spel注入

<!-- 
    spel注入: spring Expression Language sping表達式語言
 -->
<bean  name="user4" class="cn.itcast.bean.User" >
        <property name="name" value="#{user.name}" ></property>
        <property name="age" value="#{user3.age}" ></property>
        <property name="car" ref="car" ></property>
</bean>

 

  2.復雜類型注入

   2.1 數組

    <!-- 如果數組中只准備注入一個值(對象),直接使用value|ref即可 
    <property name="arr" value="tom" ></property>
    -->
    <!-- array注入,多個元素注入 -->
    <property name="arr">
        <array>
            <value>tom</value>
            <value>jerry</value>
            <ref bean="user4" />
        </array>
    </property>

   2.2 List

    <!-- 如果List中只准備注入一個值(對象),直接使用value|ref即可 
    <property name="list" value="jack" ></property>-->
    <property name="list"  >
        <list>
            <value>jack</value>
            <value>rose</value>
            <ref bean="user3" />
        </list>
    </property>

   2.3 Map

    <!-- map類型注入 -->
    <property name="map"  >
        <map>
            <entry key="url" value="jdbc:mysql:///crm" ></entry>
            <entry key="user" value-ref="user4"  ></entry>
            <entry key-ref="user3" value-ref="user2"  ></entry>
        </map> 
    </property>

   2.4 Properties

    <!-- prperties 類型注入 -->
    <property name="prop"  >
        <props>
            <prop key="driverClass">com.jdbc.mysql.Driver</prop>
            <prop key="userName">root</prop>
            <prop key="password">1234</prop>
        </props>
    </property>

 

六、練習:將spring容器應用到struts-crm項目

  管理Service對象以及Dao對象

  1.導包(4+2),再加1

   再加1指的是:spring-web-4.2.4.RELEASE.jar(因為要用到web的監聽)

 

  2.將Service對象以及Dao對象配置到spring容器(注入屬性記得加相應的set方法)

<?xml version="1.0" encoding="UTF-8"?>
<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-4.2.xsd ">

    <!-- 配置Dao -->
    <bean name="customerDao" class="cn.itheima.dao.impl.CustomerDaoImpl" ></bean>
    <bean name="linkManDao" class="cn.itheima.dao.impl.LinkManDaoImpl" ></bean>
    <bean name="userDao" class="cn.itheima.dao.impl.UserDaoImpl" ></bean>
    <!-- 配置Service -->
    <bean name="customerService" class="cn.itheima.service.impl.CustomerServiceImpl" >
        <property name="customerDao" ref="customerDao" ></property>
    </bean>
    <bean name="linkManService" class="cn.itheima.service.impl.LinkManServiceImpl" >
        <property name="cd" ref="customerDao" ></property>
        <property name="lmd" ref="linkManDao" ></property>
    </bean>
    <bean name="userService" class="cn.itheima.service.impl.UserServiceImpl" >
        <property name="ud" ref="userDao" ></property>
    </bean>

</beans>

 

  3.在Action中獲得容器中的Service對象

   3.1 web.xml中配置容器隨項目啟動

  <!-- 可以讓spring容器隨項目的啟動而創建,隨項目的關閉而銷毀 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 指定加載spring配置文件的位置 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

   3.2 在Action中獲得容器

        //獲得spring容器=>從Application域獲得即可
        
        //1 獲得servletContext對象
        ServletContext sc = ServletActionContext.getServletContext();
        //2.從Sc中獲得ac容器
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
        //3.從容器中獲得CustomerService
        UserService us = (UserService) ac.getBean("userService");

 

  4.管理容器在項目中的生命周期

   下面錯誤的示范.導致每次請求都創建新的容器

        //創建容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //獲得cs(customerService對象)
        CustomerService cs = (CustomerService) ac.getBean("customerService");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM