java

位置:IT落伍者 >> java >> 浏览文章

使用SpringtUtil获取Spring IoC容器中的Bean


发布日期:2021年12月06日
 
使用SpringtUtil获取Spring IoC容器中的Bean
功能

方便获取Spring IoC容器中的Bean调用方法getBean(String name)

import orgntextApplicationContext;

import orgntextsupportClassPathXmlApplicationContext;

import orgntextsupportFileSystemXmlApplicationContext;

public class SpringtUtil {

private static SpringtUtil instance = new SpringtUtil();

private static ApplicationContext context;

public static final String spring_context_configfile = nfigfile;

public static Object getBean(String name) {

return getContext()getBean(name);

}

public static ApplicationContext getContext() {

return SpringtUtilgetInstance(ntext;

}

public static SpringtUtil getInstance()

{

return instance;

}

private SpringtUtil()

{

try

{

String springContextFileName =PropertiesUtilgetProperty(spring_context_configfile);

//Systemoutprintln(springContextFileName);

context = new FileSystemXmlApplicationContext(springContextFileName);

} catch (Throwable t) {

tprintStackTrace();

}

}

public static void main(String[] args)

{

try {

UserService userService= (UserService)SpringtUtilgetBean(userService);

userServicetest(id name);

} catch (Exception e) {

eprintStackTrace();

}

}

}

               

上一篇:用spring简单实现发送邮件

下一篇:Hibernate数据源不得不注意的问题