spring配置语法

<?xml version="1.0" encoding="UTF-8"?>

<!--

    beans是整个配置文件的根节点,包含一个或多个bean元素

    xmlns:用于引入页面标签的命名空间定义

    beans、XMLSchema-instance、context是最基本的命名空间定义

    aop:启用AOP功能时的命名空间

    tx:启用声明事务时的命名空间

    mvc:启用springMVC时的命名空间

    xsi:schemaLocation:用于指明xmlns引入的命名空间定义相配套的schema定义文件的装载路径,版本号必须统一,且与对应

    jar包版本相匹配,对应位置在各自对应jar包中的META-INF中的spring.handlers和spring.schemas中,选择合适的定义文件

-->

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

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/aop

                    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

                    http://www.springframework.org/schema/beans

                    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

                    http://www.springframework.org/schema/context

                    http://www.springframework.org/schema/context/spring-context-4.0.xsd

                    http://www.springframework.org/schema/tx

                    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

                    http://www.springframework.org/schema/mvc

                    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!--开启注解处理器,用于启用spring注解-->

    <context:annotation-config/>

 

    <!--开启组件自动扫描,扫描路径由base-package属性定义-->

    <context:conponent-scan base-package="xxx.xxx.xxx"/>

 

    <!-- 开启SpringMVC注解模式 -->

    <mvc:annotation-driven/>

 

    <!--开启基于@AspectJ切面的注解处理器-->

    <aop:aspectj-autoproxy/>

 

    <!--使用class属性指定类的默认构造方法创建一个单实例Bean,名称有id属性指定-->

    <bean id="Bean实例名称" class="Bean类全名"/>

 

    <!--scop属性为prototype时表示每次讲生成新的实例,即原型模式-->

    <bean id="Bean实例名称" class="Bean类全名" scope="prototype"/>

 

    <!--用于指定对象实例化后要调用的初始化方法和销毁时调用的销毁方法-->

    <bean id="Bean实例名称" class="Bean类全名" init-method="初始化时调用的方法" destroy-method="销毁时调用的方法"/>

 

    <bean id="Bean实例名称" class="Bean类全名">

 

        <!--property标签用于对Bean实例中的属性进行赋值,对弈基本数据类型的值可由value属性直接指定,而ref属性则表示对其他bean实例的引用-->

        <property name="Bean类中的属性名称" ref="要引用的Bean的名称"/>

        <property name="Bean类中的属性名称" value="直接指定属性的值/>

 

        <!--创建一个内部匿名Bean实例(匿名内部类)赋值给指定的属性,该匿名Bean实例无法被外界访问-->

        <property name="Bean类中的属性名称">

            <bean class="Bean类全名"/>

        </property>

 

        <property name="Bean类中的Set类型属性名称">

            <!--set标签用于创建一个Set类型的实例赋值给指定的Set类型属性,Set实例中的元素通过value或者ref字标签 指定。

            对于基本数据类型的元素可由value标签生成,如果需要引用其他Bean实例作为Set元素的话,可由ref标签指定-->

            <set>

                <value>set中的元素</value>

                <ref bean="要引用的Bean名称"/>

            </set>

        </property>

 

        <property name="Bean类中的List类型属性名称">

            <!--list标签用于创建一个List类型的实例赋值给指定的List类型属性,List实例中的元素通过value或者ref字标签指定。

            对于基本数据类型的元素可由value标签生成,如果需要引用其他Bean实例作为List元素的话,可由ref标签指定-->

            <list>

                <value>list中的元素</value>

                <ref bean="要引用的Bean名称"/>

            </list>

        </property>

 

        <property name="Bean类中的Map类型属性名称">

            <!--map标签用于创建一个Set类型的实例赋值给指定的Map类型属性,Map实例中的元素通过entry字标签指定。Map元素的键

            entry标签的key属性直接指定,值则可由value或者ref字标签指定,对于基本数据类型的元素可由value标签生成,

            如果需要引用其他Bean实例作为Set元素的话,可由ref标签指定-->

            <map>

                <entry key="map元素的key">

                    <value>map元素的value</value>

                </entry>

                <entry key="map元素的key">

                    <ref bean="要引用的Bean名称"/>

                </entry>

            </map>

        </property>

 

        <property name="Bean类中的Properties类型属性名称">

            <!--创建一个Properties类型的实例赋值给指定的Properties类型的属性-->

            <props>

                <!--Properties实例中的属性项元素由prop标签生成,属性项元素的键由key属性指定,属性项元素的值可直接放置在prop标签中-->

                <prop key="properties元素的key">

                    properties元素的value

                </prop>

            </props>

        </property>

 

        <property name="Bean类中要初始化为null的属性名称">

            <!--null标签用于给需要赋null值得属性进行赋值-->

            <null/>

        </property>

</bean>

 

<bean id="Bean实例名称" class="Bean类全名">

        <!--通过传入相应的构造参数进行Bean实例化,constructor-arg标签用于指定一个构造参数,

        index属性标明当前是第几个构造参数(从0开始),type属性声明构造参数的类型,构

        造参数的值如果是基本数据类型可由value属性直接指定,如果是引用对象,则由ref指定-->

<constructor-arg index="从0喀什的序号" type="构造参数的类型" value="构造参数的值"/>

<constructor-arg index="从0喀什的序号" type="构造参数的类型" ref="要引用的Bean的名称"/>

</bean>

 

<bean id="目标对象名称" class="目标对象类全名"/>

 

<bean id="切面实例名称" class="切面类全名"/>

 

<aop:config>

<aop:aspect id="切面id" ref="要引用的切面实例名称">

<aop:pointcut id="切入点名称" expression="切入点正则表达式"/>

<aop:before pointcut-ref="切入点名称" method="切面类中用做前置通知的方法名"/>

<aop:after-returning pointcut-ref="切入点名称" method="切面类中用做后置通知的方法名"/>

<aop:arter-throwing pointcut-ref="切入点名称" method="切面类中用异常通知的方法名"/>

<aop:arter pointcut-ref="切入点名称" method="切面类中用做最终通知的方法名"/>

<aop:around pointcut-ref="切入点名称" method="切面类中用做环绕通知的方法名"/>

</aop:aspect>

</aop:config>

 

    <!--配置事务管理器-->

<bean id="事务管理器实例名称" class="事务管理器全称">

<property name="数据源属性名称" ref="要引用的数据源实例名称"/>

</bean>

 

<!-- 注解方式配置事物 -->

    <tx:annotation-driven transaction-manager="transactionManager" />

 

    <!-- 用于拦截器配置事务 -->

    <!--配置事务通知-->

    <tx:advice id="事务通知名称" transaction-manager="事务管理器实例名称">

        <tx:attributes>

            <!--方法以get开头的不使用事务-->

            <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>

            <!--其他方法默认开启事务-->

            <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />

            <!--其他方法默认开启事务-->

            <tx:method name="*"/>

        </tx:attributes>

    </tx:advice>

 

    <!-- 使用AOP技术事项事务管理 -->

    <aop:config>

        <aop:pointcut id="事务切入点名称" expression="事务切入点正则表达式"/>

        <aop:advisor advice-ref="事务通知名称" pointcut="事务切入点名称"/>

    </aop:config>

</beans>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值