Spring IOC XML配置

Spring Ioc 使用XML配置

1、首先到引入依赖包



    org.springframework
    spring-context
    5.1.6.RELEASE

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


  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 phone;
    private Set 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 phone, Set 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 getPhone() {
        return phone;
    }

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

    public Set getOldName() {
        return oldName;
    }

    public void setOldName(Set 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



        
            
            
        
    
        
            
            
            
            
                
                    13711112222
                    13711112223
                
            
            
            
                
                    小明
                    小红
                
            
            
            
                
                    admin
                    123456
                
            
            
            
        

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
  
3.1.6 自动装配 如果你的Bean中需要注入其他对象的话 你可以使用 autowire 这个标签注入 他有两种注入方式
1、一种是根据id名字注入

2、二种是更具类型注入

3.1.7 单例和多例 scope
1、单例模式:当xml加载到spring中 只要是 scope="singleton" 然后 默认都是单例模式 spring加载的时候就会把这些对象创建好

2、多例模式: 当每次getBean的时候才创建一个新的对象 初始话的时候不会创建

你可能感兴趣的:(Spring IOC XML配置)