迷惑,spring&&web

 

Applicationcontext

如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了。ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用的功能。

ApplicationContext类体系结构

ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件

 

BeanFactory初始化相似,ApplicationContext的初始化也很简单,如果配置文件放置在类路径下,用户可以优先使用ClassPathXmlApplicationContext实现类:

ApplicationContext ctx = new ClassPathXmlApplicationContext("com/baobaotao/context/beans.xml")

对于ClassPathXmlApplicationContext来说,"com/baobaotao/context/beans.xml"等同于"classpath: com/baobaotao/context/beans.xml"

如果配置文件放置在文件系统的路径下,则可以优先考虑使用FilySystemXmlApplicationContext实现类: 

ApplicationContext ctx =new FileSystemXmlApplicationContext("com/baobaotao/context/beans.xml");  

对于FileSystemXmlApplicationContext来说,“com/baobaotao/context/beans.xml”等同于“file:com/baobaotao/context/beans.xml”

         还可以指定一组配置文件,Spring会自动将多个配置文件在内存中"整合"成一个配置文件,如下所示:

ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"conf/beans1.xml","conf/beans2.xml"});

当然FileSystemXmlApplicationContext和ClassPathXmlApplicationContext都可以显式使用带资源类型(classpath:或file:)前缀的路径,它们的区别在于如果不显式指定资源类型前缀,将分别将路径解析为文件系统路径和类路径罢了。

在获取ApplicationContext实例后,就可以像BeanFactory一样调用getBean(beanName)返回Bean了。ApplicationContext的初始化和BeanFactory有一个重大的区别:BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean时才实例目标Bean;而ApplicationContext则在初始化应用上下文时就实例化所有单实例的Bean。因此ApplicationContext的初始化时间会比BeanFactory稍长一些,不过稍后的调用则没有"第一次惩罚"的问题。

WebApplicationContext类体系结构

WebApplicationContext是专门为Web应用准备的,它允许从相对于Web根目录的路径中装载配置文件完成初始化工作。从WebApplicationContext中可以获得ServletContext的引用,整个Web应用上下文对象将作为属性放置到ServletContext中,以便Web应用环境可以访问Spring应用上下文。Spring专门为此提供一个工具类WebApplicationContextUtils,通过该类的getWebApplicationContext(ServletContext sc)方法,即可以从ServletContext中获取WebApplicationContext实例。

 

由于Web应用比一般的应用拥有更多的特性,因此WebApplicationContext扩展了ApplicationContext。WebApplicationContext定义了一个常量ROOT_WEB_APPLICATION_ CONTEXT_ATTRIBUTE,在上下文启动时,WebApplicationContext实例即以此为键放置在ServletContext的属性列表中,因此我们可以直接通过以下语句从Web容器中获取WebApplicationContext:

WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(

WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

这正是我们前面所提到的WebApplicationContextUtils工具类getWebApplicationContext (ServletContext sc)方法的内部实现方式。这样Spring的Web应用上下文和Web容器的上下文就可以实现互访,二者实现了融合(图3-10):

 

图3-10  Spring和Web应用的上下文融合

 

WebApplicationContext初始化

WebApplicationContext的初始化方式和BeanFactory、ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,也就是说它必须在拥有Web容器的前提下才能完成启动的工作。有过Web开发经验的读者都知道可以在web.xml中配置自启动的Servlet或定义Web容器监听器(ServletContextListener),借助这两者中的任何一个,我们就可以完成启动Spring Web应用上下文的工作。

所有版本的Web容器都可以定义自启动的Servlet,但只有Servlet 2.3及以上版本的Web容器才支持Web容器监听器。有些即使支持Servlet 2.3 的Web服务器,但也不能在Servlet初始化之前启动Web监听器,如Weblogic 8.1、WebSphere 5.x、Oracle OC4J 9.0。

Spring分别提供了用于启动WebApplicationContext的Servlet和Web容器监听器:

org.springframework.web.context.ContextLoaderServlet;

org.springframework.web.context.ContextLoaderListener。

两者的内部都实现了启动WebApplicationContext实例的逻辑,我们只要根据Web容器的具体情况选择两者之一,并在web.xml中完成配置就可以了。

下面是使用ContextLoaderListener启动WebApplicationContext的具体配置:

代码清单3-22  通过Web容器监听器引导

1. …  

2. <!--①指定配置文件--> 

3. <context-param>                                                        

4.      <param-name>contextConfigLocation</param-name>   

5.      <param-value> 

6.        /WEB-INF/baobaotao-dao.xml, /WEB-INF/baobaotao-service.xml  

7.    </param-value> 

8. </context-param> 

9. <!--②声明Web容器监听器--> 

10. <listener>   

11.      <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> 

12. </listener> 

ContextLoaderListener通过Web容器上下文参数contextConfigLocation获取Spring配置文件的位置。用户可以指定多个配置文件,用逗号、空格或冒号分隔均可。对于未带资源类型前缀的配置文件路径,WebApplicationContext默认这些路径相对于Web的部署根路径。当然,我们可以采用带资源类型前缀的路径配置,如"classpath*:/baobaotao-*.xml"和上面的配置是等效的。

如果在不支持容器监听器的低版本Web容器中,我们可采用ContextLoaderServlet完成相同的工作:

代码清单3-23  通过自启动的Servlet引导

1. …  

2. <context-param> 

3.      <param-name>contextConfigLocation</param-name>   

4.      <param-value>/WEB-INF/baobaotao-dao.xml, /WEB-INF/baobaotao-service.xml </param-value> 

5. </context-param> 

6. …  

7. <!--①声明自动启动的Servlet --> 

8. <servlet>   

9.     <servlet-name>springContextLoaderServlet</servlet-name> 

10.     <servlet-class>org.springframework.web.context.ContextLoaderServlet </servlet-class> 

11.     <!--②启动顺序--> 

12.     <load-on-startup>1</load-on-startup> 

13. </servlet> 

由于WebApplicationContext需要使用日志功能,用户可以将Log4J的配置文件放置到类路径WEB-INF/classes下,这时Log4J引擎即可顺利启动。如果Log4J配置文件放置在其他位置,用户还必须在web.xml指定Log4J配置文件位置。Spring为启用Log4J引擎提供了两个类似于启动WebApplicationContext的实现类:Log4jConfigServlet和Log4jConfigListener,不管采用哪种方式都必须保证能够在装载Spring配置文件前先装载Log4J配置信息。

代码清单3-24  指定Log4J配置文件时启动Spring Web应用上下文

1. <context-param> 

2.         <param-name>contextConfigLocation</param-name> 

3.         <param-value> 

4.            /WEB-INF/baobaotao-dao.xml,/WEB-INF/baobaotao-service.xml  

5.         </param-value> 

6.     </context-param> 

7.      <!--①指定Log4J配置文件位置--> 

8.     <context-param>                                             

9.         <param-name>log4jConfigLocation</param-name> 

10.         <param-value>/WEB-INF/log4j.properties</param-value> 

11.     </context-param> 

12.  

13.     <!--②装载Log4J配置文件的自启动Servlet --> 

14.     <servlet> 

15.         <servlet-name>log4jConfigServlet</servlet-name> 

16.         <servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class> 

17.         <load-on-startup>1</load-on-startup> 

18.     </servlet> 

19.     <servlet> 

20.         <servlet-name> springContextLoaderServlet</servlet-name> 

21.         <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 

22.     <load-on-startup>2</load-on-startup> 

23. </servlet> 

注意上面我们将log4jConfigServlet的启动顺序号设置为1,而springContextLoaderServlet的顺序号设置为2。这样,前者将先启动,完成装载Log4J配置文件初始化Log4J引擎的工作,紧接着后者再启动。如果使用Web监听器,则必须将Log4jConfigListener放置在ContextLoaderListener的前面。采用以上的配置方式Spring将自动使用XmlWebApplicationContext启动Spring容器,即通过XML文件为Spring容器提供Bean的配置信息。

Servercontext

ServletConfig与ServletContext对象详解

一、ServletConfig对象
   在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。(配置在某个servlet标签或者整个web-app下)

   当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。

首先,需要创建私有变量:private ServletConfig config = null;

其次,要重写init方法,传入config,令this.config = config;从而获得ServletConfig对象

最后,就可以获得<init-parm>中的配置信息了

//获取初始化参数
  String value1 =this.config.getInitParameter("x1");

//获得配置文档中<init-param>标签下name对应的value
  String vlaue2 =this.config.getInitParameter("x2");
  
  //2.获取所有的初始化参数(用Enumeration接收)
  Enumeration e =this.config.getInitParameterNames();
  while(e.hasMoreElements()){
   String name =(String) e.nextElement();
   String value= this.config.getInitParameter(name);
   System.out.println(name+ "=" + value);
  }

   在开发中ServletConfig的作用有如下三个:

1)获得字符集编码

  String charset =this.config.getInitParameter("charset");
2)获得数据库连接信息

  String url =this.config.getInitParameter("url");
  String username =this.config.getInitParameter("username");
  String password =this.config.getInitParameter("password");
3)获得配置文件

  String configFile =this.config.getInitParameter("config");


二、ServletContext对象

 WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。

 1)ServletContext对象应用1:多个web组件之间使用它实现数据共享

 ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。

serlvet中,可以使用如下语句来设置数据共享

  ServletContext context =this.getServletContext(); //servletContext域对象
  context.setAttribute("data","共享数据"); //向域中存了一个data属性

在另一个servlet中,可以使用如下语句来获取域中的data属性

  ServletContext context =this.getServletContext();
  String value = (String)context.getAttribute("data");  //获取域中的data属性
  System.out.println(value);

  2)通过servletContext对象获取到整个web应用的配置信息

  String url =this.getServletContext().getInitParameter("url");

  String username =this.getServletContext().getInitParameter("username");
  String password =this.getServletContext().getInitParameter("password");

  3)通过servletContext对象实现servlet转发

