package com.aochuang.lotteryserver.core;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Spring 上下文工厂类
*
* @author Administrator
*
*/
public class SpringFactory
{
public static final Logger log = LogManager.getLogger(SpringFactory.class);
public static final String CONFIG_FILE_PATH = "/bean-definition.xml";
public static ApplicationContext context = null;
/**
* 初始化上下文
*/
public static synchronized void init()
{
init(CONFIG_FILE_PATH);
}
/**
* 初始化上下文
*
* @param path
*/
public static synchronized void init(String path)
{
context = new ClassPathXmlApplicationContext(path);
}
/**
* 从上下文中得到bean
*
* @param name
* @return
*/
public static Object getBean(String name)
{
return context.getBean(name);
}
/**
* 从上下文件中得到bean
*
* @param <T>
* @param name
* @param requiredType
* @return
*/
public static <T> T getBean(String name, Class<T> requiredType)
{
return context.getBean(name, requiredType);
}
public static void main(String[] arg)
{
init();
}
}
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Spring 上下文工厂类
*
* @author Administrator
*
*/
public class SpringFactory
{
public static final Logger log = LogManager.getLogger(SpringFactory.class);
public static final String CONFIG_FILE_PATH = "/bean-definition.xml";
public static ApplicationContext context = null;
/**
* 初始化上下文
*/
public static synchronized void init()
{
init(CONFIG_FILE_PATH);
}
/**
* 初始化上下文
*
* @param path
*/
public static synchronized void init(String path)
{
context = new ClassPathXmlApplicationContext(path);
}
/**
* 从上下文中得到bean
*
* @param name
* @return
*/
public static Object getBean(String name)
{
return context.getBean(name);
}
/**
* 从上下文件中得到bean
*
* @param <T>
* @param name
* @param requiredType
* @return
*/
public static <T> T getBean(String name, Class<T> requiredType)
{
return context.getBean(name, requiredType);
}
public static void main(String[] arg)
{
init();
}
}