这可能是你见过的最长的Spring配置文件模板及解析了

前言:

1、IDE:IDEA-2019.03

2、配置Spring的xml文件时,如果是一个web项目,可以先在web.xml里配置spring的xml路径

具体如下:

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
 </context-param>

注意:classpath属性以及下面的class属性那里需要填写正确的路径,关于如何填写出正确的路径可以看下这一篇博客

https://blog.csdn.net/weixin_43694216/article/details/104297113

3、关于Spring的配置文件学习,需要有一定前置知识,也就是Spring相关知识

4、这篇博客借鉴了诸多博客的内容,具体引用在最下面

一个比较完整的Spring配置文件从上到下大致可以有以下几个部分:

1、版本号、字符集

2、Schema约束

1、2算配置的头,也是整个配置文件的基础

3、注解处理器

开启与spring IOC 相关的注解配置                                                               

4、自动扫描

以上都算是spring的前置配置

6、DAO层配置及其它配置的引入

引入其它配置

7、bean实例的装配

 7 是配置IOC容器,是控制反转基础

7、配置基于Aspectj切面的注解处理器

7是为了能够使用注解来便捷的配置Spring切面相关

8、AOP配置

扫描二维码关注公众号,回复: 9643638 查看本文章
7 是配置AOP,便于管理事务

9、事务管理器的配置

10、事务通知的配置

9、10是事务管理方面的配置

11、基于AOP的事务管理的配置

11是将AOP与事务管理相结合的配置

而整个spring配置文件大致五个部分:

  1. 约束引入,自动扫描等前置配置
  2. jdbc及数据库相关的配置
  3. IOC相关配置(bean实例装配相关)
  4. AOP相关相关配置(切面配置等)
  5. 事务管理相关
注意,Spring的IOC,AOP开发配置往往有基于注解和xml配置两种,请根据实际情况选择

下面将按顺序展示spring配置文件的创建

注意看注释!!!

(完整的配置文件在最下面!!!)

一、版本号、字符集与schema约束

版本号、字符集没啥好说的

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

而约束,在eclipse里引入约束比较麻烦,而在idea里可以在项目的new选项那里点击XML Configuration File选择Spring Xml,然后自动创建一个包含版本号、字符集、少量Schema的XML文件。
但是初始的schema无法满足我们的要求,而一般一个Spring项目往往会有以下Schema

<beans 
	   <!-- 基本的命名空间定义 -->
	   xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       
       <!-- 引入p命名空间 -->
       xmlns:p="http://www.springframework.org/schema/p
       
	   <!-- 启用AOP功能时的命名空间 -->
	   xmlns:aop="http://www.springframework.org/schema/aop"
       
       <!-- 启用自动扫描与注解装配时的命名空间 -->
       xmlns:context="http://www.springframework.org/schema/context"
       
       <!-- 启用声明事务时的命名空间 -->
       xmlns:tx="http://www.springframework.org/schema/tx"
      
      <!-- 与上述命名空间定义相配套的schema定义文件的路径 -->
       xsi:schemaLocation=
      "http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
       
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">

其实这里可以强行找一下规律,记一下这些约束,至少要对其有印象。
从上面可见aop、beans、context、tx相关的约束都是spring需要引入的,而其路径两个一组,也很有规律。里面的约束都是有版本号的,可酌情使用自己需要的版本(复制上面的地址到浏览器打开,查找需要的版本),也可以直接忽略不写(至于为什么可以忽略不写,打开上面的地址就知道了)

二、注解处理器、自动扫描的配置

开启注解处理器是为了能够在Spring里使用注解进行开发

<!--   开启注解处理器      -->
<context:annotation-config />

开启组件自动扫描,由base-package指定相关路径

