GVKun编程网logo

无法打开ServletContext资源[/WEB-INF/applicationContext.xml](servlet无法访问资源)

15

此处将为大家介绍关于无法打开ServletContext资源[/WEB-INF/applicationContext.xml]的详细内容,并且为您解答有关servlet无法访问资源的相关问题,此外,我

此处将为大家介绍关于无法打开ServletContext资源[/WEB-INF/applicationContext.xml]的详细内容,并且为您解答有关servlet无法访问资源的相关问题,此外,我们还将为您介绍关于AnnotationConfigServletWebServerApplicationContext解析、applicationContext-webService.xml 配置、applicationContext.xml、dispathcer-servlet.xml加载、applicationContext.xml中的区别的有用信息。

本文目录一览:

无法打开ServletContext资源[/WEB-INF/applicationContext.xml](servlet无法访问资源)

无法打开ServletContext资源[/WEB-INF/applicationContext.xml](servlet无法访问资源)

好的,我是第500位用户问这个问题,我读了很多答案,但仍然没有运气。

父模块pom包含:

<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-web</artifactId>    <version>${spring.framework.version}</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-webmvc</artifactId>    <version>${spring.framework.version}</version></dependency>

子模块有maven-jetty-plugin,我使用运行我的webapp模块jetty:run

web.xml 定义标准的调度程序模块:

<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet>    <servlet-name>dispatcher</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>    <servlet-name>dispatcher</servlet-name>    <url-pattern>/</url-pattern></servlet-mapping>

dispatcher-servlet.xml在下有文件WEB-INF,但是启动失败:

FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

怎么了?文档和所有人都说Spring MVC将搜索XX-servlet.xml,其中XX是servlet的名称。为什么要搜索applicationContext.xml

答案1

小编典典

ContextLoaderListener有自己的上下文,所有servlet和过滤器都共享它。默认情况下它将搜索/WEB-INF/applicationContext.xml

你可以使用

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/somewhere-else/root-context.xml</param-value></context-param>

上的web.xml,或在不需要时删除此侦听器。

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

AnnotationConfigServletWebServerApplicationContext解析

springboot中启动的applicationContext就是这个类。类图:

getBean方法

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		assertBeanFactoryActive();
		return getBeanFactory().getBean(requiredType);
	}

	public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
	}

	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}

getBean方法的具体实现主要是通过beanFactory来实现的。

DefaultListableBeanFactory

类图

getBean具体的ioc流程后续分析

	public <T> T getBean(Class<T> requiredType) throws BeansException {
		return getBean(requiredType, (Object[]) null);
	}

applicationContext-webService.xml 配置

applicationContext-webService.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">
		
	<jaxrs:server id="promotionService" address="/promotionService">
		<jaxrs:serviceBeans>
			<bean/>
		</jaxrs:serviceBeans>
		<jaxrs:inInterceptors>
			<bean/>
		</jaxrs:inInterceptors>
		<jaxrs:outInterceptors>
			<bean/>
		</jaxrs:outInterceptors>
	</jaxrs:server>

	<jaxrs:server id="orderService" address="/orderService">
		<jaxrs:serviceBeans>
			<bean/>
		</jaxrs:serviceBeans>
		<jaxrs:inInterceptors>
			<bean/>
		</jaxrs:inInterceptors>
		<jaxrs:outInterceptors>
			<bean/>
		</jaxrs:outInterceptors>
	</jaxrs:server>
		
</beans>

 

applicationContext.xml、dispathcer-servlet.xml加载

applicationContext.xml、dispathcer-servlet.xml加载

1、applicationContext.xml和dispatcher-servlet.xml

applicationContext.xml是随ContextLoaderListener的加载而执行的,

<!-- Spring -->
<!-- 配置Spring配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>
<!-- 配置Spring上下文监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring -->

而xxx-servlet.xml是随DispatcherServlet的加载而执行的,

