基于XML方式的依赖注入,主要是基于XML的解析,类的反射机制
主要思路:
1、解析xml文件
2、获取xml文件所有的节点内容
3、根据类路径反射类
4、注入属性信息
项目基本框架:
1、首先创建普通maven项目,导入dom4j、Spring context等的依赖
pom.xml(我直接复制的,所以spring的基本依赖基本都有,可以自己选择性导入)
4.0.0
com.spring.xml
ioc
1.0-SNAPSHOT
ioc
http://www.example.com
UTF-8
1.7
1.7
4.3.7.RELEASE
junit
junit
4.11
test
dom4j
dom4j
1.6.1
org.springframework
spring-aop
${org.springframework.version}
org.springframework
spring-aspects
${org.springframework.version}
org.springframework
spring-beans
${org.springframework.version}
org.springframework
spring-context
${org.springframework.version}
org.springframework
spring-context-support
${org.springframework.version}
org.springframework
spring-core
${org.springframework.version}
org.springframework
spring-expression
${org.springframework.version}
org.springframework
spring-instrument
${org.springframework.version}
org.springframework
spring-instrument-tomcat
${org.springframework.version}
org.springframework
spring-jdbc
${org.springframework.version}
org.springframework
spring-jms
${org.springframework.version}
org.springframework
spring-messaging
${org.springframework.version}
org.springframework
spring-orm
${org.springframework.version}
org.springframework
spring-oxm
${org.springframework.version}
org.springframework
spring-test
${org.springframework.version}
org.springframework
spring-tx
${org.springframework.version}
org.springframework
spring-web
${org.springframework.version}
org.springframework
spring-webmvc
${org.springframework.version}
org.springframework
spring-webmvc-portlet
${org.springframework.version}
org.springframework
spring-websocket
${org.springframework.version}
maven-clean-plugin
3.0.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.7.0
maven-surefire-plugin
2.20.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
2、创建bean
package com.spring.xml.bean;
public class Car {
private String name;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
3、创建applicationContext.xml
4、创建类XmlApplicationContext
package com.spring.xml.utils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* 1、解析xml文件
* 2、获取xml文件所有的节点内容
* 3、根据类路径反射类
* 4、注入属性信息
*/
public class XmlApplicationContext {
// xml路径
private String xmlPath;
// 存放bean
private Map map = null;
// 存放从xml解析出来的类的需要注入的属性名和属性值
private List propertyName = new ArrayList<>();
private List
5、主类
package com.spring.xml;
import com.spring.xml.bean.Car;
import com.spring.xml.utils.XmlApplicationContext;
import java.util.Map;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
XmlApplicationContext context = new XmlApplicationContext("applicationContext.xml");
Car car = (Car)context.getBean("car");
System.out.println(car);
Map map = context.getBeanMap();
System.out.println(map);
}
}
6、结果