1. 程式人生 > >(十六)解耦合

(十六)解耦合

sel cnblogs ktr lec object exception 1.0 tar 問題

導入xml解析的jar包

  http://pan.baidu.com/s/1dFap53j

/store/src/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="CategoryService" class="com.louis.service.impl.CategoryServiceImpl"></bean>
    <bean id="ProductService" class="com.louis.service.impl.ProductServiceImpl"></bean>
    <bean id="UserService" class
="com.louis.service.impl.UserServiceImpl"></bean> <bean id="ProductDao" class="com.louis.dao.impl.ProductDaoImpl"/> <bean id="UserDao" class="com.louis.dao.impl.UserDaoImpl"/> <bean id="CategoryDao" class="com.louis.dao.impl.CategoryDaoImpl"/> </beans>

com.louis.utils.BeanFactory

package com.louis.utils;

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

public class BeanFactory {
    public static Object getBean(String id) {
        //通過id獲取一個指定的實現類
        //1、導入jar包
        
        
        try
{ //2、獲取document對象 Document document = new SAXReader().read(BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml")); //3、獲取指定的bean對象 Element element = (Element) document.selectSingleNode("//bean[@id=‘"+id+"‘]"); //4、獲取bean對象的class屬性 String value = element.attributeValue("class"); //5、反射 return Class.forName(value).newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }

com.louis.web.servlet.CategoryServlet使用

    CategoryService categoryService = (CategoryService) BeanFactory.getBean("CategoryService");

問題

java工程項目路徑的獲取

java工程編譯路徑

javaweb工程項目路徑的獲取

javaweb工程編譯路徑

(十六)解耦合