ssm系列之SpringIoc容器

一、簡介

1、ioc介紹
ioc是Inversion of Control的簡寫,中文名稱爲控制反轉,其底層爲反射。通過控制反轉,對象在被創建的時候,由一個調控系統內所有對象的外界實體將其所依賴的對象的引用傳遞給它。也可以說,依賴被注入到對象中。
2、ioc的類型
1)、類型1 (基於接口): 可服務的對象需要實現一個專門的接口,該接口提供了一個對象,可以重用這個對象查找依賴(其它服務)。早期的容器Excalibur使用這種模式。
2)、類型2 (基於setter): 通過JavaBean的屬性(setter方法)爲可服務對象指定服務。HiveMind和Spring採用這種方式。
3)、類型3 (基於構造函數): 通過構造函數的參數爲可服務對象指定服務。PicoContainer只使用這種方式。HiveMind和Spring也使用這種方式。
3、ioc的主要形式
1)、依賴查找:容器提供回調接口和上下文條件給組件。EJB和Apache Avalon 都使用這種方式。這樣一來,組件就必須使用容器提供的API來查找資源和協作對象,僅有的控制反轉只體現在那些回調方法上(也就是上面所說的 類型1):容器將調用這些回調方法,從而讓應用代碼獲得相關資源。
2)、依賴注入:組件不做定位查詢,只提供普通的Java方法讓容器去決定依賴關係。容器全權負責的組件的裝配,它會把符合依賴關係的對象通過JavaBean屬性或者構造函數傳遞給需要的對象。通過JavaBean屬性注射依賴關係的做法稱爲設值方法注入(Setter Injection);將依賴關係作爲構造函數參數傳入的做法稱爲構造器注入(Constructor Injection)

引用自百度百科

二、Springioc容器的實現方法

1、實現機制
spring ioc容器幫我們new了對象,並且給對象賦了值
2、存放進springioc容器的方法
1)、在spring-config.xml文件中配置bean
2)、通過註解的方式
其中在ioc中定義bean的前提是該bean的類 必須提供無參構造
3、IOC容器賦值
1)、簡單類型(字面值):8個基本類型+String,賦值用value
2)、
4、xml文件的具體操作
1)、id:唯一標識符 class:指定類型
例如:

<bean id="student" class="cn.chern.Student">

2)、property:該class所代表的類的屬性
name:屬性名
value:屬性值
例如:

<property name="stuNo" value="2"></property>

5、依賴注入的3種方式
1)、set注入:即獲取在bean中定義的類中的id的set函數
2)、構造器注入:通過構造方法賦值
3)、p命名空間注入
需要引入p命名空間:

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

6、通過註解定義bean
1)、概念:通過註解的形式將bean以及相應的屬性值放入ioc容器
2)、前期配置:引入xmlns 屬性:

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

3)、配置掃描器
4)、常用註解
@Component:所有層都可以用
@Repository:dao層註解
@Service:service層註解
@Controller:控制器層註解
7、使用註解實現事務(聲明式事務)
1)、目標:通過事務 使方法要麼全成功、要麼全失敗
2)、配置
增加事務tx的命名空間:

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

3)、使用
在需要成爲事務的方法前增加註解:@Transactional()
8、實現步驟
1)、將Ioc容器中的所有bean實例化爲對象
2)、將各個bean依賴的屬性值注入進去
3)、初始化ioc容器
4)、獲取對象

三、具體代碼

spring-config.xml代碼

<?xml version="1.0" encoding="UTF-8"?>
<!--使用p命名空間必須引入的xmlns 屬性: xmlns:p="http://www.springframework.org/schema/p" -->
<!--配置掃描器必須引入的xmlns 屬性: xmlns:context="http://www.springframework.org/schema/context" -->
<!--配置事務命名空間必須引入的xmlns 屬性: xmlns:tx="http://www.springframework.org/schema/tx" -->
<!--配置aop的通知必須引入的xmlns 屬性: xmlns:aop="http://www.springframework.org/schema/aop" -->
<!--給所有bean設置自動裝配: default-autowire = "" -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-autowire = "byName"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop">


