spring详解

spring

spring 对象工厂容器。通过spring工厂构建对象,解耦合,从而提高程序的可维护性和扩展性
spring工厂构建对象:读取配置文件 中的信息,获取class属性配置的全类名,通过反射,默认调用无参构造
spring工厂构建对象默认为单例,节省空间
注入

  • set注入
    通过无参构造方法构建对象后,调用set方法赋值简单类型

简单类型

<bean id="service" class="service.Userservice" scope="prototype">
	<property name="userMapper" value="userMapper">
</bean>
	

自定义类型

 <bean id="addr" class="com.baizhi.entity.Address" scope="prototype">
     <property name="city" value="郑州"/>
     <property name="street" value="文化路"/>
</bean>

<bean id="p" class="com.baizhi.entity.Person">
    <property name="id" value="1"/>
    <property name="name" value="xiaohei"/>
    <property name="address" ref="addr"/>
</bean>

数组、list、set类型

<property name="list">
    <list>
        <value>1</value>
        <value>xiaohei</value>
        <value>true</value>
        <value>1</value>
        <ref bean="addr"/>
    </list>
    ....
</property>

map类型

<property name="map">
    <map>
        <!-- 一个元素,是一个键值对-->
        <entry key="name" value="xiaohei"></entry>
        <entry key="address" value-ref="addr"></entry>
      <!--<entry key-ref="bean的id" value-ref="bean的id"/>-->
    </map>
</property>

Properties类型

<property name="properties">
    <props>
        <!-- 每一个键值对都是String -->
        <prop key="name">xiaohei</prop>
        <prop key="age">18</prop>
    </props>
</property>
  • 构造注入
    调用有参构造对象时,为属性赋值
public User(Integer id, String name) {
    System.out.println("id = [" + id + "], name = [" + name + "]");
    this.id = id;
    this.name = name;
}

public User(String name,Integer id) {
    System.out.println("name = [" + name + "], id = [" + id + "]");
    this.id = id;
    this.name = name;
}
<bean id="u2" class="com.baizhi.entity.User" scope="prototype">
    <constructor-arg value="1" type="java.lang.Integer" index="0"/>
    <constructor-arg value="2" type="java.lang.String" index="1"/>
</bean>

FactoryBean技术(创建复杂对象)

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="druidDataSource"/>
        <!--
                配置实体类的包名,自动为实体配置短类名的别名
             -->
        <property name="typeAliasesPackage" value="entity"/>
        <property name="mapperLocations">
            <!-- 配置mapper.xml的路径-->
            <list>
                <!--<value>classpath:com/baizhi/mapper/UserMapper.xml</value>
                    <value>classpath:com/baizhi/mapper/StudentMapper.xml</value>
                    <value>classpath:com/baizhi/mapper/BookMapper.xml</value>-->
                <value>classpath:mapper/*Mapper.xml</value>
            </list>
        </property>
    </bean>

Spring的IOC和DI
IOC 控制反转
DI 依赖注入

代码在运行时,属性需要什么对象,控制权反转到配置文件中,由DI为属性赋值,完成IOC

代理设计模式
代理类代理service的原始方法(调用),同时提供额外的功能。 好处在于:避免原始类因为额外功能反复修改
代理类和原始类要实现相同的接口
代理类提供额外的功能
代理类调用目标方法
调用者只和代理类打交道,不和原始类建立联系
Spring动态代理

切入表达式
execution表达式

execution(* service.*.*(..))

返回值类型 *
包名 service
类名 *
方法名 *
形参列表 (…)
springAOP 面向切面编程
配置原始对象
定义增强类
配置增强类
定义切入点
组装切面

    <!-- 配置增强类:事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"/>
    </bean>
    <tx:advice transaction-manager="txManager" id="txAdvice">
        <tx:attributes>
            <!-- show开头的方法 开启只读事务-->
            <tx:method name="show*" read-only="true"/>
            <!-- 其它的方法,都必须开启事务 -->
            <tx:method name="*" propagation="REQUIRED" timeout="10000" isolation="READ_COMMITTED" />
        </tx:attributes>
    </tx:advice>

    <!--
            定义切入点
            编织组装
         -->
    <aop:config>
        <aop:pointcut id="servicePointCut" expression="execution(* service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut"/>
    </aop:config>

Spring 注解开发
@Service
@Autowired

<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="druidDataSource"/>
</bean>

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

@Transactional
@Transactional(readOnly=trure)

事务详解
read-only=“true” 表示只读,提高查询速率 默认值为false
timeout 超时机制 秒 事务执行时间超过指定时长后 自动回滚

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="show*" read-only="true"/>
        <!-- 超时时间时间设置为3s-->
        <tx:method name="*" timeout="3"/>
    </tx:attributes>
</tx:advice>

rollback-for和no-rollback-for
rollback-for:当发生rollback-for配置的异常类型的异常时,执行回滚。
no-rollback-for:当发生no-rollback-for配置的异常类型的异常时,不执行回滚。

Spring中默认:

RuntimeException及其子类(运行时异常)的异常执行回滚

Exception及其子类(编译时异常)的异常不会执行回滚

隔离级别

出现的问题
脏读:一个用户读到另一个用户未提交的数据
不可重复的:在同一个事务中,不同时间读到的数据不一致
幻读:一个事务按相同的查询条件重新读取以前检索过的数据,却发现其他事务插入了满足其查询条件的新数据

read-uncommitted 读未提交
read-committd 读已提交
repeatable-read 可重复读
serialzable 序列化读

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值