ClassPathXmlApplicationContext会自己在CLASSes里面找,不过我只是把配置文件放在src文件下成功找到过,String[] paths = { "/applicationContextDataSource.xml",
"/applicationContextService.xml" };
System.out.println(System.getProperty("user.dir"));
ctx=new ClassPathXmlApplicationContext(paths); 此时配置文件放在SRC目录下,由此得知也可如FileSystemXmlApplicationContext指定路径得到
另外FileSystemXmlApplicationContext需要指定地址,默认为项目的根文件夹 例子
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/org/nieweigou/springIoc/bean.xml"); 结果成功
ApplicationContext ctx=new FileSystemXmlApplicationContext("/web/WEB-INF/dataAccessContext-local.xml"); 也成功,放在web/web-inf/目录下
XmlWebApplicationContext为WEB得到上下文方式,请见另一篇BLOG有详细介绍
SPRING 的得到BEAN 简单的可以用BeanFactory得到 ,ApplicationContext本来就是继承BeanFactory,提供了更多的功能而已。
spring为ApplicationContext提供的3种实现分别为:ClassPathXmlApplicationContext,FileSystemXmlApplicationContext和XmlWebApplicationContext,其中XmlWebApplicationContext是专为Web工程定制的。使用举例如下:
1. FileSystemXmlApplicationContext
eg1. ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件
eg2.
String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载单个配置文件
eg3.
ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");//根据具体路径加载文件
2. ClassPathXmlApplicationContext
eg1. ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
eg2.
String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new ClassPathXmlApplication(locations);
注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。
3. XmlWebApplicationContext
eg1. ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//web下得到路径一般如下:
上文中说过,当前用户目录,即相对于System.getProperty("user.dir")返回的路径。
对于JavaEE服务器,这可能是服务器的某个路径,这个并没有统一的规范!
而不是我们发布的Web应用程序的根目录!
这样,在Web应用程序中,我们绝对不能使用相对于当前用户目录的相对路径。
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
这样,我们只需要提供相对于Web应用程序根目录的路径,就可以构建出定位资源的绝对路径。
这是我们开发Web应用程序时一般所采取的策略。
/**
* 2007-6-25
*
*/
package org.chuangqiao.inWeb.service;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* ����:��工厂类,用以得到一些service,也可以用继承ACTIONX,不过此类就脱离了继承 方便其他地方用到
* nieweiguo
*/
public class ServiceFactory {
//protected static ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");
protected static ApplicationContext ctx=null;
static
{
//在Application环境下使用
/*String[] paths = { "/applicationContextDataSource.xml",
"/applicationContextService.xml" };
ctx = new ClassPathXmlApplicationContext(paths);
*/
//在web环境下使用
// ctx = BaseAction.getWebApplicationContext();
}
public static Object getService(String serviceName)
{
/*ServletContext servletContext=this.getServlet().getServletContext();
HttpSession session = org.openamf.RequestContext.getHttpSession(true);
ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(session.getServletContext());
ctx=new FileSystemXmlApplicationContext("/applicationContext.xml"); */
//System.out.println(ServletContext.getRealPath("/"));
String[] paths = { "/applicationContextDataSource.xml",
"/applicationContextService.xml" };
System.out.println(System.getProperty("user.dir"));
ctx=new ClassPathXmlApplicationContext(paths);
return ctx.getBean(serviceName);
}
public static Object getService(Class serviceInrterface)
{
return getService(serviceInrterface.getName());
}
}
//在action中得到spring bean
public class IndexAction extends Action{
private PrdocuMgr prdocuMgr;
public void setServlet(ActionServlet actionServle){
super.setServlet(actionServle);
ServletContext servletContext=actionServle.getServletContext();
WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
prdocuMgr=(PrdocuMgr )wac.getBean("prdocuMgr");
}
}