- package org.apple.dao.ioc;
- public interface BeanFactory {
- public Object getBean(String name);
- }
- package org.apple.dao.ioc;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- import java.util.Map.Entry;
- public class PropertyBeanFactory implements BeanFactory {
- private Map<String, Object> map = new HashMap<String, Object>();
- @SuppressWarnings("unchecked")
- public PropertyBeanFactory(String configLocation) {
- super();
- Properties properties = new Properties();
- try {
- if (configLocation == null || "".equals(configLocation)) {
- configLocation = "beans.properties";
- }
- properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(configLocation));
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- Set<?> set = properties.entrySet();
- for (Iterator iterator = set.iterator(); iterator.hasNext();) {
- Map.Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
- String daoName = (String) entry.getKey();// daoName
- String daoImplname = (String) entry.getValue();// daoImplName
- Object clazz = null;
- try {
- clazz = Class.forName(daoImplname).newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- map.put(daoName, clazz);
- }
- }
- public Object getBean(String name) {
- return map.get(name);
- }
- }
- package org.apple.dao.ioc;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @SuppressWarnings("serial")
- public class BaseServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- this.doPost(req, resp);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.process(request, response);
- }
- @Override
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- }
- @Override
- protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
- BeanFactory beanFactory = (BeanFactory) getServletContext().getAttribute("beanFactory");
- Method[] methods = this.getClass().getMethods();
- for (Method method : methods) {
- if (method.getName().startsWith("set")) {
- String string = method.getName().substring(3);
- StringBuffer stringBuffer = new StringBuffer(string);
- stringBuffer.replace(0, 1, (string.charAt(0) + "").toLowerCase());
- string = stringBuffer.toString();
- try {
- Object bean = beanFactory.getBean(string);
- method.invoke(this, bean);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- super.service(arg0, arg1);
- }
- protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String method = request.getParameter("method");
- if (method == null || "".equals(method)) {
- execute(request, response);
- } else {
- try {
- Method method2 = this.getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
- method2.invoke(this, request, response);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public BaseServlet() {
- super();
- }
- public void destroy() {
- super.destroy();
- }
- // 默认方法
- public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- public void init() throws ServletException {
- }
- }
- package org.apple.dao.ioc;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- @SuppressWarnings("serial")
- public class InitDI extends HttpServlet {
- String configLocation = null;
- public InitDI() {
- super();
- }
- public void destroy() {
- }
- public void init(ServletConfig servletConfig) throws ServletException {
- System.out.println("-------get beans configLocation-------");
- configLocation = servletConfig.getInitParameter("configLocation");
- BeanFactory beanFactory = new PropertyBeanFactory(configLocation);
- servletConfig.getServletContext().setAttribute("beanFactory", beanFactory);
- System.out.println("-----------init ok---------------------");
- }
- }
- package org.apple.dao.ioc;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apple.dao.TestDao;
- @SuppressWarnings("serial")
- public class TestDaoServlet extends BaseServlet {
- public void destroy() {
- super.destroy();
- }
- private TestDao testDao;
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
- // response.sendRedirect(null)可以避免转发时request信息携带
- public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- testDao.test();
- }
- public void init() throws ServletException {
- }
- public void setTestDao(TestDao testDao) {
- this.testDao = testDao;
- }
- public TestDao getTestDao() {
- return testDao;
- }
- }
- package org.apple.dao.ioc;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apple.dao.TestDao;
- @SuppressWarnings("serial")
- public class TestDaoServlet extends BaseServlet {
- public void destroy() {
- super.destroy();
- }
- private TestDao testDao;
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
- // response.sendRedirect(null)可以避免转发时request信息携带
- public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- testDao.test();
- }
- public void init() throws ServletException {
- }
- public void setTestDao(TestDao testDao) {
- this.testDao = testDao;
- }
- public TestDao getTestDao() {
- return testDao;
- }
- }
- ================================================================
- package org.apple.dao;
- public interface TestDao {
- public void test();
- }
- package org.apple.dao.impl;
- import org.apple.dao.TestDao;
- public class TestDaoImpl implements TestDao {
- public void test() {
- System.out.println("test di");
- }
- }
- package org.apple.vo;
- public class Test {
- private int id;
- public void setId(int id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- }
- /SimpleWebIOC/src/beans.properties
- testDao=org.apple.dao.impl.TestDaoImpl
- web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>InitDI</servlet-name>
- <servlet-class>org.apple.dao.ioc.InitDI</servlet-class>
- <init-param>
- <param-name>configLocation</param-name>
- <param-value>beans.properties</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet>
- <servlet-name>TestDaoServlet</servlet-name>
- <servlet-class>org.apple.dao.ioc.TestDaoServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TestDaoServlet</servlet-name>
- <url-pattern>/servlet/TestDaoServlet</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
- //该片段来自于http://www.codesnippet.cn/detail/230620149851.html
来源: http://www.codesnippet.cn/detail/230620149851.html