在前面的一篇日志中,记录了web.xml配置启动的顺序,web启动到监听器ContextLoaderListener时,开始加载spring的配置文件applicationContext.xml(通常就叫这个名字),在查询大量资料之后决心将该文件详细的配置说明和讲解记录下来,以供查阅,加深原理的理解。
        首先是spring的各类命名空间和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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
</beans>
        这一部分, 可以自行查阅资料,不去深究也罢。
        以下配置,均在<beans></beans>标签之类,为了便于讲解,直接写出来,一般的在这个配置文件中主要做两件事,扫描注解和注册数据源(数据库信息相关)

第一,扫描注解,看下面两个标签

1、<context:component-scan base-package="cn.byan" use-default-filters="false"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
2、<context:annotation-config />
第1种标签:
base-package填入你要扫描的包
use-default-filters这个 属性表示你要扫描注解的类,
true,默认会扫描注解了这四个标志的类:Service,Component,Repository,Controller;
false,如果你设为false,那么就需要增加 <context:exclude-filter/>这个子节点,指出哪种注解不让spring去默认扫描
当设为false时,需要掌握子节点的这几个属性:
<context:exclude-filter/>:该子节点指定要排除扫描的类,比如一般的@controller的注解类可能会放到mvc的配置文件中去指定扫描
<context:include-filter/>:相反,该子节点指定要扫描的类,什么时候用,还没遇到过
type :通常都是annotation(注解的意思)
expression :注解类的全名(每种注解类在spring中都有全名)
第2种标签:
<context:annotation-config />,也是spring用的比较多的默认扫描的配置,功能和1差不多,但是第一种配置包含第2种配置,所以直选其一即可。1用的比较多

以上标签,通俗点讲,就是:web启动时,默认扫描cn.byan包中注解了@service,@Component,@Repository标注的类,交给spring容器去管理(不扫描@Controller标注的类)。

第二,配置数据源

<!-- 用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
    <value>classpath:conf/jdbc.properties</value>
  </list>
  </property>
</bean>
数据库的配置信息,一般写成配置文件jdbc.properties,放在项目中,在项目启动时加载读取

<!-- dataSource读取数据库的配置信息 ,这里用的是dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" init-method="init" destroy-method="close">
  <property name="driverClassName" value="${jdbc_driverClassName}"></property>
  <property name="url" value="${jdbc_url}"></property>
  <property name="username" value="${jdbc_username}"></property>
  <property name="password" value="${jdbc_password}"></property>
  <!-- 连接池最大使用连接数 -->
  <property name="maxActive" value="${jdbc_maxActive}"></property>
  <!-- 初始化连接大小 -->
  <property name="initialSize" value="${jdbc_initialSize}"></property>
  <!-- 获取连接最大等待时间 -->
  <property name="maxWait" value="${jdbc_maxWait}"></property>
  <!-- 连接池最大空闲 -->
  <property name="maxIdle" value="${jdbc_maxIdle}"></property>
  <!-- 连接池最小空闲 -->
  <property name="minIdle" value="${jdbc_minIdle}"></property>
  <!-- 自动清除无用连接 -->
  <property name="removeAbandoned" value="${jdbc_removeAbandoned}"></property>
  <!-- 清除无用连接的等待时间 -->
  <property name="removeAbandonedTimeout" value="${jdbc_removeAbandonedTimeout}"></property>
  <!-- 事物是否自动提交-->
  <property name="defaultAutoCommit" value="${jdbc_defaultAutoCommit}"></property>
  <!-- 连接属性 -->
  <property name="connectionProperties" value="clientEncoding=UTF-8"></property>
</bean> <!-- 配置SessionFactory,这里用的是hibernate4,mybatis目前还不太熟 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.byan.entity" />
  <property name="hibernateProperties">
  <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  </props>
  </property>
</bean> <!-- 定义事务管理器,用了spring管理的hibernate4的事物,再也不用担心事物。。。-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 声明事务管理器,此时在ServiceImpl或者DaoImpl类上需加上@Transactional注解-->
<tx:annotation-driven transaction-manager="transactionManager" />

以上是applicationContext.xml配置文件要掌握的最基础的配置使用信息

第九篇:Spring的applicationContext.xml配置总结的更多相关文章

  1. 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决

    问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...

  2. spring的applicationContext.xml配置SessionFactory抛异常

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFa ...

  3. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  4. Spring的配置文件ApplicationContext.xml配置头文件解析

    Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...

  5. 这一次搞懂Spring Web零xml配置原理以及父子容器关系

    前言 在使用Spring和SpringMVC的老版本进行开发时,我们需要配置很多的xml文件,非常的繁琐,总是让用户自行选择配置也是非常不好的.基于约定大于配置的规定,Spring提供了很多注解帮助我 ...

  6. Spring的applicationContext.xml文件

    以下是详解Spring的applicationContext.xml文件代码:<!-- 头文件,主要注意一下编码 --><?xml version="1.0" e ...

  7. springmvc.xml和applicationContext.xml配置的特点

    1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" ...

  8. Spring IOC-基于XML配置的容器

    Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...

  9. Spring Ioc容器xml配置

    Spring Ioc容器xml配置基本结构: <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

随机推荐

  1. ajax--getJSON

    penson.json [ { "name":"张三", "age":25, "sex":"男", ...

  2. Thymeleaf语法总结

    Thymeleaf是Spring boot推荐使用的模板引擎. 一.th属性 html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个.其中th属性执行的优先级从1~8,数字越低优先级越 ...

  3. Spring boot热部署实战

    1.介绍 在开发工程中,修改一点儿代码,想看效果就需要重新启动服务,这样会花费大量时间在重启服务上,通过devtools热部署可以大大减少重启服务的时间. 之所以能减少时间,是因为Spring Boo ...

  4. 2019 hdu多校1

    A:一类线性dp,时间卡的有点紧 /* 定义 dp[t][i][j][k]代表填完前 t 个位置后,{0, 1, 2, 3} 这 4 个数字最后一次出现的位置, 排序后为 t, i, j, k(t & ...

  5. Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow

    2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...

  6. Go将统治下一个10年?Go语言发展现状分析

    “本文是国内Go语言大中华区首席布道师——许式伟,在QCon2015上海站上的分享.他预测Go语言10年内一定会超过C和java,并且统治这一个10年. Go语言语法及标准库变化 Go从1.0版本到现 ...

  7. spring boot部署到阿里云碰到的总总问题

    2375错误,我没装docker,从pom中删了吧 mysql,不能写本机对外,得写127.0.0.1. 如何生成jar包,在pom中写上jar <groupId>com.coding&l ...

  8. topjui.common.js

    function getTabWindow() { var curTabWin = null; if (topJUI.config.aloneUse) { curTabWin = window; } ...

  9. python之tkinter学习目录

    前言 下面的目录结构,采用的学习视频资料是网易云课堂中[莫凡]老师的,在目录的最下面的地方给出了对应的链接! 学习是逐渐积累起来的,代码也是!下面的每一篇中的对应代码,都秉承着这样的一个理念:代码是成 ...

  10. 28-Ubuntu-远程管理命令-02-查看网卡的配置信息

    命令 功能 ifconfig 查看网卡配置信息 ifconfig | grep inet 查看网卡对应的IP地址 ping  127.0.0.1 检测本地网卡是否正常 ping  IP地址 检测到目标 ...