您的位置:首页 > 编程语言 > Java开发

spring常用配置和解释

2018-02-07 13:54 281 查看
先说一下spring的约束吧:如有错误请评论,谢谢。
beans:最基本的管理配置bean的常用于控制反转,和依赖注入的。
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
context:用于读取配置文件的
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

aop:用于配置切面的,这里稍微介绍一下aop的概念(横向重复,纵向抽取),
而spring的代理对象就能体现出aop(常用于配置事务管理)。

spring的代理对象实现的两种方式:

一种是动态代理:基于接口的,被代理的对象要实现接口。

一种是cjlib:可以不需要接口,直接通过继承就可以实现。

代理对象:切点+通知

切点:要被增强的方法,(可以理解为对数据库增删改的方法需要添加事务)。

通知:就是增强事物的代码块。

事务:事务特性:acid(不解释)。事务并发问题:脏读,不可重复读,幻读。事务的隔离级别:1 读未提交,2 读已提交,4 可重复读,8 串行化

而spring封装了事务管理代码,事物的操作(打开,提交,回滚),隔离级别,是否只读,

以及事物的传播行为(可以理解为service层之间相互调用,到底用谁的事务等等)。

在spring中事务管理最为核心的对象就是TransactionManager对象。

看一下aop的约束:
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

tx : 配置事务通知 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

约束介绍完了,下面是常用配置
hibernate5+c3p0连接池配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 配置LocalSessionFactoryBean,spring提供的用于整合hibernate的工厂bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 注入hibernate相关的属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 注入hibernate的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:com/xxx/xx/xxxx/*.xml</value>
</list>
</property>
</bean>

 </beans>
配置事物的第一种方式:注解的方式

<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

<!-- 组件扫描 -->
<context:component-scan base-package="com.xxx.xx"/>

<!-- 支持spring注解 -->
<context:annotation-config/>

<tx:annotation-driven/>第二种:aop方式
1.先配置通知

   <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
   
<!-- 配置通知 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>

2.配置切点+通知
<aop:config>
<aop:pointcut expression="execution(* com.xxx.service.impl.*ServiceImpl.*(..))" id="txSer"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txSer" />
</aop:config>

在web.xml中配置spring监听器,及上下文
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c3p0 spring aop hibernate5
相关文章推荐