分类:
2008-09-09 17:11:31
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class SpringtUtil {
private static SpringtUtil instance = new SpringtUtil();
private static ApplicationContext context;
public static final String spring_context_configfile = "spring.context.configfile";
public static Object getBean(String name) {
return getContext().getBean(name);
}
public static ApplicationContext getContext() {
return SpringtUtil.getInstance().context;
}
public static SpringtUtil getInstance()
{
return instance;
}
private SpringtUtil()
{
try
{
String springContextFileName =PropertiesUtil.getProperty(spring_context_configfile);
//System.out.println(springContextFileName);
context = new FileSystemXmlApplicationContext(springContextFileName);
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void main(String[] args)
{
try {
UserService userService= (UserService)SpringtUtil.getBean("userService");
userService.test("id", "name");
} catch (Exception e) {
e.printStackTrace();
}
}
}