熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

模擬spring框架注入實現原理

2022-06-13   來源: Java開源技術 

  定義一些抽象的方法

  [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輔助包!


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28763.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.