定義一些抽象的方法
[java]
package com
public interface Person {
public void save();
public void useAxe();
}
package com
public interface Person {
public void save();
public void useAxe();
}
[java]
package com
public interface Axe {
public void chop();
}
package com
public interface Axe {
public void chop();
}
實現的一些方法
[java]
package com
import com
import com
public class Chinese implements Person {
private Axe axe;
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this
}
public void save() {
System
}
public void useAxe(){
axe
}
}
package com
import com
import com
public class Chinese implements Person {
private Axe axe;
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this
}
public void save() {
System
}
public void useAxe(){
axe
}
}
[java]
package com
import com
public class StoneAxe implements Axe {
@Override
public void chop() {
System
}
}
package com
import com
public class StoneAxe implements Axe {
@Override
public void chop() {
System
}
}
這裡是關鍵spring框架模擬的實現的一些原理!!!
[java]
package junit
import java
import java
import java
import
import java
import java
import java
import java
import mons
import org
import org
import org
import org
public class ApplicationContext {
List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();
Map<String
public ApplicationContext(){};
public ApplicationContext(String filename){
readXml(filename);
initBean();
this
}
public void readXml(String filename){
SAXReader saxReader = new SAXReader();
Document document = null;
try{
//使用反射機制
URL xmlPath = this
//通過文件路徑獲得這個文件的document對象
document = saxReader
Map<String
nsMap
XPath xsub = document
xsub
//獲得所有路徑下的節點
List<Element> beans = xsub
for(Element element : beans){
System
System
BeanInformation beanInformation = new BeanInformation();
beanInformation
beanInformation
XPath xref = element
xref
List<Element> propertys = xref
for(Element property : propertys){
PropertyInformation propertyInformation = new PropertyInformation();
propertyInformation
propertyInformation
propertyInformation
beanInformation
}
beansInformation
}
} catch(Exception e){
e
}
}
public void initBean(){
for(BeanInformation beanInformation: beansInformation){
if(beanInformation
//通過反射機制
try {
singleton
} catch (Exception e) {
e
}
}
}
}
/**
* 關於注入的實現
*/
private void injectObject() {
for(BeanInformation beanInformation : beansInformation){
Object bean = singleton
if(bean!=null){
try {
PropertyDescriptor[] ps = Introspector
for(PropertyInformation propertyInformation : beanInformation
for(PropertyDescriptor properdesc : ps){
if(propertyInformation
Method setter = properdesc
if(setter!=null){
Object value = null;
if(propertyInformation
value = singleton
}else{
value = nvert(propertyInformation
}
setter
setter
}
break;
}
}
}
} catch (Exception e) {
}
}
}
}
public Object getBean(String id){
return this
}
}
package junit
import java
import java
import java
import
import java
import java
import java
import java
import mons
import org
import org
import org
import org
public class ApplicationContext {
List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();
Map<String
public ApplicationContext(){};
public ApplicationContext(String filename){
readXml(filename);
initBean();
this
}
public void readXml(String filename){
SAXReader saxReader = new SAXReader();
Document document = null;
try{
//使用反射機制
URL xmlPath = this
//通過文件路徑獲得這個文件的document對象
document = saxReader
Map<String
nsMap
XPath xsub = document
xsub
//獲得所有路徑下的節點
List<Element> beans = xsub
for(Element element : beans){
System
System
BeanInformation beanInformation = new BeanInformation();
beanInformation
beanInformation
XPath xref = element
xref
List<Element> propertys = xref
for(Element property : propertys){
PropertyInformation propertyInformation = new PropertyInformation();
propertyInformation
propertyInformation
propertyInformation
beanInformation
}
beansInformation
}
} catch(Exception e){
e
}
}
public void initBean(){
for(BeanInformation beanInformation: beansInformation){
if(beanInformation
//通過反射機制
try {
singleton
} catch (Exception e) {
e
}
}
}
}
/**
* 關於注入的實現
*/
private void injectObject() {
for(BeanInformation beanInformation : beansInformation){
Object bean = singleton
if(bean!=null){
try {
PropertyDescriptor[] ps = Introspector
for(PropertyInformation propertyInformation : beanInformation
for(PropertyDescriptor properdesc : ps){
if(propertyInformation
Method setter = properdesc
if(setter!=null){
Object value = null;
if(propertyInformation
value = singleton
}else{
value = nvert(propertyInformation
}
setter
setter
}
break;
}
}
}
} catch (Exception e) {
}
}
}
}
public Object getBean(String id){
return this
}
}
[java]
package junit
import java
import java
public class BeanInformation {
private String id;
private String name;
private Set<PropertyInformation> propertyInformation = new HashSet<PropertyInformation>();
public String getId() {
return id;
}
public void setId(String id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public Set<PropertyInformation> getPropertyInformation() {
return propertyInformation;
}
public void setPropertyInformation(Set<PropertyInformation> propertyInformation) {
this
}
}
package junit
import java
import java
public class BeanInformation {
private String id;
private String name;
private Set<PropertyInformation> propertyInformation = new HashSet<PropertyInformation>();
public String getId() {
return id;
}
public void setId(String id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public Set<PropertyInformation> getPropertyInformation() {
return propertyInformation;
}
public void setPropertyInformation(Set<PropertyInformation> propertyInformation) {
this
}
}
[java]
package junit
public class PropertyInformation {
private String name;
private String ref;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this
}
public String getValue() {
return value;
}
public void setValue(String value) {
this
}
}
package junit
public class PropertyInformation {
private String name;
private String ref;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this
}
public String getValue() {
return value;
}
public void setValue(String value) {
this
}
}
測試類
[java]
package junit
import com
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ApplicationContext(
Person person = (Person)ac
person
person
}
}
package junit
import com
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ApplicationContext(
Person person = (Person)ac
person
person
}
}
[html]
<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:context=
xsi:schemaLocation=
/schema/beans/spring
/schema/context
/schema/context/spring
<bean id=
<property name=
</bean>
<bean id=
</beans>
<?xml version=
<beans xmlns=
xmlns:xsi=
xmlns:context=
xsi:schemaLocation=
/schema/beans/spring
/schema/context
/schema/context/spring
<bean id=
<property name=
</bean>
<bean id=
</beans>
補充說明
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28763.html