【学习笔记】Spring IoC容器的配置(xml配置)

Spring的IOC入门-环境搭建

(1)创建Project maven
(2)创建模块module maven
(3)配置依赖

<!--spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

SpringIOC容器

SpringIOC容器,是spring核心内容。

作用: 创建对象 & 处理对象的依赖关系

IOC容器创建对象:

创建对象, 有几种方式:

  1. 调用无参数构造器

  2. 带参数构造器

  3. 工厂创建对象(静态工厂方式与非静态工厂方式创建对象)

配置对象的基本结构

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

怎么取出在IoC容器中的对象

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	Xxx bean = (Xxx)context.getBean("bean的id");/方式一
	Xxx bean = context.getBean("bean的id",Xxx.class对象)/方式二

示例使用:

public class Person {
    private String name;
	private int age;
	private String sex;
	public Person() {
    }
    public Person(String name,int age,String sex){
    	this.name=name;
    	this.age=age;
    	this.sex=sex;
    }
    get set省略

调用无参数构造器

<bean class="com.glc.bean.Person" id="person" />

带参数构造器

如果想要以其他参数个数创建对象,必须存在响应的构造方法,要不然报错

<bean class="com.glc.bean.Person2" id="person">
    <constructor-arg name="name" value="zhangsan"/>
    <constructor-arg name="age" value="13"/>
    <constructor-arg name="sex" value=""/>
</bean>

静态工厂方式

public class PersonFactory {
    public static Person getBean() {
        return new Person();//静态方法返回创建的对象
    }
}

在这里插入图片描述

非静态工厂方式

public class PersonFactory {
    public Person getBean() {
        return new Person();//静态方法返回创建的对象
    }
}

在这里插入图片描述

单例和多例

单例是什么?

内存中只有一个对象,每次获取到该对象的地址值一样.

多实例是什么?

内存中的每个对象都是一个新的对象,他们的地址值都不同.

(1)问题: 每次获取对象的时候,spring是新创建一个对象还是始终给我们返回同一个对象.
(2)答案: spring默认的情况下创建的对象都是单例的. (每次返回的对象都是同一个)

scope="singleton" 单例(默认值)
scope="prototype" 多例
scope="request" 创建的对象放到request域中
scope="session" 创建对象放到session对象

多实例

<bean id="person" class="com.wzx.domain.Person" scope="prototype"/>

单实例

<bean id="person" class="com.wzx.domain.Person" scope="singleton"/>

Spring生命周期

(1)生命周期

  • 创建方法init
  • 销毁方法destory
  • 普通方法service

(2)属性

  • init-method 当该对象初始化的时候该方法会自动执行
  • destroy-method 当该对象即将销毁的时候会自动调用该方法
public class Person{
   public void init(){
        System.out.println("init...");
    }
    public void eat(){
        System.out.println("eat...");
    }
    public void destory(){
        System.out.println("destory...");
    }
}
   <bean id="person6" class="com.wzx.domain.Person"
          init-method="init"
          destroy-method="destory"
    />

Spring依赖注入DI-set方法

依赖注入DI (dependency injection)就是给对象的属性设置值
set方法给对象设置值
(基本类型赋值)

property标签
让spring调set方法,前提条件类中必须有set方法

name : 代表的是set方法去掉set,首字母小写setName Name name value:
value 可以设置基本类型或字符串类型的值,
ref (引用) : 引用对象的id,作为一个对象类型注入

在这里插入图片描述
(复杂类型赋值)

    //数组类型
    <bean class="com.glc.bean.Person" id="person5">
        <property name="strings">
            <array>
                <value></value>
                <value></value>
                <value></value>
            </array>
        </property>
    </bean>
    //list类型
    <bean class="com.glc.bean.Person" id="person6">
        <property name="order">
            <list>
                <value></value>
                <value></value>
                <value></value>
            </list>
        </property>
    </bean>
    //set类型
    <bean class="com.glc.bean.Person" id="person4">
        <property name="set">
            <set>
                <value></value>
                <value></value>
                <value></value>
            </set>
        </property>
    </bean>
    //map类型
    <bean class="com.glc.bean.Person" id="person4">
        <property name="map">
            <map>
                <entry key="1" value=""/>
                <entry key="2" value=""/>
                <entry key="3" value=""/>
            </map>
        </property>
    </bean>
    //properties类型
    <bean class="com.glc.bean.Person" id="person4">
        <property name="properties">
            <props>
                <prop key="1"></prop>
                <prop key="2"></prop>
                <prop key="3"></prop>
            </props>
        </property>
    </bean>
    //值设置为空
    <bean class="com.glc.bean.Person" id="person4">
        <property name="Null">
            <null></null>
        </property>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值