少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

方法一:
package com.abin.lee.queue;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * 获取spring容器,以访问容器中定义的其他bean
 *
 * @author lyltiger
 * @since MOSTsView 3.0 2009-11-16
 */
public class SpringContextUtil implements ApplicationContextAware {

 // Spring应用上下文环境
 private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
   "com/abin/lee/spring/applicationContext-queue.xml");

 /**
  * 实现ApplicationContextAware接口的回调方法,设置上下文环境
  *
  * @param applicationContext
  */
 public void setApplicationContext(ApplicationContext applicationContext) {
  SpringContextUtil.applicationContext = applicationContext;
 }

 /**
  * @return ApplicationContext
  */
 public static ApplicationContext getApplicationContext() {
  return applicationContext;
 }

 /**
  * 获取对象 这里重写了bean方法,起主要作用
  *
  * @param name
  * @return Object 一个以所给名字注册的bean的实例
  * @throws BeansException
  */
 public static Object getBean(String name) throws BeansException {
  return applicationContext.getBean(name);
 }

}





方法二:

package com.abin.lee.queue;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BeanFactoryUtil {
 private static BeanFactory factory = new XmlBeanFactory(
   new ClassPathResource(
     "com/abin/lee/spring/applicationContext-queue.xml"));

 public static BeanFactory getFactory() {
  return factory;
 }

 public static void setFactory(BeanFactory factory) {
  BeanFactoryUtil.factory = factory;
 }
 
 public static Object getBean(String name){
  return factory.getBean(name);
 }
}







具体用法:

package com.abin.lee.queue;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueueServlet extends HttpServlet{
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Map map=request.getParameterMap();
  String name1=(String)((Object[])map.get("name1"))[0];
  String name2=(String)((Object[])map.get("name2"))[0];
  MakeQueue makeQueue = (MakeQueue)BeanFactoryUtil.getBean("makeQueue");//bean的名称
  System.out.println(makeQueue.queueSize());
  makeQueue.addQueue(name1);
  makeQueue.addQueue(name2);
  System.out.println(makeQueue.queueSize());
  
  ServletOutputStream out=response.getOutputStream();
  BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
  writer.write("success");
  writer.flush();
  writer.close();
 }
}



posted on 2012-08-20 11:34 abin 阅读(2150) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问