用spring获取bean违例
1:定义工程接口
package com.spdx.app.proxy;
public interface IBeanFactory
{
public Object createBean(String beanName);
}
2:定义抽象工程,实现单例
public abstract class AbstractBeanFactory implements IBeanFactory
{
private static String className = "com.spdx.app.proxy.SimpleBeanFactory";
private static Object lock = new Object();
private static AbstractBeanFactory instance;
public static AbstractBeanFactory getInstance()
{
if (null == instance)
{
synchronized (lock)
{
if (null == instance)
{
try
{
Class clazz = Class.forName(className);
instance = (AbstractBeanFactory) clazz.newInstance();
}
catch (ClassNotFoundException e)
{
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
}
}
}
return instance;
}
}
3:工厂实现
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SimpleBeanFactory extends AbstractBeanFactory
{
private ApplicationContext application = null;
private Resource resource = null;
private BeanFactory beanFactory = null;
public SimpleBeanFactory()
{
application = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
application = new FileSystemXmlApplicationContext("src/applicationContext.xml");
//resource = new ClassPathResource("applicationContext.xml");
//beanFactory = new XmlBeanFactory(resource);
System.out.println("init application...");
}
public Object createBean(String beanName)
{
return application.getBean(beanName);
//return beanFactory.getBean(beanName);
}
}
1:定义工程接口
package com.spdx.app.proxy;
public interface IBeanFactory
{
public Object createBean(String beanName);
}
2:定义抽象工程,实现单例
public abstract class AbstractBeanFactory implements IBeanFactory
{
private static String className = "com.spdx.app.proxy.SimpleBeanFactory";
private static Object lock = new Object();
private static AbstractBeanFactory instance;
public static AbstractBeanFactory getInstance()
{
if (null == instance)
{
synchronized (lock)
{
if (null == instance)
{
try
{
Class clazz = Class.forName(className);
instance = (AbstractBeanFactory) clazz.newInstance();
}
catch (ClassNotFoundException e)
{
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
}
}
}
}
return instance;
}
}
3:工厂实现
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SimpleBeanFactory extends AbstractBeanFactory
{
private ApplicationContext application = null;
private Resource resource = null;
private BeanFactory beanFactory = null;
public SimpleBeanFactory()
{
application = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
application = new FileSystemXmlApplicationContext("src/applicationContext.xml");
//resource = new ClassPathResource("applicationContext.xml");
//beanFactory = new XmlBeanFactory(resource);
System.out.println("init application...");
}
public Object createBean(String beanName)
{
return application.getBean(beanName);
//return beanFactory.getBean(beanName);
}
}