⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 factory.java

📁 基于Spring2.5的精品课程网站
💻 JAVA
字号:
package org.adam.tools;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class Factory {
	private static BeanFactory beanFactory;
	  static {
	    try {
	      beanFactory = new ClassPathXmlApplicationContext(
	          new String[] { "applicationContext.xml" });
	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	  }

	  public static Object getBean(String beanName) {
	    if (beanName == null) {
	      return null;
	    }
	    try {
	      Object obj = beanFactory.getBean(beanName);
	      if (obj == null) {
	        System.err.println("Lookup Object[" + beanName + "] Error!");
	      }
	      return obj;
	    } catch (Exception ex) {
	      ex.printStackTrace();
	      return null;
	    }
	  }
	  
	  public static Object getBeanName(String name){
		  //AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		  Resource resource = new ClassPathResource("applicationContext.xml");
	      BeanFactory factory = new XmlBeanFactory(resource);
		  try{
			  return factory.getBean(name);
		  }catch(Exception e){
			  return null;
		  }finally{
			  //ctx.close();
			  
		  }	  
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -