GVKun编程网logo

在Spring应用程序中处理* -context.xml和applicationContext.xml文件(spring application.yml)

11

关于在Spring应用程序中处理*-context.xml和applicationContext.xml文件和springapplication.yml的问题就给大家分享到这里,感谢你花时间阅读本站内

关于在Spring应用程序中处理* -context.xml和applicationContext.xml文件spring application.yml的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML、applicationContext.xml中的区别、applicationContext.xml和applicationContext-mvc.xml、applicationContext.xml和SpringMVC的区别等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

在Spring应用程序中处理* -context.xml和applicationContext.xml文件(spring application.yml)

在Spring应用程序中处理* -context.xml和applicationContext.xml文件(spring application.yml)

我正在使用Spring Web MVC作为事务hibernate的前端(所有注释驱动)。我将web.xml设置如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>wdman</display-name><session-config>    <session-timeout>30</session-timeout></session-config><context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet>    <servlet-name>wdman</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>    <servlet-name>wdman</servlet-name>    <url-pattern>/*</url-pattern></servlet-mapping><!-- Disables Servlet Container welcome file handling.     Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --><welcome-file-list>    <welcome-file></welcome-file></welcome-file-list></web-app>

我不明白<tx:annotation-driven/>从根上下文(在本例中是一个已定义的applicationContext.xml)到*
-servlet.xml上下文的选项是如何继承的。它似乎不起作用。component-scan为了使应用程序正常工作,我同时拥有这两个文件。这可以吗?组件不重复吗?

您能帮我指出一些简洁的文档,描述这些上下文如何合并吗?

答案1

小编典典

我不明白选项是如何<tx:annotation-driven/>从根上下文(在这种情况下为定义的applicationContext.xml)继承到*-servlet.xml上下文的

他们不是。这些设置是上下文本地的。

component-scan为了使应用程序正常工作,我同时拥有这两个文件。这可以吗?组件不重复吗?

如果component-scan在每个上下文中都有相同的配置,则可以,这些组件将被复制。由您决定指定扫描仅实例化每个上下文所需的组件。

然而,在规定的豆子applicationContext.xml
孩子的上下文可见的,所以你应该能够让您的组件扫描父语境,离开它的子上下文。根据经验,只有特定于MVC的东西才需要在servlet上下文中声明,并且那些bean可以引用父对象中定义的bean。

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中<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

applicationContext.xml和applicationContext-mvc.xml

applicationContext.xml和applicationContext-mvc.xml

1.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- 扫描service部分的包 --> <!-- 配置连接池 --> <!-- 集成hibernate的jpa功能-工厂bean引入EntityManager--> <!-- Jpa 事务配置 --> <!-- 注解声明式事务管理 --> <!-- 注解声明式事务管理 --> <!-- Spring Data Jpa配置 --> <!-- 扫描service部分的包 --> <context:component-scan base-package="com.域名.模块名service" /> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!--连接数据4个属性 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!--maxActive: 最大连接数量 --> <property name="maxActive" value="150" /> <!--minIdle: 最小空闲连接 --> <property name="minIdle" value="5" /> <!--maxIdle: 最大空闲连接 --> <property name="maxIdle" value="20" /> <!--initialSize: 初始化连接 --> <property name="initialSize" value="30" /> <!-- 用来配置数据库断开后自动连接的 --> <!-- 连接被泄露时是否打印 --> <property name="logAbandoned" value="true" /> <!--removeAbandoned: 是否自动回收超时连接 --> <property name="removeAbandoned" value="true" /> <!--removeAbandonedTimeout: 超时时间(以秒数为单位) --> <property name="removeAbandonedTimeout" value="10" /> <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 --> <property name="maxWait" value="1000" /> <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. --> <property name="timeBetweenEvictionRunsMillis" value="10000" /> <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 --> <property name="numTestsPerEvictionRun" value="10" /> <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 --> <property name="minEvictableIdleTimeMillis" value="10000" /> <property name="validationQuery" value="SELECT NOW() FROM DUAL" /> </bean> <!-- 集成hibernate的jpa功能 --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--待扫描的实体类包,不再需要persistence.xml了 --> <property name="packagesToScan" value="com.域名.模块名.domain" /> <!-- 3.配置JPA的实现 --> <!-- private JpaVendorAdapter jpaVendorAdapter; --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <!-- org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter --> <!-- private boolean showSql = false;是否显示sql语句 --> <property name="showSql" value="true" /> <!-- private boolean generateDdl = false;是否建表 --> <property name="generateDdl" value="false" /> <!-- private String databasePlatform;原来方言 --> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> </bean> </property> </bean> <!-- Jpa 事务配置 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 注解声明式事务管理 --> <tx:annotation-driven /> <!-- Spring Data Jpa配置 ********************************************--> <!-- base-package:扫描的包 --> <jpa:repositories base-package="com.域名.模块名.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory" factory-class="com.域名.模块名.repository.BaseRepositoryFactoryBean" /> <!-- factory-这个是自定义增强后的 BaseRepositoryFactoryBean --> </beans>

 

 

2. applicationContext-mvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
    <!--!!!!5件套!!!!!-->
    <!-- 1.对静态资源进行放行 -->
    <!-- 2.扫描controller部分的包 -->
    <!-- 3.添加mvc对@RequestMapping等注解的支持 -->
    <!-- 4.ViewResolver 视图解析器 (struts2视图类型类似) -->
    <!-- 5.配置文件上传解析器。 -->


    <!-- 1.对静态资源进行放行 -->
    <mvc:default-servlet-handler />
    <!-- 2.扫描controller部分的包 -->
    <!-- @Component组件, @Repository持久层, @Service业务逻辑层, and @Controller控制器 -->
    <context:component-scan base-package="com.域名.模块名.web.controller" />
    <!-- 3.添加mvc对@RequestMapping等注解的支持 -->
    <mvc:annotation-driven />

    <!-- 4.ViewResolver 视图解析器 (struts2视图类型类似) -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置视图路径的前后缀,该配置可以让我们写视图路径的时候更简单。 -->
        <!-- 希望跳转jsp是[/WEB-INF/views/前缀][xxx变量][.jsp后缀] -->
        <!-- * @see #setPrefix -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- * @see #setSuffix -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 5.配置文件上传解析器。 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为1MB -->
        <property name="maxUploadSize">
            <value>1048576</value>
        </property>
    </bean>
</beans>

 

applicationContext.xml和SpringMVC的区别

applicationContext.xml和SpringMVC的区别


使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而这个一般是采用非spring mvc架构,如使用struts之类而又想引入spring才添加的,这个是用来加载Application Context。
如果直接采用SpringMVC,只需要把所有相关配置放到springMVC.xml中就OK了。

二、
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.


上面那一句所以就解释了我们经常看到的

applicationContex.xml中

<context:component-scan base-package="*.*"
use-default-filters="false">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>

springMVC.xml中

<context:component-scan base-package="*.*" use-default-filters="false"> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan>

今天关于在Spring应用程序中处理* -context.xml和applicationContext.xml文件spring application.yml的讲解已经结束,谢谢您的阅读,如果想了解更多关于APPLICATIONCONTEXT.XML 与 SPRING-MYBATIS.XML、applicationContext.xml中的区别、applicationContext.xml和applicationContext-mvc.xml、applicationContext.xml和SpringMVC的区别的相关知识,请在本站搜索。

本文标签: