Java实战(一)—Dom4J读取配置文件实现抽象工厂+反射
2015-02-28 23:39
483 查看
Dom4j(简单了解)
DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型。
DOM4J是开源组织提供的一个免费的、强大的XML解析工具,如果开发者需要在项目中使用那么需要下载并引入jar包即可。
抽象工厂 +反射
简单工厂 :适于需求明确,很少变动
工厂方法:适于单个系列产品的灵活变动
抽象工厂:适于多个系列产品的灵活配置
引入工厂目的:隐藏产品创建细节
初次学习抽象工厂+反射是在第一遍学习设计模式,当时用简单的XML进行解析,和现在使用的Dom4J原理是一样的,都是使用字符串进行灵活配置。
Dom4J解析的XML
工厂:
item模块:
Service层实现:
Servlet调用Service即可:
Dom4J读取配置文件实现抽象工厂+反射,与之前.NET解析XML本质是一样的,只是在语法上有些不同。通过使用XML使我们的系统更加灵活,耦合性更低。Dom4J的学习还需要深入,这里只是简单的用了一下。
DOM4J即Document Object Model for Java使用java技术以文档方式解析XML数据的模型。
DOM4J是开源组织提供的一个免费的、强大的XML解析工具,如果开发者需要在项目中使用那么需要下载并引入jar包即可。
抽象工厂 +反射
简单工厂 :适于需求明确,很少变动
工厂方法:适于单个系列产品的灵活变动
抽象工厂:适于多个系列产品的灵活配置
引入工厂目的:隐藏产品创建细节
初次学习抽象工厂+反射是在第一遍学习设计模式,当时用简单的XML进行解析,和现在使用的Dom4J原理是一样的,都是使用字符串进行灵活配置。
Dom4J解析的XML
<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?> <config> <db-info> <driver-name>oracle.jdbc.driver.OracleDrive</driver-name> <url>jdbc:oracle:thin:@localhost:1521:orcl</url>> <username>drp1</username> <password>drp1</password> </db-info> <dao-factory> <!-- Oracle数据库映射路径 --> <item-dao-factory>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4MyOracle</item-dao-factory> <!-- MySql数据库映射路径 --> <user-dao-factory>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4MySql</user-dao-factory> </dao-factory> </config></span></span>
工厂:
<span style="font-size:18px;">1、工厂接口 publicinterface ItemDaoFactory { publicItemDao createItemDao(); } 2、Oracle工厂 publicclass ItemDaoFactory4MyOracle implements ItemDaoFactory { @Override publicItemDao createItemDao() { returnnew ItemDao4MyOracleImpl(); } } 3、MySql工厂 public class ItemDaoFactory4MySql implements ItemDaoFactory { @Override publicItemDao createItemDao() { //TODO Auto-generated method stub returnnew ItemDao4MySqlImpl(); } }</span>
item模块:
<span style="font-size:18px;">1.接口 public interface ItemDao{ /** * 添加物料 * @param conn * @param item */ public void addItem(Connection conn,Item item); } 2.Oracle实现 public classItemDao4MyOracleImpl implements ItemDao { @Override public void addItem(Connection conn, Item item) { Stringsql="insert intot_items(item_no,item_name,spec,pattern,item_category_id,item_unit_id)"; sql+=" values(?,?,?,?,?,?)"; PreparedStatement pstmt=null; try { pstmt= conn.prepareStatement(sql); pstmt.setString(1,item.getItemNO()); pstmt.setString(2,item.getItemName()); pstmt.setString(3,item.getSpec()); pstmt.setString(4,item.getPattern()); pstmt.setString(5,item.getItemCategory().getId()); pstmt.setString(6,item.getItemUnit().getId()); pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); //用户看到的异常 throw new ApplicationException("添加物料失败!"); }finally{ DbUtil.close(pstmt); } } } 3.MySql实现 public classItemDao4MySqlImpl implements ItemDao { @Override publicvoid addItem(Connection conn, Item item) { // TODOAuto-generated method stub System.out.println("ItemDao4MySqlImpl.addItem()"); } }</span></span>
Service层实现:
<span style="font-size:18px;"> public classItemManagerImpl implements ItemManager { privateItemDaoFactory factory=null; @Override publicvoid addItem(Item item) { //根据需求在XML中解析出需要的连接的数据库路径 StringclassName=XMLConfigReader.getInstance().getDaoFactory("item-dao-factory"); try { //创建所需要的工厂 factory=(ItemDaoFactory)Class.forName(className).newInstance(); } catch(InstantiationException e) { e.printStackTrace(); } catch(IllegalAccessException e) { e.printStackTrace(); } catch(ClassNotFoundException e) { e.printStackTrace(); } //通过工厂创建具体的产品 ItemDaoitemDao=factory.createItemDao(); Connectionconn=null; //这里的catch有无都可以,如果有必须下面这样写,否则会拦截Dao层抛出的异常,无法显示给用户 try { //获取数据库连接 conn=DbUtil.getConnection(); //调用具体业务增删改 itemDao.addItem(conn,item); } catch(ApplicationException e) { throw e; }finally{ DbUtil.close(conn); } } }</span></span>
Servlet调用Service即可:
<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;"> public classAddItemServlet extends HttpServlet { publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { //取得表单数据 StringitemNo=request.getParameter("itemNo"); StringitemName=request.getParameter("itemName"); Stringspec=request.getParameter("spec"); Stringpattern=request.getParameter("pattern"); Stringcategory=request.getParameter("category"); Stringunit=request.getParameter("unit"); //构造Item对象 Itemitem=new Item(); item.setItemNO(itemNo); item.setItemName(itemName); item.setSpec(spec); item.setPattern(pattern); //构造物料类别 ItemCategoryitemCategory=new ItemCategory(); itemCategory.setId(category); item.setItemCategory(itemCategory); //构造物料单位 ItemUnititemUnit=new ItemUnit(); itemUnit.setId(unit); item.setItemUnit(itemUnit);</span><p style="margin: 0in 0in 0in 0.75in;"></p><p style="margin:0in;margin-left:.75in;font-family:楷体;font-size:12.0pt"><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;"> </span></p>
ItemManager itemManager=new ItemManagerImpl(); String errorMessage=""; try { itemManager.addItem(item); } catch (ApplicationException e) { errorMessage=e.getMessage(); } //重定向到主页面 response.sendRedirect(request.getContextPath()+"/basedata/item_maint.jsp?errorMessage="+URLEncoder.encode(errorMessage,"GB18030")); }
Dom4J读取配置文件实现抽象工厂+反射,与之前.NET解析XML本质是一样的,只是在语法上有些不同。通过使用XML使我们的系统更加灵活,耦合性更低。Dom4J的学习还需要深入,这里只是简单的用了一下。
相关文章推荐
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- Java中dom4j读取配置文件实现抽象工厂+反射
- Java中通过dom4j读取配置文件实现抽象工厂+反射
- JAVA开发6--java项目实现读取配置文件相关
- java用spring实现配置文件properties的读取
- java使用dom4j解析xml配置文件实现抽象工厂反射示例
- Java读取XML配置文件详细总结(dom4j方式)
- java 用单态方式实现数据库配置文件读取
- java xml配合实现读取配置文件登陆
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- java实现读取配置文件 (文件名、目录名或卷标语法不正确。)
- Java 过滤器读取配置文件实现过滤编码
- (转)实现对Java配置文件Properties的读取、写入与更新操作
- dom4j实现模拟Spring读取配置文件 注入
- JAVA技术:XML配置文件的读取处理
- java中读取配置文件的方法和数据库底层的编写
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)