当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring applicationContext.xml 配置文件

详解spring applicationContext.xml 配置文件

2019年07月22日  | 移动技术网IT编程  | 我要评论

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;

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网