从spring配置文件读取bean的方法

/**第一种 */
ApplicationContext ac = new FileSystemXmlApplicationContext("serviceContext.xml");
JurisdictionImp jurisdictionImp = (JurisdictionImp)ac.getBean("jurisdictionImp");/**第二种*/
FileSystemXmlApplicationContext ctx =new FileSystemXmlApplicationContext(CONTEXT_FILE);
JurisdictionImp jurisdictionImp = (JurisdictionImp) ctx.getBean("jurisdictionImp");
/**第三种*/
Resource resource = new ClassPathResource(CONTEXT_FILE);
XmlBeanFactory beanFactory = new XmlBeanFactory( resource);
JurisdictionImp jurisdictionImp=(JurisdictionImp) beanFactory.getBean("jurisdictionImp");
/**第四种*/
ServletContext context=request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
JurisdictionImp jurisdictionImp=(JurisdictionImp) ctx.getBean("jurisdictionImp");
/**第五种*/
/**
* 获得其它业务类对象
* */
public Object getBean(String name)
{
WebApplicationContext ctx
if (ctx == null)
{
ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(servlet.getServletContext());
}
return ctx.getBean(name);
}
  

你可能感兴趣的:(java,spring,bean,xml,servlet)