java

位置:IT落伍者 >> java >> 浏览文章

模拟spring框架注入实现原理


发布日期:2020年08月19日
 
模拟spring框架注入实现原理

定义一些抽象的方法

[java]

package comhuxinspringinjectdao;

public interface Person {

public void save();

public void useAxe();

}

package comhuxinspringinjectdao;

public interface Person {

public void save();

public void useAxe();

}

[java]

package comhuxinspringinjectdao;

public interface Axe {

public void chop();

}

package comhuxinspringinjectdao;

public interface Axe {

public void chop();

}

实现的一些方法

[java]

package comhuxinspringinjectdaoimpl;

import comhuxinspringinjectdaoAxe;

import comhuxinspringinjectdaoPerson;

public class Chinese implements Person {

private Axe axe;

public Axe getAxe() {

return axe;

}

public void setAxe(Axe axe) {

thisaxe = axe;

}

public void save() {

Systemoutprintln(保存人的方法);

}

public void useAxe(){

axechop();

}

}

package comhuxinspringinjectdaoimpl;

import comhuxinspringinjectdaoAxe;

import comhuxinspringinjectdaoPerson;

public class Chinese implements Person {

private Axe axe;

public Axe getAxe() {

return axe;

}

public void setAxe(Axe axe) {

thisaxe = axe;

}

public void save() {

Systemoutprintln(保存人的方法);

}

public void useAxe(){

axechop();

}

}

[java]

package comhuxinspringinjectdaoimpl;

import comhuxinspringinjectdaoAxe;

public class StoneAxe implements Axe {

@Override

public void chop() {

Systemoutprintln(铁斧头砍柴真慢);

}

}

package comhuxinspringinjectdaoimpl;

import comhuxinspringinjectdaoAxe;

public class StoneAxe implements Axe {

@Override

public void chop() {

Systemoutprintln(铁斧头砍柴真慢);

}

}

这里是关键spring框架模拟的实现的一些原理!!!

[java]

package junittest;

import javabeansIntrospector;

import javabeansPropertyDescriptor;

import javalangreflectMethod;

import URL;

import javautilArrayList;

import javautilHashMap;

import javautilList;

import javautilMap;

import monsbeanutilsConvertUtils;

import orgdomjDocument;

import orgdomjElement;

import orgdomjXPath;

import orgdomjioSAXReader;

