加载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中的三个方法
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", ssex='" + ssex + '\'' +
", sage=" + sage +
'}';
}
public Student() {
System.out.println("Student无参构造方法");
}
public Student(Integer sid, String sname, String ssex, Integer sage) {
System.out.println("Student有参构造方法");
this.sid = sid;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
}
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">
<property name="sid">
<value>1</value>
</property>
<property name="sname" value="klxh"></property>
<property name="ssex" value="女"></property>
<property name="sage" value="18"></property>
</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");
/**
* 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);
}
}
②构造方法赋值
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);
}
}