spring 小技巧

1:得到文件路径

     得到工程下conf包下的文件,返回值可resource .getInputStream()

ClassPathResource resource = new ClassPathResource( "conf/customer_report_config.xml"); 

2:得到配置文件中的类

/**第一种 */
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);
}

  

更新中。。。

你可能感兴趣的:(spring)