Detail the spring applicationContext.xml configuration file

  • 2020-06-01 09:52:43
  • OfStack

applicationContext xml file

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="" xmlns:context="" xmlns:xsi="" xmlns:aop="" 
  xmlns:tx="" xmlns:p="" xmlns:util="" xmlns:jdbc="" 
  <!--  Automatic scanning web package  , Class that will be annotated   Included in the spring Container management  --> 
  <context:component-scan base-package=""></context:component-scan> 
  <!--  The introduction of jdbc The configuration file  --> 
  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
  <!-- dataSource  configuration  --> 
  <bean id="dataSource" class="" init-method="init" destroy-method="close"> 
    <!--  Basic attributes  url , user , password --> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
    <!--  Configure the initial size, minimum, and maximum  --> 
    <property name="initialSize" value="1" /> 
    <property name="minIdle" value="1" /> 
    <property name="maxActive" value="20" /> 
    <!--  Configure to get the time for the connection wait timeout  --> 
    <property name="maxWait" value="60000" /> 
    <!--  How long is the configuration interval 1 A secondary detection that detects idle connections that need to be closed in milliseconds  --> 
    <property name="timeBetweenEvictionRunsMillis" value="60000" /> 
    <!--  configuration 1 The minimum time a connection can live in a pool, in milliseconds  --> 
    <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" /> 
    <!--  Open the PSCache , and specify each connection PSCache The size of the  --> 
    <property name="poolPreparedStatements" value="false" /> 
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> 
    <!--  Configure monitoring statistics intercept filters --> 
    <property name="filters" value="stat" /> 
  <!-- mybatis File configuration, scan all mapper file  --> 
  <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 with mybatis Integrate the configuration and scan all dao --> 
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="" p:sqlSessionFactoryBeanName="sqlSessionFactory" /> 
  <!--  right dataSource  Data source for transaction management  --> 
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> 
  <!--  Configuration to make Spring using CGLIB The agent  --> 
  <aop:aspectj-autoproxy proxy-target-class="true" /> 
  <!--  Enable support for transaction annotations  --> 
  <tx:annotation-driven transaction-manager="transactionManager" /> 
  <!-- Cache configuration  --> 
  <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" /> 

1. < context:component-scan base-package="" > < /context:component-scan > Initialization function Spring container, will scan com. eduoinfo. finances. bank. Under web marked (@ Component, @ Service @ Controller, @ Repository) annotation class into spring container management

On the class, implement the bean declaration using the following annotations

Component refers to components in general, and we can use this annotation to annotate components when they are not easy to categorize.

@Service is used to annotate business-tier components

@Controller is used to annotate control layer components (e.g. controller of srping mvc, action of struts)

@Repository is used to annotate the data access component, the DAO component


@RequestMapping(value = "/test")
public class TestController {


On the member variables of the class, use the following annotations to implement the automatic assembly of properties

Autowired: assemble by type of class

Resource (recommended) :

1 if both name and type are specified, bean with only 1 matching is found in the context of spring for assembly, and an exception is thrown if it is not found

2. If name is specified, bean with a matching name (id) is searched from the context for assembly, and an exception is thrown if it is not found

3. If type is specified, only 1bean with a type matching is found in the context for assembly. If it is not found or more than one is found, an exception will be thrown

4. If neither name nor type is specified, the assembly will be automatically carried out in the way of byName; If there is no match, it will be matched back to 1 original type. If there is a match, it will be automatically assembled.

The @Resource annotation is on the field, so you don't have to write the setter method, and the annotation belongs to J2EE, reducing the coupling to spring.


private TestServiceImpl testServiceImpl;

Related articles: