您的位置:首页 > 移动开发

spring完整配置文件(applicationContext.xml)

2018-02-24 16:33 357 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: