spring applicationContext.xml 配置文件詳解

applicationContext.xml 文件

[html] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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"  
  3.     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"  
  4.     xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/context  
  7.     http://www.springframework.org/schema/context/spring-context.xsd  
  8.     http://www.springframework.org/schema/beans  
  9.     http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.     http://www.springframework.org/schema/tx  
  11.     http://www.springframework.org/schema/tx/spring-tx.xsd  
  12.     http://www.springframework.org/schema/jdbc  
  13.     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
  14.     http://www.springframework.org/schema/cache  
  15.     http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
  16.     http://www.springframework.org/schema/aop  
  17.     http://www.springframework.org/schema/aop/spring-aop.xsd  
  18.     http://www.springframework.org/schema/util  
  19.     http://www.springframework.org/schema/util/spring-util.xsd">  
  20.   
  21.     <!-- 自動掃描web包 ,將帶有註解的類 納入spring容器管理 -->  
  22.     <context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>  
  23.   
  24.     <!-- 引入jdbc配置文件 -->  
  25.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  26.         <property name="locations">  
  27.             <list>  
  28.                 <value>classpath*:jdbc.properties</value>  
  29.             </list>  
  30.         </property>  
  31.     </bean>  
  32.   
  33.     <!-- dataSource 配置 -->  
  34.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  35.         <!-- 基本屬性 url、user、password -->  
  36.         <property name="url" value="${jdbc.url}" />  
  37.         <property name="username" value="${jdbc.username}" />  
  38.         <property name="password" value="${jdbc.password}" />  
  39.   
  40.         <!-- 配置初始化大小、最小、最大 -->  
  41.         <property name="initialSize" value="1" />  
  42.         <property name="minIdle" value="1" />  
  43.         <property name="maxActive" value="20" />  
  44.   
  45.         <!-- 配置獲取連接等待超時的時間 -->  
  46.         <property name="maxWait" value="60000" />  
  47.   
  48.         <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->  
  49.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  50.   
  51.         <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->  
  52.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  53.   
  54.         <property name="validationQuery" value="SELECT 'x'" />  
  55.         <property name="testWhileIdle" value="true" />  
  56.         <property name="testOnBorrow" value="false" />  
  57.         <property name="testOnReturn" value="false" />  
  58.   
  59.         <!-- 打開PSCache,並且指定每個連接上PSCache的大小 -->  
  60.         <property name="poolPreparedStatements" value="false" />  
  61.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  62.   
  63.         <!-- 配置監控統計攔截的filters -->  
  64.         <property name="filters" value="stat" />  
  65.     </bean>  
  66.   
  67.     <!-- mybatis文件配置,掃描所有mapper文件 -->  
  68.     <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" />  
  69.   
  70.     <!-- spring與mybatis整合配置,掃描所有dao -->  
  71.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />  
  72.   
  73.     <!-- 對dataSource 數據源進行事務管理 -->  
  74.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />  
  75.   
  76.     <!-- 配置使Spring採用CGLIB代理 -->  
  77.     <aop:aspectj-autoproxy proxy-target-class="true" />  
  78.   
  79.     <!-- 啓用對事務註解的支持 -->  
  80.     <tx:annotation-driven transaction-manager="transactionManager" />  
  81.   
  82.     <!-- Cache配置 -->  
  83.     <cache:annotation-driven cache-manager="cacheManager" />  
  84.     <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />  
  85.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />  
  86.   
  87. </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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章