package org.springframework.context;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframew0rk.context.Autowired;
import org.springframew0rk.context.Controller;
import org.springframew0rk.context.Repository;
import org.springframew0rk.context.Service;
import com.spring.service.StudentService;
public class AnnotationXmlApplicationContext implements ApplicationContext {
private Map<String , Object> map0 = new HashMap<String,Object>(); //存放(id,class产生的对象)
public AnnotationXmlApplicationContext(String xml){
try {
this.parse(xml);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void parse(String xml) throws Exception{
// TODO Auto-generated method stub
SAXReader sx = new SAXReader();
Document doc = sx.read(new File("src/"+xml));
Element ele = doc.getRootElement();
Element com = ele.element("component-scan");
String packname = com.attributeValue("base-package");
String realpackname = packname.replaceAll("\\.", "/");
File file = new File("src/"+realpackname);
componentscan(file.listFiles());//扫描所有的类
discan(map0);
}
//扫描所有类后,对其方法进入注入
private void discan(Map<String, Object> map02) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// TODO Auto-generated method stub
for(Object obj : map02.values()){ //得到map中的所有值
Class c = obj.getClass();
//查找方法,哪个方法有@Autowired
Method[] ms = c.getMethods();
for(Method m : ms){
//setSS(StudentService ss);
if(m.getAnnotation(Autowired.class)!=null){
//找到要注入的方法后,再找到方法中要注入的类型
Class cc = m.getParameterTypes()[0];
System.out.println(cc);
//在map中找到和这个类型匹配的进行注入
for(Object o : map02.values()){
Class clazz[] = o.getClass().getInterfaces();
if(clazz.length != 0){
if(cc == clazz[0]){//因为方法中的参数是接口,所以我们要找到注入对象的接口相匹配的
m.invoke(obj, new Object[]{o});
}
}
}
}
}
}
}
//扫描所有的组件,即扫描所有指定包下面的类
private void componentscan(File[] files) throws Exception {
// TODO Auto-generated method stub
for(File file : files){
if(file.isFile()){
String filePath = file.getPath();
//得到类的全路径
String path = filePath.substring(4).split("\\.")[0].replaceAll("\\\\", "\\.");
Class c = Class.forName(path);
if(c.getAnnotation(Service.class)!=null ||
c.getAnnotation(Controller.class)!=null ||
c.getAnnotation(Repository.class)!=null
){
Object obj = c.newInstance(); //产生一个实例 ,加入到map中
String id = c.getSimpleName().substring(0,1).toLowerCase()+c.getSimpleName().substring(1);
map0.put(id, obj);
}
}else{
componentscan(file.listFiles());
}
}
}
@Override
public Object getBean(String id) {
// TODO Auto-generated method stub
return map0.get(id);
}
public static void main(String[] args) {
new AnnotationXmlApplicationContext("spring_annotation.xml");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
spring2.zip (37个子文件)
spring2
src
com
spring
dao
impl
StudentDAOImpl.java 667B
StudentDAO.java 157B
action
StudentAction.java 515B
bean
Bean.java 54B
service
impl
StudentServiceImpl.java 819B
StudentService.java 168B
test
Test.java 682B
org
springframework
context
AnnotationXmlApplicationContext.java 3KB
ApplicationContext.java 123B
ClassPathXmlApplicationContext.java 3KB
springframew0rk
context
Autowired.java 329B
Controller.java 310B
Service.java 307B
Repository.java 310B
spring.xml 399B
spring_annotation.xml 1KB
bin
com
spring
dao
StudentDAO.class 188B
impl
StudentDAOImpl.class 926B
action
StudentAction.class 980B
bean
Bean.class 269B
service
impl
StudentServiceImpl.class 1KB
StudentService.class 200B
test
Test.class 887B
org
springframework
context
ApplicationContext.class 204B
ClassPathXmlApplicationContext.class 4KB
AnnotationXmlApplicationContext.class 5KB
springframew0rk
context
Autowired.class 412B
Service.class 393B
Repository.class 399B
Controller.class 399B
spring.xml 399B
spring_annotation.xml 1KB
.classpath 427B
.settings
org.eclipse.jdt.core.prefs 629B
.project 383B
lib
dom4j-1.6.1.jar 307KB
commons-logging-1.0.4.jar 37KB
共 37 条
- 1
资源评论
- tiaozhanmaidi2013-04-16讲解的是挺详细的,谢谢分享
- Hsienhua2014-06-17资料还行,就是分有点高
- cengjingyiwei2013-04-23确实很牛B~
zou676990
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功