Spring初始化时创建容器和applicationContext.xml 配置文件的存放位置(四)

17 篇文章 0 订阅

在web项目中,每次请求都会创建一个Spring的工厂(容器),这样浪费服务器资源,所以一个项目只应该有一个工厂(容器)。

解决方法是:

  • 在服务器启动的时候,创建一个工厂

  • 创建完工厂,把这个工厂类保存到ServletContext中

  • 每次使用的时候,就从ServletContext中获取。

使用Spring的核心监听器ContextLodaerListener

需要引入spring jar包:

spring-web-4.2.4.RELEASE

1.在web.xml里用< context-param>配置需要加载的spring配置文件。如果要装入多个配置文件,在< param-value>标记中用逗号作分隔符即可。

2.在web.xml里配置Listener,Listener会去加载< context-param>指定的路径下的配置文件。如果没有指定路径,默认加载的是 /WEB-INF/applicationContext.xml 路径下的文件

web.xml配置:

<!--配置Spring的核心监听器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

最后在Action中获取工厂类的使用:

//    编写保存客户的方法
    public String save(){
        //创建一个工厂类
        //服务器启动的时候使用ContextLoaderListener创建工厂方式
        ServletContext sc = ServletActionContext.getServletContext();
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);
        
        //自己每次请求新建工厂方式
		//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
        customerService.save(customer);
        return NONE;
    }

通过ContextLoaderListener创建工厂方式:

ServletContext sc = ServletActionContext.getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sc);

每次请求创建工厂方式:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);

关于applicationContext.xml 配置文件的存放位置

web.xml中classpath:和classpath*: 有什么区别?

classpath:只会到你的class路径中查找找文件;

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

存放位置:

  • 1:src下面 需要在web.xml中定义如下:

      <context-param>
      
      <param-name>contextConfigLocation</param-name>
      
      <param-value>classpath:applicationContext.xml</param-value>
      
      < /context-param>
    
  • 2:WEB-INF下面 需要在web.xml中定义如下:

      <context-param>
      
      <param-name>contextConfigLocation</param-name>
      
      <param-value>WEB-INF/applicationContext*.xml</param-value>
      
      < /context-param>
    

web.xml 通过contextConfigLocation配置spring 的方式SSI框架配置文件路径问题:

struts2的 1个+N个 路径:src+src(可配置) 名称: struts.xml + N spring 的 1个 路径:

src 名称: applicationContext.xml ibatis 的 1个+N个 路径:

src+src(可配置) 名称: SqlMapConfig.xml + N

部署到应用服务器(tomcat)后,src目录下的配置文件会和class文件一样,自动copy到应用的 classes目录下

spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,

运行时使用的是web-info/classes目录下的applicationContext.xml。

配置web.xml使这2个路径一致:

<context-param> 

<param-name>contextConfigLocation</param-name> 

<param-value>/WEB-INF/classes/applicationContext.xml</param-value> 

< /context-param> 

多个配置文件的加载 :

<context-param> 

<param-name>contextConfigLocation</param-name> 

<param-value> 

classpath*:conf/spring/applicationContext_core*.xml, 

classpath*:conf/spring/applicationContext_dict*.xml,

classpath*:conf/spring/applicationContext_hibernate.xml

</param-value>
</context-param>

contextConfigLocation 参数定义了要装入的 Spring 配置文件。

首先与Spring相关的配置文件必须要以"applicationContext-"开头,

要符合约定优于配置的思想,这样在效率上和出错率上都要好很多。

还有最好把所有Spring配置文件都放在一个统一的目录下,如果项目大了还可以在该目录下分模块建目录。

这样程序看起来不会很乱。

在web.xml中的配置如下:

Xml代码

<context-param> 

< param-name>contextConfigLocation</param-name> 

< param-value>classpath*:**/applicationContext-*.xml</param-value>

< /context-param> 
  • "**/"表示的是任意目录;

    "**/applicationContext-*.xml"表示任意目录下的以"applicationContext-"开头的XML文件。

可以根据需要修改。最好把所有Spring配置文件都放在一个统一的目录下,如:

<context-param> 

<param-name>contextConfigLocation</param-name> 

<param-value>classpath:/spring/applicationContext-*.xml</param-value>

< /context-param> 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值