spring学习笔记-3(IOC)

1.概述

  • IOC(inversion of control,控制反转)是一种设计思想,将设计好的对象交给容器控制,而不是传统的在对象内部直接控制。在没有IOC的情况下,在需要一个对象需要自己主动去创建,这时候创建者和被创建者之间就存在了一种耦合关系。而在IOC的情况下,创建对象的工作是由Spring负责的,创建好的对象会放在一个容器中,当需要一个对象的时候,Spring会从容器中提取一个匹配的对象给需要的对象。
  • IOC的本质是创建对象的控制权被转移了,以前创建对象的主动权和时机由自己把握,现在这种权利移交给了IOC容器了。
/*没有IOC*/
Student student = new Student();

/* 使用IOC,studnet对象可以从spring容器中获取*/
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = ac.getBean("student1", Student.class);

2.Spring中实现IOC

2.1.对象的创建

在applicationContext.xml文件中可以对对象进行配置,这些对象由spring进行统一管理。以Student类为例,其对象创建有3中主要方式

public class Student {

    private int id;
    private String name;
    private int age;
    private int tid;
    private String[] array;
    private Set<String> set;
    private List<String> list;
    private Map<String, String> map;

    private Teacher teacher;	

    public Student() {
    }

    public Student(int id, String name, int age, int tid) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.tid = tid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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;
    }

    public int getTid() {
        return tid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    public String[] getArray() {
        return array;
    }

    public void setArray(String[] array) {
        this.array = array;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

}

2.1.1.使用构造方法进行创建

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--1.1默认使用无参构造方法构造对象-->
    <!--id:实例的id号,可通过该id号获取对应的实例
    class:类路径-->
    <bean id="student1" class="com.bear.sxt.pojo.Student"/>

    <!--1.2.使用有参构造方法构造对象-->
    <bean id="student2" class="com.bear.sxt.pojo.Student">
        <!--index:参数索引
            name:参数名字
            type:参数类
            value:参数默认值,用于基本数据类型、String等
            ref:当类中的属性为bean类型时,使用ref代替value-->
        <constructor-arg index="0" name="id" type="int" value="1"/>
        <constructor-arg index="1" name="name" type="java.lang.String" value="有参构造方法"/>
        <constructor-arg index="2" name="age" type="int" value="25"/>
        <constructor-arg index="3" name="tid" type="int" value="1"/>
    </bean>
</beans>

2.1.2.使用实例工厂进行创建

  • 定义一个实例工厂类
public class StudentFactory {
    public Student newInstance() {
        return new Student(1, "实例工厂", 25, 1);
    }
}
  • 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--2.使用实例工厂进行构造(工厂类需要实例化了才能创建目标对象)-->
    <bean id="factory" class="com.bear.sxt.factory.StudentFactory"/>
    <!--factory-bean:创建对象所需的工厂实例的id
        factory-method:工厂类中创建实例的方法名-->
    <bean id="student3" factory-bean="factory" factory-method="newInstance"/>
</beans>

2.1.3.使用静态工厂进行创建

  • 定义一个静态工厂类
public class StaticStudentFactory {
    public static Student newInstance() {
        return new Student(2, "静态工厂", 25, 2);
    }
}
  • 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--3.使用静态工厂进行构造(工厂类不需要实例化就能创建目标对象)-->
    <!--class:工厂类的类路径
        factory-method:工厂类中创建实例的方法名-->
    <bean id="student4" class="com.bear.sxt.factory.StaticStudentFactory" factory-method="newInstance"/>
</beans>

2.2.给对象的成员变量

  • 交由Spring创建对象的时候可以指定成员变量的初值。以上述的Student类为例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--给类中各种属性赋值-->
    <bean id="student5" class="com.bear.sxt.pojo.Student">
        <!--基本数据类型、String类型等的赋值-->
        <!--直接通过value属性进行赋值-->
        <property name="id" value="3"/>
        <!--通过value子标签进行赋值-->
        <property name="name">
            <value>属性赋值测试</value>
        </property>
        <property name="age">
            <value>25</value>
        </property>
        <property name="tid" value="2"/>

        <!--数组类型-->
        <property name="array">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>


        <!--set类型-->
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

        <!--list类型-->
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>

        <!--map类型-->
        <property name="map">
            <map>
                <entry key="key1" value="value1"/>
                <entry key="key2" value="value2"/>
            </map>
        </property>

        <!--bean类型,需要使用ref属性引用另一个bean对象-->
        <property name="teacher" ref="teacher"/>
    </bean>
    <!--被student对象引用的bean对象-->
    <bean id="teacher" class="com.bear.sxt.pojo.Teacher">
        <property name="id" value="1"/>
        <property name="name" value="DI测试"/>
    </bean>
</beans>

2.3.对象的获取

加载applicationContext.xml文件后,对象就已经创建了并由spring容器管理。无论你使不使用这些对象,这些对象都是存在的。

2.3.1.获取指定的对象

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过applicationContext.xml文件中配置的bean的id号获取对应的bean对象
Student student1 = ac.getBean("student1", Student.class);

2.3.2.获取spring管理的所有对象的id

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
String[] names = ac.getBeanDefinitionNames();
for (String s : names) {
    System.out.println(s);
}

2.4.bean的其他属性

2.4.1.scope属性

用于控制对象的有效范围.

  • singleton:单例。
  • prototype:原型(多例)。
  • request:每次请求都是单例,不同请求是多例。
  • session:每次对话是单例,不同对话是多例。
  • global session:和session类似。
  • application:一个应用里是单例(其实就跟纯单例一样)。
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值