官网








package com.sxs.spring.bean;
/**
* Entity
* @author 孙显圣
* @version 1.0
*/
public class Monster {
private Integer monsterId;
private String name;
private String skill;
//无参构造器,一定要给,底层是反射创建对象
public Monster() {}
public Monster(Integer monsterId, String name, String skill) {
this.monsterId = monsterId;
this.name = name;
this.skill = skill;
}
public Integer getMonsterId() {
return monsterId;
}
public void setMonsterId(Integer monsterId) {
this.monsterId = monsterId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return "Monster{" +
"monsterId=" + monsterId +
", name='" + name + '\'' +
", skill='" + skill + '\'' +
'}';
}
}




package com.sxs.spring.test;
import com.sxs.spring.bean.Monster;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author 孙显圣
* @version 1.0
*/
public class SpringBeanTest {
@Test
public void getMonster() {
//创建容器ApplicationContext,该容器是对应于一个xml配置文件
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//第一种获取bean对象的方式
//1.直接获取配置文件中的bean对象,不指定泛型
Object monster01 = ioc.getBean("monster01");
//2.这是一个Object类型的方法,指向一个monster类型的对象,所以需要向下转型
Monster monster = (Monster) monster01;
//3.输出信息
System.out.println(monster);
//第二种获取bean对象的方式
//1.获取配置文件中的bean对象,指定泛型,则这个方法就是泛型类型的
Monster monster011 = ioc.getBean("monster01", Monster.class);
//2.输出信息
System.out.println(monster011);
}
}

//验证类加载路径 @Test public void classPath() { //输出类加载路径 System.out.println(this.getClass().getResource("/").getPath()); //所以new ClassPathXmlApplicationContext("beans.xml"); //相当于获取/D:/Intelij_IDEA_Project/spring/spring/out/production/spring/下的beans.xml }

可以看出,读取的实际是spring下面的文件,对应于项目中的src下,可以理解为在项目中src/就是类加载路径








//获取beans.xml中所有对象的id
@Test
public void getAllId() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}



package com.sxs.spring.sxsapplicationcontext;
import com.sxs.spring.bean.Monster;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 实现ApplicationContext的简单机制
*
* @author 孙显圣
* @version 1.0
*/
public class SxsApplicationContext {
//存储单例对象的字段
private ConcurrentHashMap singletonObjects = new ConcurrentHashMap<>();
//构造器,用于读取xml文件,默认在src下
public SxsApplicationContext(String iocBeanXmlFileName) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//获取类路径
String path = this.getClass().getResource("/").getPath();
//dom4j读取文件
SAXReader saxReader = new SAXReader();
Document read = saxReader.read(new File(path + iocBeanXmlFileName));
Element rootElement = read.getRootElement();
//获取二级元素的第一个
Element bean = (Element) rootElement.elements("bean").get(0);
//获取属性信息
String classAllPath = bean.attributeValue("class");
String id = bean.attributeValue("id");
//获取元素信息
List property = bean.elements("property");
//遍历获取,这里直接简化一下,直接获取
Integer monsterId = Integer.parseInt(property.get(0).attributeValue("value"));
String name = property.get(1).attributeValue("value");
String skill = property.get(2).attributeValue("value");
//反射创建对象Monster对象
Class> aClass = Class.forName(classAllPath);
Monster o = (Monster) aClass.newInstance();
//为属性赋值
o.setMonsterId(monsterId);
o.setName(name);
o.setSkill(skill);
//放到单例对象中
singletonObjects.put(id, o);
}
//提供一个getBean方法,获取id对应的bean对象
public T getBean(String id, Class aClass) {
for (Map.Entry stringObjectEntry : singletonObjects.entrySet()) {
if (stringObjectEntry.getKey().equals(id)) {
//返回T类型的bean对象
return (T) stringObjectEntry.getValue();
}
}
return null;
}
}
package com.sxs.spring.sxsapplicationcontext;
import com.sxs.spring.bean.Monster;
import org.dom4j.DocumentException;
/**
* @author 孙显圣
* @version 1.0
*/
public class SxsApplicationContextTest {
public static void main(String[] args) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException {
SxsApplicationContext sxsApplicationContext = new SxsApplicationContext("beans.xml");
//获取bean对象
Monster monster01 = sxsApplicationContext.getBean("monster01", Monster.class);
System.out.println(monster01);
}
}





//得到系统默认分配的id并且得到bean对象
@Test
public void getDefaultIdToFindBean() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//获取所有bean对象
String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(ioc.getBean(beanDefinitionName));
}
}


package com.sxs.spring.bean;
/**
* @author 孙显圣
* @version 1.0
*/
public class Car {
private Integer id;
private String name;
private Double price;
public Car() {}
public Car(Integer id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
}
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 Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
//得到car对象
@Test
public void getCarObject() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
Car car = ioc.getBean("car", Car.class);
System.out.println(car);
}
