spring完整配置文件(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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
<!-- 啓動自動掃描該包下所有的Bean(例如@Controller) -->
<context:component-scan base-package="com.spider" />


    <bean id="duke" class="com.springinaction.springidol.Juggler" > 
        <!-- 通過構造方法設置屬性值 -->
        <constructor-arg value="15"></constructor-arg>
    </bean>
    
    <bean id="sonnect29" class="com.springinaction.springidol.Sonnet29"></bean>
    
    <bean id="poeticPoem" class="com.springinaction.springidol.PoeticJuggler">
        <constructor-arg value="15"></constructor-arg>
        <constructor-arg ref="sonnect29"></constructor-arg>
    </bean>
    
        <!-- 建立一個Instrumentalist bean 
        @通過property爲bean設置屬性值,一旦instrumentalist被實例化,則對象會被賦此值
    -->
    <bean id="Kenny" class="com.springinaction.springidol.Instrumentalist">
        <property name="song" value="Jingle Bells"></property>
        <property name="age" value="37"></property>
        
        <!-- 這種做法可以實現接口與類的鬆耦合,比如下面兩個都實現了Instrument接口的樂器類,Kenny bean可以隨意引用 -->
        <!-- 
        <property name="instrument" ref="saxphone"></property> 
        <property name="instrument" ref="piano"></property> 
        -->
        <!-- 內部bean的使用方式,這裏用在property,constructor裏面也是一樣用 -->
        <property name="instrument">
            <bean class="com.springinaction.springidol.piano"></bean>
        </property>
    </bean>
    
    <bean id="saxphone" class="com.springinaction.springidol.saxphone"></bean>
    <bean id="piano" class="com.springinaction.springidol.piano"></bean>
    
    <!-- p命名空間用法 -->
    <bean id="Kenny2" class="com.springinaction.springidol.Instrumentalist" 
        p:song="Lemon Tree" p:age="30" p:instrument-ref="saxphone"    >
    </bean>
    
    <!-- 爲集合配置bean -->
    <bean id="hank" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <list>
                <ref bean="piano" />
                <ref bean="saxphone" />
            </list>
        </property>
        <property name="instruments2">
            <map>
                <entry key="piano" value-ref="piano"></entry>
                <entry key="saxphone" value-ref="saxphone"></entry>
            </map>
        </property>
    </bean>
    
    <!-- properties的寫法 -->
    <bean id="hank2" class="com.springinaction.springidol.OneManBand">
        <property name="instruments">
            <props>
                <!-- key和value都爲String -->
                <prop key="piano">la la la</prop> 
                <prop key="saxphone">ta ta ta</prop>
            </props>
        </property>
    </bean>
    
    <!-- 賦null值 -->
    <!-- 
    ...
        <property name="xxx"><null/></property>
    ...

     -->

    <!-- 配置一個切面 -->
    <aop:config>
        <aop:aspect id="helloWorldAspect" ref="helloWorldAspectBean">
        <!-- 配置切點 -->
            <aop:pointcut id="helloWorldServicePointcut" expression="execution(* com.gao.spring.aop.*.*(..))" />
            
            <!-- 配置前置通知 -->
            <aop:before pointcut-ref="helloWorldServicePointcut" method="beforeAdvice" />
            <!-- 配置前置通知 -->
            <aop:after pointcut-ref="helloWorldServicePointcut" method="afterAdvice" />
            <!-- 配置後置返回通知 -->
            <aop:after-returning pointcut-ref="helloWorldServicePointcut" method="afterReturnAdvice" returning="result" />
            <!-- 配置環繞通知 -->
            <aop:around pointcut-ref="helloWorldServicePointcut" method="aroundAdvice" />
            <!-- 異常通知 -->
            <aop:after-throwing pointcut-ref="helloWorldServicePointcut" method="throwingAdvice" throwing="e" />
        </aop:aspect>
    </aop:config>

    <!-- 配置一個切面with arg -->
    <aop:config>
        <aop:aspect id="jokerTheMindReader" ref="joker">
        <!-- 配置切點 -->
            <aop:pointcut id="cycloneTheThinker" expression="execution(* com.gao.spring.aop.args.CycloneTheThinker.thinkOfSomeThing(String)) and args(thought)" />
            
            <!-- 配置前置通知 -->
            <aop:before pointcut-ref="cycloneTheThinker" method="intercepetThought" arg-names="thought" />
        </aop:aspect>

    </aop:config>


 <!-- 導入其他文件 -->    

<import resource="applicationContext-mongodb.xml"/>

<import resource="applicationContext-msg.xml"/>
<import resource="applicationContext-activity.xml"/>
<import resource="applicationContext-jobs.xml"/>
<import resource="applicationContext-tgk.xml"/>

<import resource="applicationContext-hessian.xml"/>


<!-- 加入spring註解 -->

<context:annotation-config />


<!-- 激活aop自動代理功能,要保證xml文件頭有aop的引用 -->
<aop:aspectj-autoproxy proxy-target-class="true" />

</beans>
發佈了25 篇原創文章 · 獲贊 14 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章