public class ApplicationContext {

List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();

Map<StringObject> singleton = new HashMap<StringObject>();

public ApplicationContext(){};

public ApplicationContext(String filename){

readXml(filename);

initBean();

thisinjectObject();

}

public void readXml(String filename){

SAXReader saxReader = new SAXReader();

Document document = null;

try{

//使用反射机制通过文件名加载文件路径

URL xmlPath = thisgetClass()getClassLoader()getResource(filename);

//通过文件路径获得这个文件的document对象

document = saxReaderread(xmlPath);

Map<StringString> nsMap = new HashMap<StringString>();

nsMapput(ns/schema/beans);//加入命名空间

XPath xsub = documentcreateXPath(//ns:beans/ns:bean);//创建beans/bean查询路径

xsubsetNamespaceURIs(nsMap);//设置命名空间

//获得所有路径下的节点

List<Element> beans = xsubselectNodes(document);//获取文档下所有bean节点

for(Element element : beans){

Systemoutprintln(elementattributeValue(id));

Systemoutprintln(elementattributeValue(class));

BeanInformation beanInformation = new BeanInformation();

beanInformationsetId(elementattributeValue(id));

beanInformationsetName(elementattributeValue(class));

XPath xref = elementcreateXPath(ns:property);//创建properties查询路径

xrefsetNamespaceURIs(nsMap);//设置命名空间

List<Element> propertys = xrefselectNodes(element);

for(Element property : propertys){

PropertyInformation propertyInformation = new PropertyInformation();

propertyInformationsetName(propertyattributeValue(name));

propertyInformationsetRef(propertyattributeValue(ref));

propertyInformationsetValue(propertyattributeValue(value));

beanInformationgetPropertyInformation()add(propertyInformation);

}

beansInformationadd(beanInformation);

}

} catch(Exception e){

eprintStackTrace();

}

}

public void initBean(){

for(BeanInformation beanInformation: beansInformation){

if(beanInformationgetName()!=null && !equals(beanInformationgetName())){

//通过反射机制根据名字初始化这个类

try {

singletonput(beanInformationgetId() ClassforName(beanInformationgetName())newInstance());

} catch (Exception e) {

eprintStackTrace();

}

}

}

}

/**

* 关于注入的实现

*/

private void injectObject() {

for(BeanInformation beanInformation : beansInformation){

Object bean = singletonget(beanInformationgetId());

if(bean!=null){

try {

PropertyDescriptor[] ps = IntrospectorgetBeanInfo(beangetClass())getPropertyDescriptors();

for(PropertyInformation propertyInformation : beanInformationgetPropertyInformation()){

for(PropertyDescriptor properdesc : ps){

if(propertyInformationgetName()equals(properdescgetName())){

Method setter = properdescgetWriteMethod();//获取属性的setter方法 private

if(setter!=null){

Object value = null;

if(propertyInformationgetRef()!=null && !equals(propertyInformationgetRef()trim())){

value = singletonget(propertyInformationgetRef());

}else{

value = nvert(propertyInformationgetValue() properdescgetPropertyType());

}

settersetAccessible(true);

setterinvoke(bean value);//把引用对象注入到属性

}

break;

}

}

}

} catch (Exception e) {

}

}

}

}

public Object getBean(String id){

return thissingletonget(id);

}

}

package junittest;

import javabeansIntrospector;

import javabeansPropertyDescriptor;

import javalangreflectMethod;

import URL;

import javautilArrayList;

import javautilHashMap;

import javautilList;

import javautilMap;

import monsbeanutilsConvertUtils;

import orgdomjDocument;

import orgdomjElement;

import orgdomjXPath;

import orgdomjioSAXReader;

public class ApplicationContext {

List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();

Map<StringObject> singleton = new HashMap<StringObject>();

public ApplicationContext(){};

public ApplicationContext(String filename){

readXml(filename);

initBean();

thisinjectObject();

}

public void readXml(String filename){

SAXReader saxReader = new SAXReader();

Document document = null;

try{

//使用反射机制通过文件名加载文件路径

URL xmlPath = thisgetClass()getClassLoader()getResource(filename);

//通过文件路径获得这个文件的document对象

document = saxReaderread(xmlPath);

Map<StringString> nsMap = new HashMap<StringString>();

nsMapput(ns/schema/beans);//加入命名空间

XPath xsub = documentcreateXPath(//ns:beans/ns:bean);//创建beans/bean查询路径

xsubsetNamespaceURIs(nsMap);//设置命名空间

//获得所有路径下的节点

List<Element> beans = xsubselectNodes(document);//获取文档下所有bean节点

for(Element element : beans){

Systemoutprintln(elementattributeValue(id));

Systemoutprintln(elementattributeValue(class));

BeanInformation beanInformation = new BeanInformation();

beanInformationsetId(elementattributeValue(id));

beanInformationsetName(elementattributeValue(class));

XPath xref = elementcreateXPath(ns:property);//创建properties查询路径

xrefsetNamespaceURIs(nsMap);//设置命名空间

List<Element> propertys = xrefselectNodes(element);

for(Element property : propertys){

PropertyInformation propertyInformation = new PropertyInformation();

propertyInformationsetName(propertyattributeValue(name));

propertyInformationsetRef(propertyattributeValue(ref));

propertyInformationsetValue(propertyattributeValue(value));

beanInformationgetPropertyInformation()add(propertyInformation);

}

beansInformationadd(beanInformation);

}

} catch(Exception e){

eprintStackTrace();

}

}

public void initBean(){

for(BeanInformation beanInformation: beansInformation){

if(beanInformationgetName()!=null && !equals(beanInformationgetName())){

//通过反射机制根据名字初始化这个类

try {

singletonput(beanInformationgetId() ClassforName(beanInformationgetName())newInstance());

} catch (Exception e) {

eprintStackTrace();

}

}

}

}

/**

* 关于注入的实现

*/

private void injectObject() {

for(BeanInformation beanInformation : beansInformation){

Object bean = singletonget(beanInformationgetId());

if(bean!=null){

try {

PropertyDescriptor[] ps = IntrospectorgetBeanInfo(beangetClass())getPropertyDescriptors();

for(PropertyInformation propertyInformation : beanInformationgetPropertyInformation()){

for(PropertyDescriptor properdesc : ps){

if(propertyInformationgetName()equals(properdescgetName())){

Method setter = properdescgetWriteMethod();//获取属性的setter方法 private

if(setter!=null){

Object value = null;

if(propertyInformationgetRef()!=null && !equals(propertyInformationgetRef()trim())){

value = singletonget(propertyInformationgetRef());

}else{

value = nvert(propertyInformationgetValue() properdescgetPropertyType());

}

settersetAccessible(true);

setterinvoke(bean value);//把引用对象注入到属性

}

break;

}

}

}

} catch (Exception e) {

}

}

}

}

public Object getBean(String id){

return thissingletonget(id);

}

}

[java]

package junittest;

import javautilHashSet;

import javautilSet;

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) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Set<PropertyInformation> getPropertyInformation() {

return propertyInformation;

}

public void setPropertyInformation(Set<PropertyInformation> propertyInformation) {

thispropertyInformation = propertyInformation;

}

}

