首页 > 代码库 > spring
spring
1.创建bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="userController" class="com.augmentum.oes.controller.UserController" scope="singleton"></bean> <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton"></bean> <bean id="userDaoImpl" class="com.augmentum.oes.dao.impl.UserDaoImpl" scope="singleton"></bean> <bean id="questionController" class="com.augmentum.oes.controller.QuestionController" scope="singleton"> <property name="questionServiceImpl" ref="questionServiceImpl" value=""/> </bean> <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton"> <property name="questionDaoImpl" ref="questionDaoImpl"/> </bean> <bean id="questionDaoImpl" class="com.augmentum.oes.dao.impl.QuestionDaoImpl" scope="singleton"> <property name="jdbcTemplete" ref="jdbcTemplete"/> </bean> <bean id="jdbcTemplete" class="com.augmentum.oes.common.JDBCTemplete" scope="singleton" ></bean> </beans>
2.存储model
package com.augmentum.oes.common; import java.util.ArrayList; import java.util.List; public class BeanConfig { private String id; private String className; private String scope; private List<BeanProperty> properties = new ArrayList<BeanProperty>(); public void addProperty(BeanProperty property) { properties.add(property); } public List<BeanProperty> getProperties() { return properties; } public void setProperties(List<BeanProperty> properties) { if (properties == null) { properties = new ArrayList<BeanProperty>(); } this.properties = properties; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } }
package com.augmentum.oes.common; public class BeanProperty { private String name; private String value; private String ref; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value =http://www.mamicode.com/ value; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } }
3.创建工厂
package com.augmentum.oes.common; import java.io.InputStream; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.augmentum.oes.servlet.DispatcherServlet; public class BeanFactory { private static Map<String, BeanConfig> beans = new HashMap<String, BeanConfig>(); private static Map<String, Object> objects = new HashMap<String, Object>(); private static BeanFactory beanFactory; public Object getBean(String id) { if (beans.containsKey(id)) { BeanConfig bean = beans.get(id); String scope = bean.getScope(); if (scope == null || scope.equals("")) { scope = "singleton"; } if (scope.equalsIgnoreCase("singleton")) { if (objects.containsKey(id)) { return objects.get(id); } } String className = bean.getClassName(); Class<?> clz = null; try { clz = Class.forName(className); Object object = clz.newInstance(); if (scope.equalsIgnoreCase("singleton")) { objects.put(id, object); } List<BeanProperty> beanProperties = bean.getProperties(); if (beanProperties != null || !beanProperties.isEmpty()) { for (BeanProperty beanProperty : beanProperties) { String name = beanProperty.getName(); String fistChar = name.substring(0,1); String leaveChar = name.substring(1); String methodName ="set"+ fistChar.toUpperCase()+leaveChar; Method method = null; Method[] methods = clz.getMethods(); //find in object method for (Method methodInClass : methods) { String methodNameInClass = methodInClass.getName(); if (methodNameInClass.equals(methodName)) { method = methodInClass; break; } } String ref = beanProperty.getRef(); String value = beanProperty.getValue(); if (ref != null && !ref.trim().equals("")) { //digui get this bean.xml have bean set Object Object refObj = this.getBean(ref); method.invoke(object, refObj); } else if (value != null && !value.trim().equals("")) { Class<?>[] parmts = method.getParameterTypes(); String propertyValue = beanProperty.getValue(); if (parmts[0]==String.class) { method.invoke(object, propertyValue); } if (parmts[0]==int.class) { method.invoke(object, Integer.parseInt(propertyValue)); } if (parmts[0]==boolean.class) { method.invoke(object, Boolean.parseBoolean(propertyValue)); } } } } return object; } catch (Exception e) { e.printStackTrace(); } } return null; } private BeanFactory(){} //thread join public static BeanFactory getInstance() { if (beanFactory == null) { beanFactory = new BeanFactory(); beanFactory.init(); } return beanFactory; } private void init() { InputStream inputStream = null; try { inputStream = DispatcherServlet.class.getClassLoader().getResourceAsStream("bean.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inputStream); Element element = document.getDocumentElement(); NodeList beanNodes = element.getElementsByTagName("bean"); if (beanNodes == null) { return; } int beanLength = beanNodes.getLength(); for (int i = 0; i < beanLength; i++) { Element beanElement = (Element) beanNodes.item(i); BeanConfig bean = new BeanConfig(); String id = beanElement.getAttribute("id"); bean.setId(id); String className = beanElement.getAttribute("class"); bean.setClassName(className); String scope = beanElement.getAttribute("scope"); bean.setScope(scope); beans.put(id, bean); NodeList beanPropertyNodes = beanElement.getElementsByTagName("property"); if (beanPropertyNodes != null|| !beanPropertyNodes.equals("")) { int beanPropertyLength = beanPropertyNodes.getLength(); for (int j = 0; j < beanPropertyLength; j++) { Element beanPropertyElement = (Element) beanPropertyNodes.item(j); BeanProperty beanProperty = new BeanProperty(); beanProperty.setName(beanPropertyElement.getAttribute("name")); beanProperty.setRef(beanPropertyElement.getAttribute("ref")); beanProperty.setValue(beanPropertyElement.getAttribute("value")); bean.addProperty(beanProperty); } } } } catch (Exception e) { e.printStackTrace(); } } }
4.set注入
spring
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。