GVKun编程网logo

将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml(将spring配置应用程序的方式有哪些)

9

对于将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解将spring配置应用程

对于将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解将spring配置应用程序的方式有哪些,并且为您提供关于APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML、applicationContext.xml、dispathcer-servlet.xml加载、applicationContext.xml与spring-severlet.xml加载关系、applicationContext.xml和dispatcher-servlet.xml的区别的宝贵知识。

本文目录一览:

将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml(将spring配置应用程序的方式有哪些)

将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml(将spring配置应用程序的方式有哪些)

我正在Eclipse中开发Web应用程序。Spring MVC Web应用程序将得到完善。我已经创建了servlet xml并将其映射到web.xml中。
一切正常,直到这里&Servlet加载HTML页面, 但是现在我想添加一个JPA层。因此,我在WEB-
INF中创建了applicationContext.xml,并将所有与JPA相关的spring配置放入该文件中。但是,当我启动Web应用程序时,未加载与JPA相关的内容。我相信的是,applicationContext.xml本身未加载。我在这里缺少任何想法或任何其他配置吗?

答案1

小编典典

applicationContext.xml除非你配置Spring文件默认不加载ContextLoaderListenerweb.xml或其他类似的配置文件。

我假设文件夹中applicationContext.xml存在WEB-INF

  <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>        /WEB-INF/applicationContext.xml    </param-value>    </context-param>

如果您有多个配置文件,则可以将它们指定为,单独的值,例如

<param-value> classpath:applicationContext.xml, classpath:securityContext.xml</param-value>

APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML

APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML

之前使用的 ssm 框架时,配置的是 spring-mybatis.xml 这个文件,今天在网上搜 ssm 框架整合时,发现了 applicationContext.xml 这个文件(对于 Spring 和 mybatis 两个框架的整合),然后又查了下 spring-mybatis.xml 的配置,发现 applicationContext.xml 与 spring-mybatis.xml 的配置内容都差不多,所以,我觉得这两个文件虽然名称不同,但是要做的内容都是相同的,整合 Spring 与 MyBatis 这两个框架

Spring

Spring 是一个开源框架,Spring 是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而,Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。简单来说,Spring 是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架

MyBatis

MyBatis 本是 apache 的一个开源项目 iBatis, 2010 年这个项目由 apache software foundation 迁移到了 google code,并且改名为 MyBatis 。MyBatis 是一个基于 Java 的持久层框架。iBATIS 提供的持久层框架包括 SQL Maps 和 Data Access Objects(DAO)MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录

文件配置(Spring 与 MyBatis 整合)

以下大部分内容转自:https://www.cnblogs.com/zipon/p/5773735.html

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
</beans>

applicationContext.xml 配置内容((在 web.xml 文件需要加 < listener>)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">
 
    <<span style="color:#ff0000;">!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 --></span>
    <context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>
 
    <!-- 引入jdbc配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:jdbc.properties</value>
            </list>
        </property>
    </bean>
 
    <!-- dataSource 配置 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
 
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />
 
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />
 
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
 
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
 
        <property name="validationQuery" value="SELECT ''x''" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
 
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
 
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat" />
    </bean>
 
    <!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" />
 
    <span style="color:#ff0000;"><!-- spring与mybatis整合配置,扫描所有dao --></span>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
 
    <!-- 对dataSource 数据源进行事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
 
    <span style="color:#ff0000;"><!-- 配置使Spring采用CGLIB代理 --></span>
    <aop:aspectj-autoproxy proxy-target-class="true" />
 
   <span style="color:#ff0000;"> <!-- 启用对事务注解的支持 --></span>
    <tx:annotation-driven transaction-manager="transactionManager" />
 
<span style="color:#ff0000;">    <!-- Cache配置 --></span>
    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
 
</beans>

 

对于 pojo(entity)层之前我都是单独配置一个 mybatis-config.xml,然后进行引入,而以下的配置是直接将 pojo 配在了 Session 的配置里,省去了一个配置,值得学习:此段代码参考链接:https://blog.csdn.net/u010067848/article/details/78985444

<!--Mybatis的SessionFactory配置 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.azimiao.tmall.pojo"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>

1、<context:component-scan base-package=”com.eduoinfo.finances.bank.web”></context:component-scan> 作用 Spring 容器初始化的时候,会扫描 com.eduoinfo.finances.bank.web 下 标有 (@Component,@Service,@Controller,@Repository) 注解的 类 纳入 spring 容器管理

在类上 ,使用以下注解,实现 bean 的声明
@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service 用于标注业务层组件
@Controller 用于标注控制层组件(如 srping mvc 的 controller,struts 中的 action)
@Repository 用于标注数据访问组件,即 DAO 组件

示例:
@Controller
@RequestMapping(value = “/test”)
public class TestController {}

在类的成员变量上,使用以下注解,实现属性的自动装配
@Autowired : 按类 的 类型进行装配
@Resource (推荐) : 1 如果同时指定了 name 和 type,则从 Spring 上下文中找到唯一匹配的 bean 进行装配,找不到则抛出异常

  2. 如果指定了 name,则从上下文中查找名称(id)匹配的 bean 进行装配,找不到则抛出异常
3. 如果指定了 type,则从上下文中找到类型匹配的唯一 bean 进行装配,找不到或者找到多个,都会抛出异常 
4. 如果既没有指定 name,又没有指定 type,则自动按照 byName 方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
@Resource 注解在字段上,这样就不用写 setter 方法了,并且这个注解是属于 J2EE 的,减少了与 spring 的耦合。

示例:
@Resource
private TestServiceImpl testServiceImpl;

 

spring-mvc.xml 和 application-context.xml 的配置与深入理解链接:https://blog.csdn.net/qq_35571554/article/details/82453684

 

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与spring-severlet.xml加载关系

applicationContext.xml与spring-severlet.xml加载关系


读取applicationContext.xml(spring配置文件)文件:(通过编码加载) 方法一: ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml"); 方法二: ApplicationContext ctx=new ClasspathXmlApplicationContext("applicationContext.xml"); 其他: //得到beanfactory InputStream is = new FileInputStream("applicationContext.xml"); Xmlbeanfactory factory = new Xmlbeanfactory(is); //通过beanfactory获取Bean实例: BeanName bean=(BeanName)factory.getBean("beanName"); 对于WEB应用,spring提供了可配置的ApplicationContext加载机制: 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet。这两者在功能上完全 等同,只是一个是基于Servlet2.3版本中新引入的Listener接口实现,而另一个基于Servlet接口实现。 配置非常简单,在web.xml中增加: <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 或 <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> 通过以上配置,Web容器会自动加载/WEB-INF/applicationContext.xml初始化 ApplicationContext实例,如果需要指定配置文件位置,可通过context-param加以指定: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/myApplicationContext.xml</param-value> </context-param> 配置完成之后,即可通过 WebApplicationContextUtils.getWebApplicationContext 方法在Web应用中获取ApplicationContext引用。 转至:http://www.360doc.com/content/14/0126/16/14352979_348129730.shtml

applicationContext.xml和dispatcher-servlet.xml的区别

applicationContext.xml和dispatcher-servlet.xml的区别

applicationContext.xml和dispatcher-servlet.xml的区别

在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢?

  Spring 官方文档介绍如下:

    Spring lets you define multiple contexts in a parent-child hierarchy.  
    The applicationContext.xml defines the beans for the "root webapp context",i.e. the context associated with the webapp.  
    The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp,one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1,spring2-servlet.xml for servlet spring2).  
    Beans in spring-servlet.xml can reference beans in applicationContext.xml,but not vice versa.  
    All Spring MVC controllers must go in the spring-servlet.xml context.  
    In most simple cases,the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet,then there's not really much point,unless you have a specific use for it.   

  可见,applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文

dispatcherServlet在web.xml中的配置

<servlet>
        <servlet-name>chapter2</servlet-name>
        <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>chapter2</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 

load-on-startup:表示启动容器时初始化该Servlet;

url-pattern:表示哪些请求交给Spring Web MVC处理,“/”是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。

该dispatcherServlet默认使用WebApplicationContext作为上下文,Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”。

dispatcherServlet也可以配置自己的初始化参数,覆盖默认配置:

摘自Spring Reference

参数

描述

contextClass

实现WebApplicationContext接口的类,当前的servlet用它来创建上下文。如果这个参数没有指定, 默认使用XmlWebApplicationContext。

contextConfigLocation

传给上下文实例(由contextClass指定)的字符串,用来指定上下文的位置。这个字符串可以被分成多个字符串(使用逗号作为分隔符) 来支持多个上下文(在多上下文的情况下,如果同一个bean被定义两次,后面一个优先)。

namespace

WebApplicationContext命名空间。默认值是[server-name]-servlet。



  1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。

  2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。

  不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。

  ----------------------------------------------------------------------------------------------------------------------------------------

  ApplicationContext.xml是spring 全局配置文件,用来控制spring 特性的

  dispatcher-servlet.xml是spring mvc里面的,控制器、拦截uri转发view

  使用applicationContext.xml文件时是需要在web.xml中添加listener的:

  <listener>
      listener-class>org.springframework.web.context.ContextLoaderListener</>
  >
分类: SpringMVC

关于将spring-servlet.xml保留在Web应用程序中后,未加载applicationContext.xml将spring配置应用程序的方式有哪些的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML、applicationContext.xml、dispathcer-servlet.xml加载、applicationContext.xml与spring-severlet.xml加载关系、applicationContext.xml和dispatcher-servlet.xml的区别等相关内容,可以在本站寻找。

本文标签: