加载BeanFactory一个小扩展 (两种方式都可以)
    BeanFactory factory = new ClassPathXmlApplicationContext(“”); (③)
    BeanFactory factory = new FileSystemXmlApplicationContext(“”); (可以①②)

    ①SystemPath(系统路径 当前电脑的硬盘路径 C D)
    ②ProjectPath(工程路径 当前写源代码的地方 并不是最终字节码的位置)
    ③ClassPath(真实类路径 字节码所在的地方 字节码文件信息加载入内存)

    对象交给Spring来管理——IOC
    对象中可能会有一些属性—-自动帮我们将属性值赋上
    对象创建的同时能自动的将对象中的属性值注入进去——-DI(Dependency Injection)
    (控制翻转IOC、依赖注入DI 、面向切面编程AOP)

    通过bean创建的同时给属性赋值
    ①set方法赋值
    注意:无参数构造方法必须存在 属性对应的set方法也必须存在
    Student中的三个方法

    1. @Override
    2. public String toString() {
    3. return "Student{" +
    4. "sid=" + sid +
    5. ", sname='" + sname + '\'' +
    6. ", ssex='" + ssex + '\'' +
    7. ", sage=" + sage +
    8. '}';
    9. }
    10. public Student() {
    11. System.out.println("Student无参构造方法");
    12. }
    13. public Student(Integer sid, String sname, String ssex, Integer sage) {
    14. System.out.println("Student有参构造方法");
    15. this.sid = sid;
    16. this.sname = sname;
    17. this.ssex = ssex;
    18. this.sage = sage;
    19. }

    ApplicationContext.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. https://www.springframework.org/schema/beans/spring-beans.xsd">
    6. <bean name="student" class="domain.Student">
    7. <property name="sid">
    8. <value>1</value>
    9. </property>
    10. <property name="sname" value="klxh"></property>
    11. <property name="ssex" value="女"></property>
    12. <property name="sage" value="18"></property>
    13. </bean>
    14. </beans>

    TestMain

    package test;
    
    import domain.Student;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestMain {
        public static void main(String[] args){
    
            //演示Spring的DI
            BeanFactory factory=new ClassPathXmlApplicationContext("ApplicationContext.xml");
            Student student=(Student) factory.getBean("student");
    
            /**
             * getBean底层
             * 1.读取xml文件---Class(domain.Student)
             * 2.Class clazz=Class.forName("domain.Student");
             * 3.Student student=(Student)clazz.newInstance();
             * 4.找寻class中的所有私有属性
             *  Filed[] fs=clazz.getFileds();
             *  for(Filed f : fs){
             *      属性找到对应的set方法
             *      set方法.invoke();
             *  }
             */
    
            System.out.println(student);
    
        }
    }
    

    image.png

    ②构造方法赋值
    Student中的三个方法不变
    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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--    通过带参数的构造方法给属性赋值-->
        <bean name="student" class="domain.Student">
        <constructor-arg name="sid" value="1" type="java.lang.Integer"></constructor-arg>
        <constructor-arg name="sname" value="klxh" type="java.lang.String"></constructor-arg>
        <constructor-arg name="ssex">
    <!--        代表赋值null-->
            <null></null>
        </constructor-arg>
        <constructor-arg name="sage" value="18"></constructor-arg>
        </bean>
    
    </beans>
    

    TestMain

    package test;
    
    import domain.Student;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestMain {
        public static void main(String[] args){
            //演示Spring的DI
            BeanFactory factory=new ClassPathXmlApplicationContext("ApplicationContext.xml");
            Student student=(Student) factory.getBean("student");
            /**
             * 1.创建bean工厂,读取xml文件,获取class类名字
             * 2.Class clazz=Class.forName(类名字)
             * 3.找到带参数的构造方法
             *  Constructor con=clazz.getConstructor(配置决定参数个数)
             *  配置还决定是否能与属性名匹配
             *  反射找到属性、属性类型
             * 4.执行构造方法创建对象
             *  con.newInstance(值)
             */
            System.out.println(student);
    
        }
    }
    

    image.png