<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
>
<!--
context:component-scan:指定扫描x包下所有的类中的注解
base-package:包名
注意:扫描包时,会扫描指定包下的所有子孙包
-->
<context:component-scan base-package="review.struts"></context:component-scan>
<!--
context:property-placeholder:指定spring读取外部文件
location:外部文件所在路径
-->
<!-- <context:property-placeholder location=""/> -->
<!-- 配置数据连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean name="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
bean:使用该元素描述需要spring容器管理的对象
Class:被管理对象的完整类名
name:给被管理的对象起个名字,获取对象时根据名称获取对象
可以重复 ,可以使用特殊字符
id: 与name属性一模一样 ,
名称不重复 ,不能使用特殊字符
结论:经量使用name属性
scope:
singleton:(默认值):单例对象,被标识为单例的对象,在Spring容器中只会存在一个实例
prototype: 多例原型,被标识为多例的对象,每次在获得才会创建,每次创建都是新的对象
整合Sturts2 时,ActionBean必须配置为多例的
request:web环境下,对象与request生命周期一致
session:web环境下,对象与session生命周期一致
生命周期属性:
init-method:配置一个方法做为生命周期初始化方法,Spring会在创建对象之后立即调用
destroy-method:配置一个方法做为生命周期销毁方法,Spring容器会在关闭并销毁所有的容器中对象之前调用
空参创建
factory-method:创建该对象的工厂方法
p:xxx 名称空间注入
-->
<bean name="user" class="review.struts.User" scope="prototype" init-method="init"
destroy-method="destroy" p:sex="0">
<!--
property:set方法注入/属性注入
name:属性名
value:基础数据类型注入
ref:引用数据类型注入
Sple表达式注入(了解)
-->
<property name="name" value="DL"></property>
<property name="user" ref="user1"></property>
<property name="age" value="#{user1.age}"></property>
<property name="arr">
<!-- array:数组 -->
<array>
<!-- value:数组值 -->
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
<value>5</value>
<value>6</value>
</array>
</property>
<property name="list">
<!-- list:list集合 -->
<list>
<value>use</value>
<value>use1</value>
<value>use2</value>
<!--ref 引用 实体对象
bean:要引用的bean名 -->
<ref bean="user1"/>
</list>
</property>
<property name="map">
<!-- map:map集合 -->
<map>
<!--
entry:元素
key:键(基础)
key-ref:键(引用)
value:键(基础)
value-ref:键(引用)
value-type:值的类型
-->
<entry key="a" value="1" value-type="int"></entry>
<entry key="b" value="2"></entry>
<entry key-ref="user1" value="2"></entry>
<entry key-ref="user1" value-ref="user1"></entry>
</map>
</property>
<property name="properties">
<!-- props:属性 -->
<props>
<!--
key:键
-->
<prop key="a">1</prop>
<prop key="b">1</prop>
<prop key="c">1</prop>
<prop key="d">1</prop>
</props>
</property>
<!--
constructor-arg:构造方法注入
name:构造参数名
value:基础数据类型注入
ref:引用数据类型注入
index:构造参数索引 0起
type:构造参数类型
-->
<constructor-arg name="name" value="a" type="String"></constructor-arg>
<constructor-arg name="age" value="14" index="2"></constructor-arg>
<constructor-arg name="sex" value="0" ></constructor-arg>
</bean>
<bean name="user1" class="review.struts.User">
<property name="age" value="12"></property>
</bean>
<bean name="userF" class="review.struts.Factroy" factory-method="create"></bean>
<!-- 配置核心事务过滤器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
tx:advice:配置事务通知
id:
transaction-manager:关联核心事务管理器
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--
tx:attributes:事务属性
-->
<tx:attributes>
<!--
tx:method:已方法为单位,指定方法应用那个事务属性
name:关联匹配方法
Propagation :传播行为
Propagationkey属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。
有以下选项可供使用:REQUIRED-支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
SUPPORTS-支持当前事务,如果当前没有事务,就以非事务方式执行。
MANDATORY-支持当前事务,如果当前没有事务,就抛出异常。
REQUIRES_NEW-新建事务,如果当前存在事务,把当前事务挂起。
NOT_SUPPORTED-以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
NEVER-以非事务方式执行,如果当前存在事务,则抛出异常。
isolation:隔离级别
1. DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。另外四个与JDBC的隔离级别相对应
2. READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
3. READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据
4. REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
5. SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读。
read-only:是否只读
-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* review.struts.service.*ServiceImpl.*(..))" id="txPc"/>
<!--
aop:advisor:配置通知
advice-ref:添加通知引用
pointcut-ref:配置切点
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>
<!-- 配置aop -->
<!-- 配置通知对象 -->
<bean name="userAdvice" class="review.struts.advice.UserAdvice"></bean>
<!-- 配置目标 -->
<bean name="userService" class="review.struts.service.UserServiceImpl"></bean>
<!-- 开启使用注解完成织入 -->
<!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
<!--
aop:config:aop配置
-->
<!-- <aop:config>
aop:pointcut:aop切入点
expression:表达式
<aop:pointcut expression="execution(* review.struts.service.*ServiceImpl.save())" id="pc"/>
aop:aspect:配置切面
ref:引用通知对象
<aop:aspect ref="userAdvice">
aop:before:配置前置通知
method:前置通知方法
pointcut-ref:引用切入点
<aop:before method="before" pointcut-ref="pc"/>
aop:after-returning:后置
该方法遇到异常不执行
<aop:after-returning method="afterRun" pointcut-ref="pc"/>
aop:around:环绕
<aop:around method="around" pointcut-ref="pc"/>
aop:after-throwing:异常拦截通知
<aop:after-throwing method="exception" pointcut-ref="pc"/>
aop:after:后置通知(异常执行)
<aop:after method="after" pointcut-ref="pc"/>
</aop:aspect>
</aop:config> -->
</beans>
spring简单解析
最新推荐文章于 2022-03-13 18:24:32 发布