标签:通过 web beans 指定 导入 color document get public
http://pan.baidu.com/s/1dFap53j
<?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>
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; } }
CategoryService categoryService = (CategoryService) BeanFactory.getBean("CategoryService");
java工程项目路径的获取
java工程编译路径
javaweb工程项目路径的获取
javaweb工程编译路径
标签:通过 web beans 指定 导入 color document get public
原文地址:http://www.cnblogs.com/Michael2397/p/7659244.html