简单spring框架原理
首先定义一个接口:
???? public interface Action {
??? String excute(String msg);
}
然后定义一个类,来实现这个接口:
??
public class UppperCaseAction implements Action{
??? private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String excute(String msg) {
return (message+" "+msg).toUpperCase();
}
}
然后就是配置文件了:applicationContext.xml
?? <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
??? <bean name="uppercase" "+className+" "+property+" "+value);
map.put(mapping.getId(), mapping);
}
}
??? return map;
???? }
public static void main(String[] args){
ParseXml px=new ParseXml();
px.ParseApplicationContext();
}
}
通过xml当中的配置,与对应的类对应起来,准确地得到bean的实例:
????? package com.hcy.opxml;
public class Mapping {
?????? private String id;
?????? private String className;
?????? private String property;
?????? private String value;
??????
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
??????
}
接下来就是测试类:
public static void main(String[] args) {
ApplicationContext ac=new FileSystemXmlApplicationContext("src/applicationContext.xml");
??????? //通过反射机制得到bean的实例
??????? Action act=(Action)ac.getBean("uppercase");
??????? System.out.println(act.excute("yes"));
}