手写简单的ioc容器代码
依赖
< dependency>
< groupId> org.dom4j</ groupId>
< artifactId> dom4j</ artifactId>
< version> 2.1.1</ version>
</ dependency>
< dependency>
< groupId> jaxen</ groupId>
< artifactId> jaxen</ artifactId>
< version> 1.1.6</ version>
</ dependency>
自定义ioc容器
<?xml version="1.0" encoding="UTF-8" ?>
< vip3>
< entity id = " tiga" class = " com.softeem.entity.Cartoon" >
< property name = " cartoonName" value = " 迪迦奥特曼" />
</ entity>
</ vip3>
实体类
package com. softeem. entity ;
public class Cartoon {
private String cartoonName;
public String getCartoonName ( ) {
return cartoonName;
}
public void setCartoonName ( String cartoonName) {
this . cartoonName = cartoonName;
}
@Override
public String toString ( ) {
return "Cartoon{" +
"cartoonName='" + cartoonName + '\'' +
'}' ;
}
}
上下文的接口
package com. softeem. context ;
public interface ApplicationContext {
Object getBean ( String name) ;
}
容器解析
package com. softeem. context ;
import org. dom4j. Document ;
import org. dom4j. DocumentException ;
import org. dom4j. Element ;
import org. dom4j. Node ;
import org. dom4j. io. SAXReader ;
import java. io. File ;
import java. lang. reflect. InvocationTargetException ;
import java. lang. reflect. Method ;
import java. util. HashMap ;
import java. util. List ;
import java. util. Map ;
import java. util. concurrent. ConcurrentHashMap ;
public class ClassPathXmlApplicationContext implements ApplicationContext {
private Map container = new HashMap ( ) ;
public ClassPathXmlApplicationContext ( ) {
try {
String path =
this . getClass ( ) . getResource ( "/applicationContext.xml" ) . getPath ( ) ;
System . out. println ( "配置文件的路径为:" + path) ;
SAXReader saxReader = new SAXReader ( ) ;
Document document = saxReader. read ( new File ( path) ) ;
List < Node > entityList = document. getRootElement ( ) . selectNodes ( "entity" ) ;
for ( Node node : entityList) {
Element entity = ( Element ) node;
String id = entity. attributeValue ( "id" ) ;
System . out. println ( "id的值为:" + id) ;
String className = entity. attributeValue ( "class" ) ;
System . out. println ( "class的值为:" + className) ;
Class < ? > clazz = Class . forName ( className) ;
Object instance = clazz. newInstance ( ) ;
List < Node > propertyList = entity. selectNodes ( "property" ) ;
for ( Node propertyNode : propertyList) {
Element property = ( Element ) propertyNode;
String name = property. attributeValue ( "name" ) ;
System . out. println ( "属性的名字为:" + name) ;
String value = property. attributeValue ( "value" ) ;
System . out. println ( "希望注入属性的值为:" + value) ;
String setMethod = "set" + name. substring ( 0 , 1 ) . toUpperCase ( ) +
name. substring ( 1 ) ;
System . out. println ( "拼接出的set方法的名字为:" + setMethod) ;
Method method = clazz. getMethod ( setMethod, String . class ) ;
method. invoke ( instance, value) ;
}
container. put ( id, instance) ;
}
System . out. println ( "容器初始化完毕!" ) ;
} catch ( DocumentException e) {
e. printStackTrace ( ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( NoSuchMethodException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
}
@Override
public Object getBean ( String name) {
return container. get ( name) ;
}
}
测试
package com. softeem ;
import com. softeem. context. ClassPathXmlApplicationContext ;
import com. softeem. entity. Cartoon ;
public class App {
public static void main ( String [ ] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ( ) ;
Cartoon tiga = ( Cartoon ) context. getBean ( "tiga" ) ;
System . out. println ( tiga) ;
}
}