随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

创建bean类和测试类:

package test;

import entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LoadTest {
    ApplicationContext app;
    @Before
    public void before() {
        // 加载spring容器
        // ApplicationContext spring顶层的核心接口
        // ClassPathXmlApplicationContext 根据项目路径的xml文件来实例化spring容器
        // FileSystemXmlApplicationContext 根据磁盘路径的xml文件来实例化spring容器
        // AnnotationConfigApplicationContext 根据javaconfig 来配置实例化spring容器
        // 容器实例化的时候就会加载所有的bean
        // 在容器实例化的时候就会将bean实例化
        app = new ClassPathXmlApplicationContext("spring1.xml");
    }
    @Test
    public void fun1(){
        System.out.println("begin!");
    }
}
package entity;

import java.util.Objects;

public class User {
    private Integer id;
    private String userName;
    private String realName;

    public User() {
        System.out.println("User加载");
    }
    public User(Integer id, String userName, String realName) {
        this.id = id;
        this.userName = userName;
        this.realName = realName;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id) && Objects.equals(userName, user.userName) && Objects.equals(realName, user.realName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, userName, realName);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }
}
package entity;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Wife {
    private Integer id;
    private String name;
    private String gender;
    private Date birth;
    private List<String> ls;
    private Map<Integer,String> course;

    public Wife() {
        System.out.println("Wife加载");
    }

    @Override
    public String toString() {
        return "Wife{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birth=" + birth +
                ", ls=" + ls +
                ", course=" + course +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Wife wife = (Wife) o;
        return Objects.equals(id, wife.id) && Objects.equals(name, wife.name) && Objects.equals(gender, wife.gender) && Objects.equals(birth, wife.birth) && Objects.equals(ls, wife.ls) && Objects.equals(course, wife.course);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, gender, birth, ls, course);
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public List<String> getLs() {
        return ls;
    }

    public void setLs(List<String> ls) {
        this.ls = ls;
    }

    public Map<Integer, String> getCourse() {
        return course;
    }

    public void setCourse(Map<Integer, String> course) {
        this.course = course;
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="entity.User" id="user"></bean>
    <bean class="entity.Wife" id="wife"></bean>
</beans>

结果:

 

 可以发现是User类先加载,然后是Wife类;

 

使用depends-on修改顺序:

加上depends-on表示User类是依赖Wife类的

 

 结果:

 

 Wife类先加载。

 

posted on 2022-05-25 13:59  时间完全不够用啊  阅读(642)  评论(0编辑  收藏  举报