spring配置文件回顾

最近项目任务较轻,自己尝试搭建个项目玩玩,在搭建过程中对spring的配置文件回顾了下,形成了大体的配置模板,仅在此作记录,方便日后回顾。

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx">

    <!-- 开启注解处理器 -->
    <context:annotation-config></context:annotation-config>
    <!-- 包扫描范围 开启组件自动扫描,扫描路径为com -->
    <context:component-scan base-package="com"/>
    <!--开启基于@AspectJ切面的注解处理器-->
    <aop:aspectj-autoproxy/>
    <!--使用class属性指定类的默认构造方法创建一个单例bean,默认单例singleton-->
    <bean id="Bean实例名称1" class="com.trh.testBean1" scope="prototype"></bean>
    <!--prototype原型模式 每次生成新的实例-->
    <bean id="Bean实例名称2" class="com.trh.testBean2" scope="prototype"></bean>
    <!--init-method 属性用于指定对象实例化后要调用的初始化方法 destory-method属性用于指定对象在销毁时要调用的方法-->
    <bean id="Bean实例名称3" class="com.trh.testBean3" init-method="bean初始化时调用的方法名" destroy-method="对象销毁时调用的方法名"></bean>

    <bean id="Bean实例名称4" class="com.trh.testBean4">
        <property name="Bean类中的属性名称1" ref="要引用的Bean实例名称"></property>
        <property name="Bean类中的属性名称2" value="直接指定属性值"></property>
        <property name="Bean类中的属性名称3">
            <!-- 创建一个内部匿名Bean实例赋值给指定的属性,改匿名bean实例无法被外界访问 -->
            <bean class="bean类全名"></bean>
        </property>
        <property name="Bean类中的Set类型属性名称">
            <set>
                <!--基本数据类型直接用value标签生成-->
                <value>set中的元素</value>
                <!--引用其他Bean实例作为set元素,由ref标签指定-->
                <ref bean="Bean实例名称1"></ref>
            </set>
        </property>
        <property name="Bean类中的List类型属性名称">
            <list>
                <!--基本数据类型直接用value标签生成-->
                <value>set中的元素</value>
                <!--引用其他Bean实例作为set元素,由ref标签指定-->
                <ref bean="Bean实例名称1"></ref>
            </list>
        </property>
        <property name="Bean类中的Map类型属性名称">
            <map>
                <!--普通字符串自定义key-->
                <entry key="map元素的key">
                    <value>map元素的value</value>
                </entry>
                <!--引用已定义的Bean实例作为key-->
                <entry key-ref="Bean实例名称1">
                    <value>map元素的value</value>
                </entry>
                <!--引用已定义的Bean实例作为key-->
                <entry key-ref="Bean实例名称1">
                    <!--引用已定义的Bean实例作为value-->
                    <ref bean="Bean实例名称2"></ref>
                </entry>
            </map>
        </property>
        <property name="Bean类中的Properties类型属性名称">
            <!--创建一个properties类型的实例赋值给指定的properties类型属性-->
            <props>
                <prop key="properties元素的key">properties元素的value</prop>
            </props>
        </property>
        <property name="Bean类中要初始化为null的属性名称">
            <!--给该属性赋值null-->
            <null></null>
        </property>
    </bean>
    
    <bean id="Bean实例名称5" class="com.trh.testBean5">
        <constructor-arg index="从0开始的序号" type="构造参数类型" value="构造参数的值"></constructor-arg>
        <constructor-arg index="从0开始的序号" type="构造参数类型" ref="要引用的bean实例"></constructor-arg>
    </bean>
    <!--切面配置-->
    <aop:config>
        <aop:aspect id="切面ID" ref="要引入的切面实例名称">
            <aop:pointcut id="切入点名称" expression="切入点正则表达式"></aop:pointcut>
            <aop:before method="前置通知方法名" pointcut-ref="切入点名称"></aop:before>
            <aop:after-returning method="后置通知方法名" pointcut-ref="切入点名称"></aop:after-returning>
            <aop:after-throwing method="异常通知方法名" pointcut-ref="切入点名称"></aop:after-throwing>
            <aop:after method="最终通知方法名" pointcut-ref="切入点名称"></aop:after>
            <aop:around method="环绕通知方法" pointcut-ref="切入点名称"></aop:around>
        </aop:aspect>
    </aop:config>

    <!--配置事物管理-->
    <bean id="事务管理器实例名称" class="事务管理器类全名">
        <property name="数据源属性名称" ref="Bean实例名称5"></property>
    </bean>
    <!--配置一个事务通知-->
    <tx:advice id="事务通知名称" transaction-manager="事务管理器实例名称">
        <tx:attributes>
            <!--方法以get开头的,不使用事务-->
            <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
            <!--其他方法以默认事务进行-->
            <tx:method name="*" rollback-for="Exception.class"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!--aop实现事务管理-->
    <aop:config>
        <aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式"></aop:pointcut>
        <aop:advisor advice-ref="事务通知名称" pointcut-ref="事务切入点名称"></aop:advisor>
    </aop:config>

</beans>
原文地址:https://www.cnblogs.com/id-tangrenhui/p/14744398.html