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">

    <<span style="color:#ff0000;">!-- 自動掃描web包 ,將帶有註解的類 歸入spring容器管理 --></span>
    <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" />

    <span style="color:#ff0000;"><!-- spring與mybatis整合配置,掃描全部dao --></span>
    <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" />

    <span style="color:#ff0000;"><!-- 配置使Spring採用CGLIB代理 --></span>
    <aop:aspectj-autoproxy proxy-target-class="true" />

   <span style="color:#ff0000;"> <!-- 啓用對事務註解的支持 --></span>
    <tx:annotation-driven transaction-manager="transactionManager" />

<span style="color:#ff0000;">    <!-- Cache配置 --></span>
    <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>

  

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自定掃描的包 -->
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

    <!-- 配置視圖解析器: 如何把 handler 方法返回值解析爲實際的物理視圖 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 配置視圖  BeanNameViewResolver 解析器: 使用視圖的名字來解析視圖 -->
    <!-- 經過 order 屬性來定義視圖解析器的優先級, order 值越小優先級越高 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
        <property name="order" value="100"></property>
    </bean>
    
    <!-- 配置國際化資源文件 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>    
    </bean>
    
    <!-- 配置直接轉發的頁面 -->
    <!-- 能夠直接相應轉發的頁面, 而無需再通過 Handler 的方法.  -->
    <mvc:view-controller path="/success" view-name="success"/>
    
    <!-- 在實際開發中一般都需配置 mvc:annotation-driven 標籤 -->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

 

  


 
一、<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 的聲明javascript

@Component 泛指組件,當組件很差歸類的時候,咱們可使用這個註解進行標註。html

@Service 用於標註業務層組件java

@Controller 用於標註控制層組件(如srping mvc的controller,struts中的action)web

@Repository 用於標註數據訪問組件,即DAO組件spring

示例:sql

@Controller
@RequestMapping(value = "/test")
public class TestController {spring-mvc

}mybatis

------------------------------------------------------------------------------------------------------------------mvc

在類的成員變量上,使用如下註解,實現屬性的自動裝配app

@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;

 

 

 

 

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
</beans>

  

 

 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 9 
10     <!-- 配置自定掃描的包 -->
11     <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
12 
13     <!-- 配置視圖解析器: 如何把 handler 方法返回值解析爲實際的物理視圖 -->
14     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15         <property name="prefix" value="/WEB-INF/views/"></property>
16         <property name="suffix" value=".jsp"></property>
17     </bean>
18     
19     <!-- 配置視圖  BeanNameViewResolver 解析器: 使用視圖的名字來解析視圖 -->
20     <!-- 經過 order 屬性來定義視圖解析器的優先級, order 值越小優先級越高 -->
21     <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
22         <property name="order" value="100"></property>
23     </bean>
24     
25     <!-- 配置國際化資源文件 -->
26     <bean id="messageSource"
27         class="org.springframework.context.support.ResourceBundleMessageSource">
28         <property name="basename" value="i18n"></property>    
29     </bean>
30     
31     <!-- 配置直接轉發的頁面 -->
32     <!-- 能夠直接相應轉發的頁面, 而無需再通過 Handler 的方法.  -->
33     <mvc:view-controller path="/success" view-name="success"/>
34     
35     <!-- 在實際開發中一般都需配置 mvc:annotation-driven 標籤 -->
36     <mvc:annotation-driven></mvc:annotation-driven>
37 
38 </beans>
View Code
相關文章
相關標籤/搜索