Spring总结2

spring总结2:

1,aop之aspectj,各种概念介绍

*a.连接点(joinpoint):目标类中的所有方法
*b.切入点(pointcut):从所有目标类所有方法中找到感兴趣的方法,这些方法都成为切入点  
    execution(表达式) 指示器中写表达式来寻找感兴趣方法
    表达式完整格式:权限? 返回类型 类.方法(方法参数)   注意这里的权限可有可无
    * *.*(..) 找所有类所有方法  (第一个*代表返回类型)
    * *(..) 找所有类所有方法
    * cn.itcast.User.*(..)  找User下面的所有方法
    * cn.itcast..*.*(..) 找itcast包下所有子包中的所有方法 
*c.切面(aspect): 增强类
*d.通知(advice): 增强类中定义的方法,这些方法用于增强目标方法。
*e.目标类(target): 要被增强的类
*f.代理类(proxy): 使用动态代理产生目标类的代理
 g.织入(weaving): 将通知方法加到目标方法中的过程  spring aop整个过程就是织入
 h.引入(introduction): 在目标类引入新的属性或者新的方法  了解一下就行

2,aop各种通知

 1. 前置通知 before   目标方法被调用之前,就执行该前置通知方法
 2. 后置通知 after    目标方法被调用完之后,不关心返回结果,就执行该后置通知方法。也叫最终通知
 3. 返回通知 after-returning  目标方法return返回之后,就执行该返回通知方法
 4. 环绕通知 around   包裹了目标方法,在目标方法之前和在目标方法之后整个过程,经常使用ProceedJoinPoint.proceed()来执行目标方法
 5. 异常通知 after-throwing  当目标方法在执行异常的时候,就会执行该异常通知方法

3,通过xml实现spring aop

spring基于aspectj实现aop编程

 1. xml开发:
   <aop:config>
        <aop:pointcut></aop:pointcut> //切入点  @Pointcut
        <aop:aspect> //切面   @Aspect
            <aop:after></aop:after>  //后置通知  @After
            <aop:before></aop:before>//前置通知
            <aop:around></aop:around>//环绕通知
            <aop:after-throwing></aop:after-throwing> //异常通知
            <aop:after-returning></aop:after-returning>//返回通知
        </aop:aspect>
   </aop:config>
 2. 注解开发:
   @Aspect+@PointCut注解可以放在一个空的方法上+@Before(引入@Pointcut注解所在方法)
   <aop:aspectj-autoproxy 手动方式来设置动态代理的/>

spring中JdbcTemplate

1,spring JdbcTemplate入门案例

实现的步骤:
1. 导入spring-jdbc jar
2. 创建DriverManagerDataSource(该数据源是来自于spring框架)并且该数据源赋值(驱动+url+账号+密码)
3. 创建JdbcTemplate,然后注入dataSource属性
jdbcTemplate.execute(sql)

2,DriverManagerDataSource和JdbcTemplate对象交给spring管理

通过applicationContext.xml <bean>和<property>标签来完成实例化和初始化

3,将连接数据库的信息放入到properties文件

需要在spring 配置文件加载该properties文件
<context:property-placeholder location="classpath:applicationContext.xml"/>

4,spring JdbcTemplate使用

1.获取JdbcTeplate:
   *JdbcDaoSupport 抽象,我们自己写的dao类可以继承,可以直接获取JdbcTemplate
   *将JdbcTemplate交给spring容器去管理  需要注入DataSource
2.使用JdbcTemplate:
   常用的一些方法:execute(所有的sql都可以),update(insert/delete/update),queryForObject()
3.JdbcTemplate查询封装
   普通类型+实体类型+List集合的封装
   RowMapper 该接口是将每一行记录手动方式来获取查询的列的值然后封装到实体对象中。
   该接口的实现类:BeanPropertyRowMapper  封装成实体

spring的事务管理

1,spring整合其他持久层事务

平台事务管理器 PlatformTransactionManager
如果使用jdbc编程:DataSourceTransactionManager
如果使用Hibernate框架:HibernateTransactionManager"

2,spring事务的属性定义

* 隔离:定义事务的隔离,避免事务脏读、重复读、幻读现象出现
* 超时:连接数据库超时,timeout=-1 说明取决于使用的数据库
* 只读:事务设置成只读事务,不能写操作
* 传播:将2个操作划分几个事务

3,spring管理事务2种方式(声明式事务管理+基于注解的事务管理)

1. 声明式事务管理  
   *配置事务管理器(PlatformTransactionManager)  增强类
        <bean id="transactionManager"
             class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <property name="dataSource" ref="c3p0DataSource"></property>
        </bean>
        注意:该事务管理器需要注入datasource.
   *给事务管理器设置属性
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save*"  propagation="REQUIRED"/>  propagation="REQUIRED" 多个操作放在一个事务中
                <tx:method name="insert*"  />
                <tx:method name="update*"  />
                <tx:method name="delete*"  />
                <tx:method name="query*" read-only="true" />
            </tx:attributes>
       </tx:advice>
   *通过配置aop来给目标类中的目标方法加事务
       <aop:config>
             <aop:pointcut >
             <aop:advisor advice-ref="txAdvice" pointcut-ref="">
       </aop:config>
  注意:一般在开发中,我们会在service层中加事务。
2. 注解开关事务管理
   * 开关:<tx:annotation-driven transaction-manager="transactionManager"/>
   * 注解:@Transactional

applicationContext.xml中标签使用

     <import resource=""/> 引入到唯一的一个配置文件中
     分模块开发配置,这样好处就是分门别类,减少耦合性,如下面的配置文件
     applicationContext-jdbc.xml   定义jdbc连接信息
     applicationContext-beans.xml  定义申明的bean
     applicationContext-redis.xml  定义声明redis连接信息

猜你喜欢

转载自blog.csdn.net/weixin_42425532/article/details/81003588
今日推荐