<!-- Spring MVC 核心控制器 DispatcherServlet 配置 -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!-- 拦截所有/rest/* 的请求,交给DispatcherServlet处理,性能最好 -->
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

在web.xml中,加载顺序是listener>filter>servlet,所以applicationContext.xml先加载!

2、分成spring.xml(applicationContext.xml)、spring-mvc.xml(dispatcher-servlet.xml)、spring-mybatis.xml

首先ContextLoaderListener加载核心的spring.xml,以及数据源和事务管理等配置文件spring-batis.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>


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

然后DispatcherServlet加载spring-mvc.xml

<servlet>
    <description>spring-mvc</description>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-mvc.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>



applicationContext.xml中<context:annotation-config> 和 <context:component-scan>的区别

applicationContext.xml中的区别

Difference between <context:annotation-config> vs <context:component-scan>

<context:annotation-config>是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean。

下面我们通过例子来详细查看他们的区别,

有三个class A,B,C,并且B,C的对象被注入到A中.

package com.xxx;
public class B {
  public B() {
    System.out.println("creating bean B: " + this);
  }
}

package com.xxx;
public class C {
  public C() {
    System.out.println("creating bean C: " + this);
  }
}

package com.yyy;
import com.xxx.B;
import com.xxx.C;
public class A { 
  private B bbb;
  private C ccc;
  public A() {
    System.out.println("creating bean A: " + this);
  }
  public void setBbb(B bbb) {
    System.out.println("setting A.bbb with " + bbb);
    this.bbb = bbb;
  }
  public void setCcc(C ccc) {
    System.out.println("setting A.ccc with " + ccc);
    this.ccc = ccc; 
  }
}

在applicationContext.xml中加入下面的配置 :

<bean id="bBean"class="com.xxx.B"/><bean id="cBean"class="com.xxx.C"/><bean id="aBean"class="com.yyy.A"><property name="bbb" ref="bBean"/><property name="ccc" ref="cBean"/></bean>

加载applicationContext.xml配置文件,将得到下面的结果:

creating bean B: com.xxx.B@c2ff5 creating bean C: com.xxx.C@1e8a1f6 creating bean A: com.yyy.A@1e152c5 setting A.bbb with com.xxx.B@c2ff5 setting A.ccc with com.xxx.C@1e8a1f6

OK,这个结果没什么好说的,就是完全通过xml的方式,不过太过时了,下面通过注解的方式来简化我们的xml配置文件

首先,我们使用autowire的方式将对象bbb和ccc注入到A中:

package com.yyy;
import org.springframework.beans.factory.annotation.Autowired;
import com.xxx.B;
import com.xxx.C;
public class A { 
  private B bbb;
  private C ccc;
  public A() {
    System.out.println("creating bean A: " + this);
  }
  @Autowired
  public void setBbb(B bbb) {
    System.out.println("setting A.bbb with " + bbb);
    this.bbb = bbb;
  }
  @Autowired
  public void setCcc(C ccc) {
    System.out.println("setting A.ccc with " + ccc);
    this.ccc = ccc;
  }
}

然后,我们就可以从applicationContext.xml中移除下面的配置

<property name="bbb" ref="bBean"/><property name="ccc" ref="cBean"/>

移除之后,我们的applicationContext.xml配置文件就简化为下面的样子了

<bean id="bBean"class="com.xxx.B"/><bean id="cBean"class="com.xxx.C"/><bean id="aBean"class="com.yyy.A"/>

当我们加载applicationContext.xml配置文件之后,将得到下面的结果:

creating bean B: com.xxx.B@5e5a50 creating bean C: com.xxx.C@54a328 creating bean A: com.yyy.A@a3d4cf
ottom: 1em; border: 0px; vertical-align: baseline; clear: both; font-family: Arial,结果是错误的的,究竟是因为什么呢?为什么我们的属性没有被注入进去呢?

是因为注解本身并不能够做任何事情,它们只是最基本的组成部分,我们需要能够处理这些注解的处理工具来处理这些注解

这就是<context:annotation-config>所做的事情

我们将applicationContext.xml配置文件作如下修改:

<context:annotation-config /><bean id="bBean"class="com.xxx.B"/><bean id="cBean"class="com.xxx.C"/><bean id="aBean"class="com.yyy.A"/>

creating bean B: com.xxx.B@15663a2 creating bean C: com.xxx.C@cd5f8b creating bean A: com.yyy.A@157aa53 setting A.bbb with com.xxx.B@15663a2 setting A.ccc with com.xxx.C@cd5f8b
ottom: 1em; border: 0px; vertical-align: baseline; clear: both; font-family: Arial,结果正确了

但是如果我们将代码作如下修改:

package com.xxx;
import org.springframework.stereotype.Component;
@Component
public class B {
  public B() {
    System.out.println("creating bean B: " + this);
  }
}

package com.xxx;
import org.springframework.stereotype.Component;
@Component
public class C {
  public C() {
    System.out.println("creating bean C: " + this);
  }
}

package com.yyy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xxx.B;
import com.xxx.C;
@Component
public class A { 
  private B bbb;
  private C ccc;
  public A() {
    System.out.println("creating bean A: " + this);
  }
  @Autowired
  public void setBbb(B bbb) {
    System.out.println("setting A.bbb with " + bbb);
    this.bbb = bbb;
  }
  @Autowired
  public void setCcc(C ccc) {
    System.out.println("setting A.ccc with " + ccc);
    this.ccc = ccc;
  }
}

applicationContext.xml配置文件修改为:

<context:annotation-config />

当我们加载applicationContext.xml配置文件之后,却没有任何输出,这是为什么呢?

那是因为<context:annotation-config />仅能够在已经在已经注册过的bean上面起作用。对于没有在spring容器中注册的bean,它并不能执行任何操作。

但是不用担心,<context:component-scan>除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。

<context:component-scan base-package="com.xxx"/>

当我们加载applicationContext.xml的时候,会得到下面的结果:

creating bean B: com.xxx.B@1be0f0a creating bean C: com.xxx.C@80d1ff

这是什么原因呢?

是因为我们仅仅扫描了com.xxx包及其子包的类,而class A是在com.yyy包下,所以就扫描不到了

下面我们在applicationContext.xml中把com.yyy也加入进来:

<context:component-scan basepackage="com.xxx,com.yyy"/><context:component-scan base-package="com.xxx"/> 
然后加载applicationContext.xml就会得到下面的结果:
creating bean B: com.xxx.B@cd5f8b creating bean C: com.xxx.C@15ac3c9 creating bean A: com.yyy.A@ec4a87 setting A.bbb with com.xxx.B@cd5f8b setting A.ccc with com.xxx.C@15ac3c9

哇,结果正确啦 !

回头看下我们的applicationContext.xml文件,已经简化为:

/><context:component-scan base-package="com.xxx"/>

了。

那如果我们在applicationContext.xml手动加上下面的配置,也就是说既在applicationContext.xml中手动的注册了A的实例对象,同时,通过component-scan去扫描并注册B,C的对象

<context:component-scan base-package="com.xxx"/><bean id="aBean"class="com.yyy.A"/>

结果仍是正确的:

creating bean B: com.xxx.B@157aa53 creating bean C: com.xxx.C@ec4a87 creating bean A: com.yyy.A@1d64c37 setting A.bbb with com.xxx.B@157aa53 setting A.ccc with com.xxx.C@ec4a87

虽然class A并不是通过扫描的方式注册到容器中的 ,但是<context:component-scan>所产生的的处理那些注解的处理器工具,会处理所有绑定到容器上面的bean,不管是通过xml手动注册的还是通过scanning扫描注册的。

那么,如果我们通过下面的方式呢?我们既配置了<context:annotation-config />,又配置了<context:component-scan base-package="com.xxx" />,它们都具有处理在容器中注册的bean里面的注解的功能。会不会出现重复注入的情况呢?

<context:annotation-config /><context:component-scan base-package="com.xxx"/><bean id="aBean"class="com.yyy.A"/>

不用担心,不会出现的:

因为<context:annotation-config />和<context:component-scan>同时存在的时候,前者会被忽略。也就是那些@autowire,@resource等注入注解只会被注入一次

哪怕是你手动的注册了多个处理器,spring仍然只会处理一次:

<context:annotation-config />
<context:component-scan base-package="com.xxx" />
<bean id="aBean"/>
<bean id="bla"/>
<bean id="bla1"/>
<bean id="bla2"/>
<bean id="bla3"/>

结果仍是正确的:

creating bean B: com.xxx.B@157aa53 creating bean C: com.xxx.C@ec4a87 creating bean A: com.yyy.A@25d2b2 setting A.bbb with com.xxx.B@157aa53 setting A.ccc with com.xxx.C@ec4a87

我们今天的关于无法打开ServletContext资源[/WEB-INF/applicationContext.xml]servlet无法访问资源的分享就到这里,谢谢您的阅读,如果想了解更多关于AnnotationConfigServletWebServerApplicationContext解析、applicationContext-webService.xml 配置、applicationContext.xml、dispathcer-servlet.xml加载、applicationContext.xml中的区别的相关信息,可以在本站进行搜索。

本文标签: