详解spring applicationContext.xml 配置文件

 更新时间:2017年02月03日 14:09:00   作者:梦想合伙人  
本篇文章主要介绍了详解spring applicationContext.xml 配置文件 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

applicationContext.xml 文件

<?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"> 
 
  <!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 --> 
  <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" /> 
 
  <!-- spring与mybatis整合配置,扫描所有dao --> 
  <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" /> 
 
  <!-- 配置使Spring采用CGLIB代理 --> 
  <aop:aspectj-autoproxy proxy-target-class="true" /> 
 
  <!-- 启用对事务注解的支持 --> 
  <tx:annotation-driven transaction-manager="transactionManager" /> 
 
  <!-- Cache配置 --> 
  <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> 

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;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • IDEA中使用Git的图文教程

    IDEA中使用Git的图文教程

    本文主要介绍了IDEA中使用Git的图文教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 一篇文章带你了解Java Spring基础与IOC

    一篇文章带你了解Java Spring基础与IOC

    这篇文章主要介绍了Java Spring基础与IOC,文中讲解的相关内容非常详细,也运用了大量的代码进行讲解,感兴趣的小伙伴可以参考一下
    2021-08-08
  • 详解Java编程JDialog窗体的用法及实例

    详解Java编程JDialog窗体的用法及实例

    这篇文章主要介绍了Java编程中JDialog窗体的用法及实例,描述了其特征,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • java音乐播放器课程设计

    java音乐播放器课程设计

    这篇文章主要为大家详细介绍了java音乐播放器的课程设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • SpringBoot之自定义banner使用代码实例

    SpringBoot之自定义banner使用代码实例

    这篇文章主要介绍了SpringBoot之自定义banner使用代码实例,在Spring Boot中,你可以通过定制Banner来个性化你的应用程序启动时的输出,Banner是一个在应用程序启动时显示的ASCII艺术字形式的标志,用于增加应用程序的识别度和个性化,需要的朋友可以参考下
    2024-01-01
  • SpringCloud Feign超详细讲解

    SpringCloud Feign超详细讲解

    Feign是Netflix公司开发的一个声明式的REST调用客户端; Ribbon负载均衡、 Hystrⅸ服务熔断是我们Spring Cloud中进行微服务开发非常基础的组件,在使用的过程中我们也发现它们一般都是同时出现的,而且配置也都非常相似
    2022-10-10
  • ConcurrentHashMap 存储结构源码解析

    ConcurrentHashMap 存储结构源码解析

    这篇文章主要为大家介绍了ConcurrentHashMap 存储结构源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • IDEA中已配置阿里镜像但maven无法下载jar包的问题及解决方法

    IDEA中已配置阿里镜像但maven无法下载jar包的问题及解决方法

    这篇文章主要介绍了IDEA中已配置阿里镜像但maven无法下载jar包的问题,本文给大家分享解决方法,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Spring框架中@PostConstruct注解详解

    Spring框架中@PostConstruct注解详解

    在Spring项目经常遇到@PostConstruct注解,下面这篇文章主要给大家介绍了关于Spring框架中@PostConstruct注解的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

    Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法

    这篇文章主要介绍了Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2018-01-01

最新评论