Spring IOC XML配置

2021-11-28

Spring Ioc 使用XML配置

1、首先到引入依赖包

<!--引入配置-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>

2、创建Spring配置文件,一般取名这三个(任意 无约定) spring-context.xml、applicationContext.xml、beans.xml

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

</beans>

3、创建一个使用ioc创建一个对象 并且帮他设置好初始值

3.1首先创建好Class 这边有两个类一个是学生类 和 地址类

3.1.1学生类 Student.java 并设置他的get和set 方法要不要xml配置不了属性
//Student.java
package com.ared.entity;


import java.util.List;
import java.util.Properties;
import java.util.Set;

public class Student {

    private int id;
    private String name;
    private List<String> phone;
    private Set<String> oldName;
    private Properties info;
    private Address address;

    public Student(){
        System.out.println("----Constructor----");
    }


    public void init(){
        System.out.println("-----init------");
    }


    public void destroy(){
        System.out.println("-----destroy-----");
    }

    public Student(int id, String name, List<String> phone, Set<String> oldName, Properties info, Address address) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.oldName = oldName;
        this.info = info;
        this.address = address;
    }

    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 List<String> getPhone() {
        return phone;
    }

    public void setPhone(List<String> phone) {
        this.phone = phone;
    }

    public Set<String> getOldName() {
        return oldName;
    }

    public void setOldName(Set<String> oldName) {
        this.oldName = oldName;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phone=" + phone +
                ", oldName=" + oldName +
                ", info=" + info +
                ", address=" + address +
                '}';
    }
}

3.1.2地址类 Address.java
//Address.java
package com.ared.entity;


public class Address {
    String homeAddress;
    String officeAddress;

    public String getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    public String getOfficeAddress() {
        return officeAddress;
    }

    public void setOfficeAddress(String officeAddress) {
        this.officeAddress = officeAddress;
    }

    @Override
    public String toString() {
        return "Address{" +
                "homeAddress='" + homeAddress + '\'' +
                ", officeAddress='" + officeAddress + '\'' +
                '}';
    }
}

3.1.3配置Spring配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

        <bean id="address" class="com.ared.entity.Address">
            <property name="homeAddress" value="青山湖"/>
            <property name="officeAddress" value="高新"/>
        </bean>
    
        <bean id="student" class="com.ared.entity.Student">
            <property name="id" value="10001"/>
            <property name="name" value="码小农"/>
            <!-- 添加一个list参数 -->
            <property name="phone">
                <list>
                    <value>13711112222</value>
                    <value>13711112223</value>
                </list>
            </property>
            <!-- 添加一个set参数 -->
            <property name="oldName">
                <set>
                    <value>小明</value>
                    <value>小红</value>
                </set>
            </property>
            <!--添加一个Properties参数-->
            <property name="info">
                <props>
                    <prop key="username">admin</prop>
                    <prop key="password">123456</prop>
                </props>
            </property>
            <!--如果属性有对象的话可以先创建bean 然后使用ref 应用id 得到值-->
            <property name="address" ref="address"/>
        </bean>
</beans>
3.1.4写一个测试类 IocTest.java
package com.ared.test;

import com.ared.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IocTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");

        Student student = (Student) context.getBean("student");
        System.out.println(student);


    }

}

3.1.5使用 在Bean创建时会可以指定一个 初始方法 和 销毁方法 只需要修改Bean 标签上加一个 init-method 和 destroy-method
  <bean id="student" class="com.ared.entity.Student" init-method="init" destroy-method="destroy">
3.1.6 自动装配 如果你的Bean中需要注入其他对象的话 你可以使用 autowire 这个标签注入 他有两种注入方式
1、一种是根据id名字注入
<bean id="student" class="com.ared.entity.Student" autowire="byName">
2、二种是更具类型注入
<bean id="student" class="com.ared.entity.Student" autowire="byType">
3.1.7 单例和多例 scope
1、单例模式:当xml加载到spring中 只要是 scope="singleton" 然后 默认都是单例模式 spring加载的时候就会把这些对象创建好
<bean id="student" class="com.ared.entity.Student" scope="singleton">
2、多例模式: 当每次getBean的时候才创建一个新的对象 初始话的时候不会创建
<bean id="student" class="com.ared.entity.Student" scope="prototype">