SpringIOC的原理
1.读取bean的XML配置文件
2.使用beanId查找bean配置,并获取配置文件中class地址。
3.使用Java反射技术实例化对象
4.获取属性配置,使用反射技术进行赋值。
总结:就是吧每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理
2.Xml的配置
<beans>
<bean id="user1" class="com.itmayiedu.entity.UserEntity">
<property name="userId" value="0001"></property>
<property name="userName" value="李四"></property>
</bean>
<bean id="user2" class="com.itmayiedu.entity.UserEntity">
<property name="userId" value="0002"></property>
<property name="userName" value="张三"></property>
</bean>
</beans>
3.java的代码
//1.读取springxml配置
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.获取bean对象
TestService testService = (TestService) classPathXmlApplicationContext.getBean("testService");
System.out.println(testService.test());
详细步骤
1.利用传入的参数获取xml文件的流,并且利用dom4j解析成Document对象
2.对于Document对象获取根元素对象后对下面的标签进行遍历,判断是否有符合的id.
3.如果找到对应的id,相当于找到了一个Element元素,开始创建对象,先获取class属性,根据属性值利用反射建立对象.
4.遍历标签下的property标签,并对属性赋值.注意,需要单独处理int,float类型的属性.因为在xml配置中这些属性都是以字符串的形式来配置的,因此需要额外处理.
5.如果属性property标签有ref属性,说明某个属性的值是一个对象,那么根据id(ref属性的值)去获取ref对应的对象,再给属性赋值.
6.返回建立的对象,如果没有对应的id,或者下没有子标签都会返回null
实体类
public class User {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
使用反射技术完成Java代码
import java.lang.reflect.Field;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.zslaa.entity.User;
public class ClassPathXmlApplicationContext {
private String xmlPath;
/**
*
* @param xmlPath spring xml 配置路径
*/
public ClassPathXmlApplicationContext(String xmlPath) {
this.xmlPath = xmlPath;
}
public Object getBean(String beanId) throws Exception {
// 解析xml器
SAXReader saxReader = new SAXReader();
Document read = null;
try {
// 从项目根目录路径下 读取
read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
if (read == null) {
return null;
}
// 获取根节点资源
Element root = read.getRootElement();
List<Element> elements = root.elements();
if (elements.size() <= 0) {
return null;
}
Object oj = null;
for (Element element : elements) {
String id = element.attributeValue("id");
if (StringUtils.isEmpty(id)) {
return null;
}
if (!id.equals(beanId)) {
continue;
// throw new Exception("使用beanId:" + beanId + ",未找到该bean");
}
// 获取实体bean class地址
String beanClass = element.attributeValue("class");
// 使用反射实例化bean
Class<?> forNameClass = Class.forName(beanClass);
oj = forNameClass.newInstance();
// 获取子类对象
List<Element> attributes = element.elements();
if (attributes.size() <= 0) {
return null;
}
for (Element et : attributes) {
// 使用反射技术为方法赋值
String name = et.attributeValue("name");
String value = et.attributeValue("value");
Field field = forNameClass.getDeclaredField(name);
field.setAccessible(true);
field.set(oj, value);
}
}
return oj;
// 1.使用beanId查找配置文件中的bean。
// 2.获取对应bean中的classpath配置
// 3.使用java反射机制实体化对象
}
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
User bean = (User) applicationContext.getBean("user2");
System.out.println("使用反射获取bean" + bean.getUserId() + "---" + bean.getUserName());
}
}