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;
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值