<!--    自动扫描指定的包(base-package),resource-pattern为指定扫描的资源    -->
<!-- 在Spring-MVC里此标签只用来扫描@Controller -->
<!-- 在Spring中该标签扫描除了控制器之外的事务逻辑 -->
<context:component-scan base-package=" "  resource-pattern="com/spring/*.xml" use-default-filters="false">

	<!-- 下面的两个标签往往与Spring-MVC相关,方便进行精准扫描 -->
	<!-- include-filter为设置白名单,指定类可以被扫描进来 -->
	<!-- 指定扫描注解,需结合自动扫描的use-default-filter谁"false"使用,也就是关闭默认的过滤器,使得只有白名单的类可以被扫描进来 -->
	<!-- expression表示需要排除的类,填全限定类名 -->
	<context:include-filter type="annotaion" expression="" />

	<!-- exclude-filter为设置黑名单,被指定的类无法被扫描进来 -->
	<context:exclude-filter type="annotation" expression="" />
</context:component-scan>
#### 三、dao层配置及其它配置的引入
##### 一、引入jdbc-config.properties文件,也就是与jdbc相关的properties文件
```java
<context:property-placeholder location = "classpath:jdbc-config.properties" />
<!-- profile值为其名,可通过@ActiveProfiles("main")注解使用该配置 -->
<!-- 这里引入阿里的druid连接池 -->
<beans profile="main">
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<!-- $引入配置文件里对应的值 -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="minIdle" value="${minIdle}" />
	</bean>
</beans>

注意classpath的填写,请看下面的博文:

https://blog.csdn.net/weixin_43694216/article/details/104297113

在XML文件里可以配置多个datasource,但是这些profile
而jdbc-config.properties文件内容一般如下(这里我以MariaDB为例):

#MariaDB驱动
jdbc.driverClassName = org.mariadb.jdbc.Driver

#JDBC URL
jdbc.url = jdbc:mariadb://localhost:3306/gradle_web_ssm?useUnicode:true&characterEncoding:UTF-8

#用户名、密码
jdbc.username = root
jdbc.password = 123456

#实体类所在包
package.model = com.pluto.mybatis.demo.entity

#mapper所在包
package.mapper = com.pluto.mybatis.demo.mapper

#mapper 相关xml文件包,resource目录下
package.xml = mybatis/mapping

#初始化连接大小
initialSize = 0

#连接池最大数
maxActive = 20

#连接池最大空闲
maxIdle = 20

#连接池最小空闲
minIdle = 1

#获取连接最大等待时间
maxWait = 60000
二、其它属性配置文件及配置的引入
<!-- 其它属性配置文件的引入 -->
<context:property-placeholder class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
	<!-- 字符串数组,可配置多个文件 -->
	<!-- name表示待配置的属性,location表示路径,注意正确填写 -->
	<property name="location">
		<array>
			<value>classpath:dababase-config.properties</value>
			<value>classpath:log4j.properties</value>
		</array>
	</property>
	<!-- ignoreResourceNotFound值为true表示允许配置文件不存在 -->
	<property name="ignoreResourceNotFound" value="true" />
</bean>

<!-- 整合spring,mybatis用的 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>

 <!-- spel的引入 -->
<bean id=" " class=" ">
    <!--@Value,#{}内是表达式的值,可放在property或constructor-arg内 -->
    <property name="arg" value="#{表达式}">
</bean>

四、bean实例的装配

在spring里装配bean实例有两种,一种是在XML里进行bean的装配,另一种是使用注解进行bean的装配(需要先开启注解处理器)。两种方式各有优缺点,但是都需要掌握。

bean的xml装配有setter注入和构造注入两种

所谓setter注入即在bean里使用set方法来配置属性,然后在XML文件里利用setter方法对bean实例注入属性。具体如下:

<!-- id值建议填写你要生成的bean实例的名称,class值填bean的全限定类名 -->

<!-- init-method内填spring容器初始化 后 需运行的方法 -->
<!-- destory-method内填spring容器销毁 前 需运行的方法 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<!-- name值为要注入的属性名,value处填其值 -->
	<property name=" " value=" " />
	
	<!-- ref填要引用的bean全限定类名,也就是当属性存在其它bean引用时用 -->
	<property name=" " ref=" " />
</bean>

<!-- 如果引入p命名空间,以下写法也可,此为setter注入 -->
<bean id=" " class=" " p:name="" p:name-ref=" "/>

注:scope值可以为singleton、prototype、session、request

singleton:
单例模式,表示每次从IOC容器获取的对象都是同一个

prototype:
原型模式,表示每次从IOC容器获取的对象都不同

session:
表示web项目中,会话过程中容器创建的对象只有一个

request:
表示web项目中,一次请求过程中容器创建的对象只有一个

所谓构造注入即在bean里使用构造器来配置属性,然后在XML文件里利用构造器为bean实例注入属性。具体如下:

<!-- id值建议填写你要生成的bean实例的名称,class值填bean的全限定类名 -->
<bean id=" " class=" ">
	<!-- index为索引值,如不给出会有默认值,但建议按属性顺序给出值 -->
	<!-- name值为要注入的属性名,value处填其值 -->
	<constructor-arg index=" " name=" " value=" " />

	<!-- type可指定构造参数类型,ref填要引用的bean全限定类名 -->
	<constructor-arg index=" " type=" " ref=" " />
</bean>

注意:
1、实际开发中,以setter注入为主。
2、bean的全限定类名正确查找方式请看下面的博文

https://blog.csdn.net/weixin_43694216/article/details/104297113

3、关于单例模式与原型模式可参照这篇博文

4、当属性为特殊类型时,使用setter方法注入略有不同
一、属性为map时

<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="map" >
		<map>
			<!-- 键为引用先定义好的bean,值为引用先定义好的bean -->
			<entry key-ref=" " value-ref=" "/>
		</map>
	</property>
</bean>

二、属性为set时

<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="set" >
		<set>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</set>
	</property>
</bean>

三、属性为list时

<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="list" >
		<list>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</list>
	</property>
</bean>

四、属性为array时

<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="array" >
		<array>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</array>
	</property>
</bean>

五、AOP配置

<!-- AOP配置 -->
<!-- 配置切点的bean,id为目标对象名称,class为目标对象全限定类名 -->
<bean id="" class=""/>

<!-- 配置切面的bean,id为切面实例名称,class为切面类全限定类名  -->
<bean id="" class="" />

<!-- 系统服务组件的切面Bean -->
<bean id="aspectService" class="com.tera.sys.filter.LogAspect">

<!-- 传统的基于代理的aop配置 创建代理工厂 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 配置目标对象 -->
        <property name="targetName" value=""></property>
        <!-- 配置通知(字符串数组) -->
        <property name="interceptorNames" value="..,.."></property>
        <!-- 配置目标对象实现的接口(Class数组)(配不配都行) -->
        <property name="interfaces" value="..,.."></property>
</bean>

<!--  开启基于AOP切面事务的注解处理器,需要引入aop命名空间,且该声明优于基于XML的声明   -->
<!-- 基于注解开发aop -->
<aop:aspectj-autoproxy />

<!-- 基于xml配置开发aop -->
<!-- proxy-target-class 属性控制是基于接口的还是基于类的代理被创建,true为基于类的代理创建 -->
<aop:config proxy-target-class="true">
	<!-- id为切面类ID,可有可无,一般填类名 ref填要引用的切面实例名称,相当于@Aspect -->
	<!-- order设置优先级,数字越小优先级越高 -->
	<aop:aspect id="" ref="" order="1">

		<!-- 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" -->

		<!-- id为切入点名称,expression为切入点表达式 -->	
		<!--切点不需要根对应的bean相关联,只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
		<!-- 等同于@Point -->
		<!-- pointcut(切入点)可以定义在aop:config下,如定义在aop:aspect元素下意味着只对当前切面有效, -->
		<!-- 基于XNL的aop配置不允许在切入点表达式中用名称引入其它切入点 -->
		<!-- 此处引入切入点既可以使用pointcut-ref引用切点,也可以使用expression引入切入点表达式 -->
		<aop:pointcut id="" expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))||execution (* com.tera.*..*Service.*(..))" />

		<!-- pointcut-ref为切入点名称,method为前置通知的方法名 -->
		<aop:before pointcut-ref="" method="beforeMethod" />

		<!-- pointcut-ref为切入点名称,method为后置通知的方法名 -->
		<aop:after-returning pointcut-ref="" method="afterRunningMethod"/>

		<!-- pointcut-ref为切入点名称,method为异常通知的方法名 -->
		<aop:after-throwing pointcut-ref="" method="throwingMethod" />

		<!-- pointcut-ref为切入点名称,method为最终通知的方法名 -->
		<aop:after pointcut-ref="" method="afterMethod" />

		<!-- pointcut-ref为切入点名称,method为环绕通知的方法名 -->
		<aop:around pointcut-ref="" method="" />
	</aop:aspect>
</aop:config>

注意:当通过类定义切入点(还可以通过接口定义),例如其表达式有 execution (* com.tera.*…Service.(…))

execution()是最常用的切点函数,其语法(SPEL)如下所示:

整个表达式可以分为这几个部分:

1、execution(): 表达式主体。

2、第一个*号:表示返回类型,*号表示所有的类型。

3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包。

4、第二个*号:表示子包,这里表示所有的子包。

5、第三个*号:表示以Service结尾的所有类

6、*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

7、这个表达式表示拦截com.tera包、子孙包下所有以Service结尾的类的方法并返回

六、事务管理器配置

<!-- 配置事务管理器 -->
<!-- id为事务管理器实例名称,class为事务管理器类全限定类名 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
	<!-- name为数据源属性名称,ref为要引用的数据源实例名称 -->
	<property name="" ref="" />
</bean>

七、事务通知配置

<!-- 基于xml配置事务通知 -->
<tx:advice id="" transaction-manager="">
	<tx:attributes>
		<!-- name为需执行配置的事务通知的方法,isolation为隔离方式,propagation为传播行为,rollback-for为异常回滚 -->
		<!-- 以get开头的方法被设置为不支持事务,其它方法以默认事务进行 -->
			<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
			<!-- read-only为true意味着在只读事务里开启该类方法 -->
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" isolation="default" read-only="true"/>
            
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" rollback-for="Exception" isolation="default"/>
	</tx:attributes>
</tx:advice>

八、基于AOP技术的事务管理配置

<!-- 使用AOP技术实现事务管理 -->
<aop:config>
	<!-- 配置AOP切面 -->
	<!-- id为事务切入点名称,expression事务切入点正则表达式 -->
	<aop:pointcut-ref="" expression="execution(* *..service.*.*(..))" />
	<!-- advice为事务通知名称,pointcut-ref为事务切入点名称-->
	<aop:advisor advice-ref="" pointcut-ref="" />
</aop:config>

<!-- 开启基于注解的事务配置 -->
<!-- 方法上配置 @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollBackFor=FundException.class) -->
<!-- 配置事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

九、引用如下:

https://blog.csdn.net/qq_39949109/article/details/82490864
https://blog.csdn.net/hyd130/article/details/73480943
https://www.cnblogs.com/Sunnor/p/5688967.html
https://www.jianshu.com/p/46f3605cf85f
https://blog.csdn.net/qq_43386754/article/details/88131308
https://blog.csdn.net/qq_42109746/article/details/89678408

十、所有配置内容(请自行甄选内容)

里面有诸多注释,方便初学者学习.虽然配置的内容很多,但不是所有的配置一定要引入,根据情况选择所需的那部分即可

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

<beans 
	   <!-- 基本的命名空间定义 -->
	   xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       
       <!-- 引入p命名空间 -->
       xmlns:p="http://www.springframework.org/schema/p
       
	   <!-- 启用AOP功能时的命名空间 -->
	   xmlns:aop="http://www.springframework.org/schema/aop"
       
       <!-- 启用自动扫描与注解装配时的命名空间 -->
       xmlns:context="http://www.springframework.org/schema/context"
       
       <!-- 启用声明事务时的命名空间 -->
       xmlns:tx="http://www.springframework.org/schema/tx"
      
      <!-- 与上述命名空间定义相配套的schema定义文件的路径 -->
       xsi:schemaLocation=
      "http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
       
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd" >





<!--   开启注解处理器      -->
<context:annotation-config />

<!--    自动扫描指定的包(base-package),resource-pattern为指定扫描的资源    -->
<!-- 在Spring-MVC里此标签只用来扫描@Controller -->
<!-- 在Spring中该标签扫描除了控制器之外的事务逻辑 -->
<context:component-scan base-package=" "  resource-pattern="com/spring/*.xml" use-default-filters="false">

	<!-- 下面的两个标签往往与Spring-MVC相关,方便进行精准扫描 -->
	<!-- include-filter为设置白名单,指定类可以被扫描进来 -->
	<!-- 指定扫描注解,需结合自动扫描的use-default-filter谁"false"使用,也就是关闭默认的过滤器,使得只有白名单的类可以被扫描进来 -->
	<!-- expression表示需要排除的类,填全限定类名 -->
	<context:include-filter type="annotaion" expression="" />

	<!-- exclude-filter为设置黑名单,被指定的类无法被扫描进来 -->
	<context:exclude-filter type="annotation" expression="" />
</context:component-scan>






<context:property-placeholder location = "classpath:jdbc-config.properties" />
<!-- profile值为其名,可通过@ActiveProfiles"main")注解使用该配置 -->
<!-- 这里引入阿里的druid连接池 -->
<beans profile="main">
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<!-- $引入配置文件里对应的值 -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="minIdle" value="${minIdle}" />
	</bean>
</beans>





<!-- 其它属性配置文件的引入 -->
<context:property-placeholder class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
	<!-- 字符串数组,可配置多个文件 -->
	<!-- name表示待配置的属性,location表示路径,注意正确填写 -->
	<property name="location">
		<array>
			<value>classpath:dababase-config.properties</value>
			<value>classpath:log4j.properties</value>
		</array>
	</property>
	<!-- ignoreResourceNotFound值为true表示允许配置文件不存在 -->
	<property name="ignoreResourceNotFound" value="true" />
</bean>

<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>


<!-- 配置基于spring的jdbc管理,在没有整合maybatis时可以使用 -->
<!-- 配置 Spring 的 org.springframework.jdbc.core.JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>
   
 <!-- spel的引入 -->
<bean id=" " class=" ">
    <!--@Value,#{}内是表达式的值,可放在property或constructor-arg内 -->
    <property name="arg" value="#{表达式}">
</bean>


<!-- id值建议填写你要生成的bean实例的名称,class值填bean的全限定类名 -->
<!-- init-method内填spring容器初始化 后 需运行的方法 -->
<!-- destory-method内填spring容器销毁 前 需运行的方法 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<!-- name值为要注入的属性名,value处填其值 -->
	<property name=" " value=" " />
	
	<!-- ref填要引用的bean全限定类名,也就是当属性存在其它bean引用时用 -->
	<property name=" " ref=" " />
</bean>

<!-- 如果引入p命名空间,以下写法也可,此为setter注入 -->
<bean id=" " class=" " p:name="" p:name-ref=" "/>


<!-- id值建议填写你要生成的bean实例的名称,class值填bean的全限定类名 -->
<bean id=" " class=" ">
	<!-- index为索引值,如不给出会有默认值,但建议按属性顺序给出值 -->
	<!-- name值为要注入的属性名,value处填其值 -->
	<constructor-arg index=" " name=" " value=" " />

	<!-- type可指定构造参数类型,ref填要引用的bean全限定类名 -->
	<constructor-arg index=" " type=" " ref=" " />
</bean>






<!-- 属性为map时 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="map" >
		<map>
			<!-- 键为引用先定义好的bean,值为引用先定义好的bean -->
			<entry key-ref=" " value-ref=" "/>
		</map>
	</property>
</bean>

<!-- 属性为set时 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="set" >
		<set>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</set>
	</property>
</bean>

<!-- 属性为list时 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="list" >
		<list>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</list>
	</property>
</bean>

<!-- 属性为array时 -->
<bean id=" " class=" " scope="prototype" init-method=" " destory-method=" ">
	<property name="array" >
		<array>
			<!-- bean值为引用先定义好的bean -->
			<ref bean=" "/>
		</array>
	</property>
</bean>






<!-- AOP配置 -->

<!-- 配置切点的bean,id为目标对象名称,class为目标对象全限定类名 -->
<bean id="" class=""/>

<!-- 配置切面的bean,id为切面实例名称,class为切面类全限定类名  -->
<bean id="" class="" />

<!-- 系统服务组件的切面Bean -->
<bean id="aspectService" class="com.tera.sys.filter.LogAspect">

<!-- 传统的基于代理的aop配置 创建代理工厂 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 配置目标对象 -->
        <property name="targetName" value=""></property>
        <!-- 配置通知(字符串数组) -->
        <property name="interceptorNames" value="..,.."></property>
        <!-- 配置目标对象实现的接口(Class数组)(配不配都行) -->
        <property name="interfaces" value="..,.."></property>
</bean>


<!--  开启基于AOP切面事务的注解处理器,需要引入aop命名空间,且该声明优于基于XML的声明   -->
<!-- 基于注解开发aop -->
<aop:aspectj-autoproxy />

<!-- 基于xml配置开发aop -->
<!-- proxy-target-class 属性控制是基于接口的还是基于类的代理被创建,true为基于类的代理创建 -->
<aop:config proxy-target-class="true">
	<!-- id为切面类ID,可有可无,一般填类名 ref填要引用的切面实例名称,相当于@Aspect -->
	<!-- order设置优先级,数字越小优先级越高 -->
	<aop:aspect id="" ref="" order="1">

		<!-- 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" -->

		<!-- id为切入点名称,expression为切入点表达式 -->	
		<!--切点不需要根对应的bean相关联,只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
		<!-- 等同于@Point -->
		<!-- pointcut(切入点)可以定义在aop:config下,如定义在aop:aspect元素下意味着只对当前切面有效, -->
		<!-- 基于XNL的aop配置不允许在切入点表达式中用名称引入其它切入点 -->
		<!-- 此处引入切入点既可以使用pointcut-ref引用切点,也可以使用expression引入切入点表达式 -->
		<aop:pointcut id="" expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))||execution (* com.tera.*..*Service.*(..))" />

		<!-- pointcut-ref为切入点名称,method为前置通知的方法名 -->
		<aop:before pointcut-ref="" method="beforeMethod" />

		<!-- pointcut-ref为切入点名称,method为后置通知的方法名 -->
		<aop:after-returning pointcut-ref="" method="afterRunningMethod"/>

		<!-- pointcut-ref为切入点名称,method为异常通知的方法名 -->
		<aop:after-throwing pointcut-ref="" method="throwingMethod" />

		<!-- pointcut-ref为切入点名称,method为最终通知的方法名 -->
		<aop:after pointcut-ref="" method="afterMethod" />

		<!-- pointcut-ref为切入点名称,method为环绕通知的方法名 -->
		<aop:around pointcut-ref="" method="" />
	</aop:aspect>
</aop:config>
  


<!-- 配置事务管理器 -->
<!-- id为事务管理器实例名称,class为事务管理器类全限定类名 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
	<!-- name为数据源属性名称,ref为要引用的数据源实例名称 -->
	<property name="" ref="" />
</bean>






<!-- 基于xml配置事务通知 -->
<tx:advice id="" transaction-manager="">
	<tx:attributes>
		<!-- name为需执行配置的事务通知的方法,isolation为隔离方式,propagation为传播行为,rollback-for为异常回滚 -->
		<!-- 以get开头的方法被设置为不支持事务,其它方法以默认事务进行 -->
			<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
			
			<!-- read-only为true意味着在只读事务里开启该类方法 -->
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" isolation="default" read-only="true"/>
            
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" rollback-for="Exception" isolation="default"/>
            
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" rollback-for="Exception" isolation="default"/>
	</tx:attributes>
</tx:advice>





<!-- 使用AOP技术实现事务管理 -->
<aop:config>
	<!-- 配置AOP切面 -->
	<!-- id为事务切入点名称,expression事务切入点正则表达式 -->
	<aop:pointcut-ref="" expression="execution(* *..service.*.*(..))" />
	<!-- advice为事务通知名称,pointcut-ref为事务切入点名称-->
	<aop:advisor advice-ref="" pointcut-ref="" />
</aop:config>

<!-- 开启基于注解的事务配置 -->
<!-- 方法上配置 Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollBackFor=FundException.class) -->
<!-- 配置事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


</beans>
发布了7 篇原创文章 · 获赞 1 · 访问量 129

猜你喜欢

转载自blog.csdn.net/weixin_43694216/article/details/104349036