package junittest;

import javautilHashSet;

import javautilSet;

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) {

thisid = id;

}

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public Set<PropertyInformation> getPropertyInformation() {

return propertyInformation;

}

public void setPropertyInformation(Set<PropertyInformation> propertyInformation) {

thispropertyInformation = propertyInformation;

}

}

[java]

package junittest;

public class PropertyInformation {

private String name;

private String ref;

private String value;

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public String getRef() {

return ref;

}

public void setRef(String ref) {

thisref = ref;

}

public String getValue() {

return value;

}

public void setValue(String value) {

thisvalue = value;

}

}

package junittest;

public class PropertyInformation {

private String name;

private String ref;

private String value;

public String getName() {

return name;

}

public void setName(String name) {

thisname = name;

}

public String getRef() {

return ref;

}

public void setRef(String ref) {

thisref = ref;

}

public String getValue() {

return value;

}

public void setValue(String value) {

thisvalue = value;

}

}

测试类

[java]

package junittest;

import comhuxinspringinjectdaoPerson;

public class Test {

public static void main(String[] args) {

ApplicationContext ac = new ApplicationContext(applicationContextxml);

Person person = (Person)acgetBean(chinese);

personsave();

personuseAxe();

}

}

package junittest;

import comhuxinspringinjectdaoPerson;

public class Test {

public static void main(String[] args) {

ApplicationContext ac = new ApplicationContext(applicationContextxml);

Person person = (Person)acgetBean(chinese);

personsave();

personuseAxe();

}

}

[html]

<?xml version= encoding=UTF?>

<beans xmlns=/schema/beans

xmlns:xsi=//XMLSchemainstance

xmlns:context=/schema/context

xsi:schemaLocation=/schema/beans

/schema/beans/springbeansxsd

/schema/context

/schema/context/springcontextxsd>

<bean id=chinese class=comhuxinspringinjectdaoimplChinese>

<property name=axe ref=stoneAxe/>

</bean>

<bean id=stoneAxe class=comhuxinspringinjectdaoimplStoneAxe/>

</beans>

<?xml version= encoding=UTF?>

<beans xmlns=/schema/beans

xmlns:xsi=//XMLSchemainstance

xmlns:context=/schema/context

xsi:schemaLocation=/schema/beans

/schema/beans/springbeansxsd

/schema/context

/schema/context/springcontextxsd>

<bean id=chinese class=comhuxinspringinjectdaoimplChinese>

<property name=axe ref=stoneAxe/>

</bean>

<bean id=stoneAxe class=comhuxinspringinjectdaoimplStoneAxe/>

</beans>

补充说明 需要导入domj相应的辅助包和junit辅助包!

               

上一篇:hibernate中获取关联属性为null的方式

下一篇:如何在MyEclipse快速搭建Hibernate应用