<!---==========================依賴注入的三種方式===========================->


        <!-- 該文件中產生的所有對象,被spring放入了一個稱之爲spring ioc容器的地方-->
        <!-- id:唯一標識符   class:指定類型 -->
      <bean id="student" class="cn.chern.Student">
          <!-- property:class所代表的類的屬性
                name:屬性名
                value:屬性值
           -->
            <property name="stuNo" value="2"></property>
            <property name="stuName" value="ls"></property>
            <property name="stuAge" value="24"></property>
        </bean>

        <!-- 1、通過p命名空間注入
        <bean id="teacher" class="cn.chern.Teacher" p:age="35" p:name="ww">-->
            <!--
               2、通過set方法賦值
               (1)、value注入
        <bean id="teacher" class="cn.chern.Teacher">
        <property name="name" value="zs"></property>
        <property name="age" value="25"></property>
         &lt;爲XML的預定義實體引用,其值爲<
        <property name="name" value="z&lt;s"></property>
        -->
                  <!--
                 (2)<value>注入
            <bean id="teacher" class="cn.chern.Teacher">
            <property name="name">
            <value type="java.lang.String">a<![CDATA[!@#$<>?:|}{+_)(*&&^]]>sd</value>
          </property>
          -->
        <!-- (3)、賦值爲null -->
        <!--
        <bean id="teacher" class="cn.chern.Teacher">
        <property name="name">
            <null/>
        </property>
        -->
        <!-- (4)、賦值爲"" -->
		<bean id="teacher" class="cn.chern.Teacher">
         <property name="name">
             <value></value>
         </property>

             <!--
              3、通過構造器賦值
              -->
            <!--
             1)、按順序寫
             <bean id="teacher" class="cn.chern.Teacher">
             <constructor-arg value="ls"></constructor-arg>
             <constructor-arg value="24"> </constructor-arg>
            -->
            <!--
            2)、不按順序寫
            <bean id="teacher" class="cn.chern.Teacher">
            a、
            <constructor-arg value="24" index="1"></constructor-arg>
            <constructor-arg value="ls" index="0"> </constructor-arg>
            b、
            <constructor-arg value="24" name="age"></constructor-arg>
            <constructor-arg value="ls" name="name"> </constructor-arg>
            c、
            <constructor-arg value="24" type="int"></constructor-arg>
            <constructor-arg value="ls" String="String"> </constructor-arg>
            d、
            <constructor-arg value="24" type="int" index="1" name="age"></constructor-arg>
            <constructor-arg value="ls" String="String" index="0" name="age"> </constructor-arg>
            -->
        </bean>


<!---==========================各種配置===========================->


    <!-- 配置掃描器 -->
    <context:component-scan base-package="需要掃描的包"></context:component-scan>

    <!-- 配置數據庫相關 -事務 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"></property>
        <property name="username" value="scott"></property>
        <property name="password" value="tiger"></property>
        <property name="maxActive" value="10"></property>   <!--最大活動時間-->
        <property name="maxIdle" value="6"></property>  <!--最大空閒時間-->
    </bean>

    <!-- 配置事務管理器:transaction-manager -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>

    </bean>

    <!-- 增加對事務的支持 -->
    <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>


    </beans>
-->

test.java代碼

package cn.chern;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void springIoc(){
        //spring上下文對象:context
        //1、new
        //2、對象屬性的複製
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//        執行從springIOC容器中獲取一個id爲student的對象
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
  public static void main(String[] args) {
        springIoc();
    }
}

Student.java代碼

package cn.chern;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Student {
    private int stuNo;
    private String stuName;
    private int stuAge;

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return this.stuNo+","+this.stuName+","+this.stuAge;
    }

Teacher.java代碼

package cn.chern;

public class Teacher {
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章