[树梢]利用dom4j模拟spring的getbean功能

本文介绍Spring框架中Bean的XML配置方法,并通过Dom4j库解析这些配置,实例演示了如何从XML文件中读取并实例化Java Bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.beans.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"
  xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop" 
   xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
   
 
 <!--  
   <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/affairs" />
      <property  name="username" value="root"/>
      <property name="password" value="123456"/>
   </bean>
   -->
   <bean id="student" class="testspring.bean.Student" >
       <!-- collaborators and configuration for this bean go here -->
        <property name = "clazz" value = "三年二班"></property>
   </bean>
</beans>

2.pojo类Student

/**
 * 
 */
package testspring.bean;

/**
 * @author 无痕菌
 *
 */
public class Student {
    private int id = 1;
    private String name  = "张三";
    private String clazz = "大四一班";
    /**
     * @return id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id 要设置的 id
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name 要设置的 name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return clazz
     */
    public String getClazz() {
        return clazz;
    }
    /**
     * @param clazz 要设置的 clazz
     */
    public void setClazz(String clazz) {
        this.clazz = clazz;
    } }
 

3.dom4j实现spring的getbean功能的模拟

/**
 * 
 */
package springReadBeanTest;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import testspring.bean.Student;

/**
 * @author 无痕菌
 *
 */
public class Dom4jXmlParseGetBean {

    /**
    *作者:alice
    *  日期:2022年1月21日
    *返回值:void
     *参数: @param args
     * @throws DocumentException 
     */
    public static void main(String[] args) throws DocumentException  {
        
        File file = new File("src/springReadBeanTest/Beans.xml");
        SAXReader reader = new SAXReader();
        // 读取文件作为文档
        Document doc = reader.read(file);
        // 获取文档的根元素
        Element root = doc.getRootElement();
        // 根据跟元素找到全部的子节点
        Iterator<Element> iter = root.elementIterator();
        while(iter.hasNext()){
            
        Element name = iter.next();
        if(name.getName().contentEquals("bean")){
            String aString = name.attributeValue("class");
            System.err.println(aString);
        try {
            Student student = (Student) Class.forName(aString).newInstance();
            System.err.println(student.getName());
        } catch (InstantiationException|IllegalAccessException |ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } 
        //System.out.println("value = " + name.getText());
        }

        /* 使用DocumentHelper来创建 Document对象
        Document document = DocumentHelper.createDocument();
        // 创建元素并设置关系
        Element person = document.addElement("person");
        Element name = person.addElement("name");
        Element age = person.addElement("age");
        // 设置文本
        name.setText("shsxt");
        age.setText("10");
        // 创建格式化输出器
        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("utf-8");
        // 输出到文件
        File file = new File("resource/outputdom4j.xml");
        XMLWriter writer = new XMLWriter(new FileOutputStream(new
        File(file.getAbsolutePath())),of);
        // 写出
        writer.write(document);
        writer.flush();
        writer.close();
        */
        
        
        
        

    }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值