Spring IOC基于XML配置使用

一、springIOC简介

ApplicationContext是Spring IoC容器实现的代表,它负责实例化,配置和组装Bean

二、环境准备

  1. 创建maven项目
  2. 添加依赖
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version>
        </dependency>
  1. 创建实体类
public class Student{
    private String name;
    private int age;
    private String hobby;
}
public class Person {
    private int id;
    private String realName;
    private String name;
    private Student student;
    private Date birthday;
    private List<String> hobbies;
    private Map<String,String> course;
}
  1. 在resources文件下创建XML文件springTest.xml

三、实例解析

1.实现依赖注入的两种方式

  • 基于set方法进行依赖注入
    1.注入的属性需添加set方法
    2.name值时set方法名字来的
	<bean class="com.tedu.liyu.entry.Student" id="student">
        <property name="age" value="12"></property>
        <property name="hobby" value="王者荣耀"></property>
        <property name="name" value="李白"></property>
    </bean>

创建测试类:

public class TestIOC {
    ClassPathXmlApplicationContext ioc;
    @Before
    public void before(){
        ioc = new ClassPathXmlApplicationContext("springTest.xml");
    }
    @Test
    public void test1(){
        Student student = ioc.getBean("student",Student.class);
        System.out.println(student);
    }
}
  • 基于构造函数的依赖注入
    1.注入的实体类需添加有参的构造方法
    2.name值为构造方法的入参变量
public Student(String name, int age, String hobby) {
        this.name = name;
        this.age = age;
        this.hobby = hobby;
}
	<bean class="com.tedu.liyu.entry.Student" id="student">
        <constructor-arg name="name" value="十三中"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="hobby" value="数学"></constructor-arg>
    </bean>

2.复杂类型的注入

下面采用的是set注入,所以实体类中需要添加相关的set方法

<bean class="com.tedu.liyu.entry.Person" id="person">
        <property name="id" value="1"></property>
        <property name="name">
            <null></null>
        </property>
        <property name="realName" value=""></property>
        <property name="birthday" value="2020/05/20"></property>
        <property name="student">
            <bean class="com.tedu.liyu.entry.Student">
                <property name="name" value="张三"></property>
                <property name="hobby" value="打篮球"></property>
                <property name="age" value="18"></property>
            </bean>
        </property>
        <property name="hobbies">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <property name="course">
            <map>
                <entry key="1" value="Java"></entry>
                <entry key="2" value="Js"></entry>
            </map>
        </property>
</bean>

3.自动注入

<bean class="com.tedu.liyu.entry.Student" id="student">
        <constructor-arg name="name" value="十三中"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="hobby" value="数学"></constructor-arg>
</bean>
<bean class="com.tedu.liyu.entry.Person" id="person2" autowire="byName">
        <property name="id" value="999"></property>
        <property name="name" value="666"></property>
</bean>

当person对象中需要引用另一个student对象时,在自动注入时,spring会在IOC容器中自动找到匹配的进行注入。
byName:按照名字进行装配,以属性名作为id去容器中查找组件,进行赋值,如果找不到则装配null
byType:按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件,如果有多个类型相同的bean对象,那么会报异常,如果找不到则装配null

4.Bean的作用域

singleton:单例,只会在IOC容器中创建一次
prototype:多例,每次获取IOC容器都会new一个bean

<bean class="com.tedu.liyu.entry.Student" id="student3" scope="singleton">
</bean>

5.生命周期回调

两种方式:
1.使用接口实现的方式实现生命周期的回调初始化方法:
实现接口InitializingBean,重afterPropertiesSet方法销毁方法:
实现接口DisposableBean,重写destroy方法

<bean class="com.tedu.liyu.entry.Student" id="student"></bean>
public class Student implements InitializingBean , DisposableBean {
	public void afterPropertiesSet() throws Exception {
        System.out.println("生命周期回调---初始化");
    }
    public void destroy() throws Exception {
        System.out.println("生命周期回调---销毁");
    }
}

2.使用对应bean里创建的方法实现生命周期的回调

<bean class="com.tedu.liyu.entry.Student" id="student" init-method="init" destroy-method="destroy"></bean>
public class Student{
	public void init(){
        System.out.println("生命周期回调---初始化");
    }
    public void destroy(){
        System.out.println("生命周期回调---销毁");
    }
}

示例如上:init-method的值为实体类中自定义初始化的方法名,destroy-method的值为实体类中自定义销毁的方法名

6.懒加载

<!-- 懒加载
        使用lazy‐init设置懒加载
        默认false:在spring容器创建时加载
        true:在使用时,才会加载-->
<bean class="com.tedu.liyu.entry.Student" id="student" lazy-init="false"></bean>

7.加载外部配置文件

1.先在resources下添加springContext.properties配置文件

student.name=123
student.age=10
student.hobby=中国
<context:property-placeholder location="springContext.properties" file-encoding="UTF-8"/>
<bean class="com.tedu.liyu.entry.Student" id="student">
     <property name="name" value="${student.name}"></property>
     <property name="age" value="${student.age}"></property>
     <property name="hobby" value="${student.hobby}"></property>
</bean>

示例如上:需要注意的是,需要添加file-encoding=“UTF-8”,不然配置文件中有中文时,引用可能会乱码。

8.SpEL的使用

<bean class="com.tedu.liyu.entry.Student" id="student">
        <constructor-arg name="name" value="十三中"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="hobby" value="数学"></constructor-arg>
</bean>
<bean class="com.tedu.liyu.entry.Person" id="person">
        <property name="name" value="#{student.name}"></property>
        <property name="student" value="#{student}"></property>
        <property name="birthday" value="2020/05/20"></property>
</bean>

SpEL:Spring Expression Language,spring的表达式语言,支持运行时查询操作对象

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值