由于servlet中的java数据不易设置样式,所以serlvet可以将java数据转发到JSP页面中进行处理

 this.getServletContext().setAttribute("data","serlvet数据转发");
  RequestDispatcher rd =this.getServletContext().getRequestDispatcher("/viewdata.jsp");
  rd.forward(request,response);

 4)通过servletContext对象读取资源文件

 在实际开发中,用作资源文件的文件类型,通常是:xml、properties,而读取xml文件必然要进行xml文档的解析,所以以下例子只对properties文件进行读取(在一个web工程中,只要涉及到写地址,建议最好以/开头)

 在web工程中,我们一般来说,是不能采用传统方式读取配置文件的,因为相对的是jvm的启动目录(tomcat的bin目录),所以我们要使用web绝对目录来获取配置文件的地址

  读取资源文件的三种方式:

 第一种:使用ServletContext的getResourceAsStream方法:返回资源文件的读取字节流

  InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
  Properties prop = newProperties();  

  prop.load(in);
  String url =prop.getProperty("url");

 第二种:使用ServletContext的getRealPath方法,获得文件的完整绝对路径path,再使用字节流读取path下的文件

  String path =this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
  String filename =path.substring(path.lastIndexOf("\\")+1); 

  //相比第一种方法的好处是:除了可以获取数据,还可以获取资源文件的名称
  FileInputStream in = newFileInputStream(path);
  Properties prop = newProperties();
  prop.load(in);
  String url =prop.getProperty("url");

 第三种:使用ServletContext的getResource方法,获得一个url对象,调用该类的openStream方法返回一个字节流,读取数据

  URL url =this.getServletContext().getResource("/WEB-INF/classes/db.properties");
  InputStream in =url.openStream();
  Properties prop = newProperties();
  prop.load(in);
  String url1 =prop.getProperty("url");

  5)web工程中,不同位置的资源文件的读取方式

  一、当资源文件在包下面时
  InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
  System.out.println(in);
  
  二、资源文件在web-inf下
  in =this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
  System.out.println(in);
  
  三、资源文件在web工程中
  in =this.getServletContext().getResourceAsStream("/db.properties");
  System.out.println(in);

  6)在非servlet程序中如何读取配置文件:用类装载器

1)用类装载方式读取 

 in =StudentDao.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties");
2)用类装载方式读取,把资源当作url对待

 URL url =StudentDao.class.getClassLoader().getResource("db.properties");

 这样可以获得资源文件名称:String path = url.getPath();

3)注意:在线程休眠过程中,即使改动了资源文件,获取到的还是原始内容

解决方案:

  URL url =StudentDao.class.getClassLoader().getResource("db.properties");
  String path =url.getPath();
  
  FileInputStream in = newFileInputStream(path);
  Properties prop = newProperties();
  prop.load(in);
  System.out.println(prop.getProperty("url"));
  
  try {
   Thread.sleep(1000*15);
  } catch (InterruptedExceptione) {
    e.printStackTrace();
  }
  in = newFileInputStream(path);
  prop = new Properties();
  prop.load(in);
  System.out.println(prop.getProperty("url"));

webAppRootKey作用

log4j是很好用的一个工具,在目前的WEB项目中经常使用。配上jakarta的common logging和Eclipse的插件Log4E很好用。 

不过还是有几个不太方便的地方: 

1 Log4j的配置文件修改了之后必须重启才能生效 

2 配置文件只能放在WEB-INF/classes 

Spring对于Log4j有了更好的增强,配置文件修改后不需要重启,不用再放到WEB-INF/classes目录下。 

参数webAppRootKey如果只有一个应用用了Spring对Log4J的增强,则可以不用设置;否则一定要进行设置。

 

web.xml中webAppRootKey

------------------------------------------------------------------------------------------------
1、 web.xml配置 
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
"webapp.root"这个字符串可以随便写任何字符串。如果不配置默认值是"webapp.root"。
 
可以用System.getProperty("webapp.root")来动态获项目的运行路径。
一般返回结果例如:/usr/local/tomcat6/webapps/项目名

定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。 
然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。 
此参数用于后面的“Log4jConfigListener”-->



2、解决以下报错

部署在同一容器中的Web项目,要配置不同的<param-value>,不能重复,否则报类似下面的错误:
Web app root system property already set to different value: 'webapp.root' = [/home/user/tomcat/webapps/project1/] instead of [/home/user/tomcat/webapps/project2/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!  
意思是“webapp.root”这个key已经指向了项目1,不可以再指向项目2.

3、加载方式

Spring通过org.springframework.web.util.WebAppRootListener 这个监听器来运行时的项目路径。
但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener这个监听器,
则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能
一般配置类型下面的例子:

Xml代码  

1. <!-- 加载Log4J 配置文件  -->  

2. <context-param>  

3.     <param-name>log4jConfigLocation</param-name>  

4.     <param-value>WEB-INF/conf/log4j.properties</param-value>  

5. </context-param>     

6.   

7. <context-param>  

8.     <param-name>log4jRefreshInterval</param-name>  

9.       <param-value>3000</param-value>  

10.  </context-param>  

11.   

12. <listener>  

13.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  

14.  </listener>  

 

4、在运行时动态的找出项目的路径

log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:
 log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log 
就可以在运行时动态的找出项目的路径

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值