Spring学习笔记

spring

  • 命名空间:file:///Users/TT/Documents/CS/doc/spring-framework-5.0.2.RELEASE-dist/spring-framework-5.0.2.RELEASE/docs/spring-framework-reference/index.html,

注解:进入core页面搜索xmlns:context
aop:进入core页面搜索xmlns:aop
事务:进入data access页面搜索xmlns:tx

IOC

  • 谈谈对ioc的理解

Inversion of Control,即“控制反转”,
控制:程序员new创建对象—>容器创建对象
反转:程序员在对象中主动控制去获取依赖对象—>容器帮忙创建及注入依赖对象

2020/2/13

  • springframework的约束文件
    file:///Users/TT/Documents/CS/%E5%AE%98%E6%96%B9%E5%AD%A6%E4%B9%A0%E6%96%87%E6%A1%A3/spring-framework-5.0.2.RELEASE-dist/spring-framework-5.0.2.RELEASE/docs/spring-framework-reference/core.html#spring-core
    搜索xmlns把拿一坨copy进xml

  • //1.获取核心容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext(“bean.xml”);

  • 注解方式实现ioc,去spring文档中core中搜索xmlns:context把命名空间导入,<context:component-scan basepackage=“包名”/>告诉spring创建容器时要扫描哪些包

  • 注解类型

    1. 创建对象:@Component
    2. 注入数据:@Autowired+@Qualifier(“beanId”)=@Resource(name=“beanId”),@Value("${jdbc.driver}")spring的el表达式注入基本类型或String
    3. 改变作用范围:@Scope
    4. 生命周期:@PreDestory:destory-method@PostConstruct:init-method
    5. 配置类:@Configuration
    6. @ComponentScan(“包名”)相当于<context:component-scan basepackage=“包名”/>指定用注解注入
    7. @Bean(name=“beanId”)当前方法的返回值作为Bean注入spring容器
    8. @Import用于导入其他配置类
    9. @PropertySource(“classpath:org/example/jdbcconfig.properties”)读取.properties文件
    10. @RunWith(SpringJunit4ClassRunner.class)用spring-test中的运行器替代JunitTest中的main方法运行器
    11. @ContextConfiguration(locations = “classpath:bean.xml”)或者@ContextConfiguration(classes=SpringConfiguration.class)告诉spring测试运行器,springIOC配置是基于哪个xml还是基于哪个注解的

2020/2/17

aop

  • 动态代理

    1. Proxy.newProxyInstance其中有个匿名内部类参数new InvocationHandler(){…}
    2. cglib:Enhancer.create其中有个匿名内部类参数new MethodInterceptor(){…}
  • 解决的问题

    1. service层事务开启/回滚/提交/关闭,设置全局编码
  • 配置文件bean.xml
    打开spring文档core页面搜xmlns:aop
    配置spring开启注解AOP的支持:aop:aspectj-autoproxy</aop:aspectj-autoproxy>

  • AOP配置术语(为了看懂doc和报错信息)

标签解释描述
JoinPoint连接点被spring拦截到的方法
PointCut切点要对哪些 Joinpoint 进行拦截的定义
Advice通知拦截到 Joinpoint 之后所要做的事情就是通知
前置通知开启事务
后置通知提交事务
异常通知catch块中的回滚事务
最终通知finally中的释放资源
Introduction引介一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
Target目标代理的目标对象
Weaving织入把增强应用到目标对象来创建新的代理对象的过程;spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。
Proxy代理一个类被AOP织入时,就产生一个结果代理类
Aspect切面是切入点和引介的结合

事务控制

  • AOP注解类型

    1. @Aspect表明当前类中方法是连接点
    2. @Pointcut(“execution(* wt…*.transfer(…))”)加在方法上表明切入点
    3. @EnableAspectJAutoProxy加在配置类上表明支持注解实现aop
    4. @Around环绕通知的方法参数:ProceddingJoinPoint
  • 准备工作

    1. 额外pom.xml依赖aspectjweaver和spring-tx包
    2. 配置bean.xml导aop和tx约束

声明式事务控制

  • 基于xml的声明式事务控制

    1. 拷xmlns:
      bean.xml中的命名空间等:在spring文档中的Data access页面搜索xmlns:tx
    2. 继承JdbcDaoSupport
      dao层实现类继承 JdbcDaoSupport,并注入springframework包的DriverManagerDataSource,以从中获取JdbcTemplate执行query或update,参数为BeanPropertyRowMapper
    3. 注入TransactionManager
      bean.xml中配置事务管理器在其中注入springframework包中的DriverManagerDataSource
    4. tx:advice
      配置事务通知<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
    5. aop:advicor
      配置aop的切入点表达式和事务通知的联系
  • 基于注解的声明式事务控制

    1. <tx:annotation-driven />
      开启spring对注解事务的支持
    2. @Transactional
      加在需要事务的类或方法上
    3. @EnableTransactionManagement
      开启spring注解事务的支持
    4. 配置入口类
    @Configuration
    @ComponentScan("wt")
    @Import({JdbcConfig.class,TransactionManagerConfig.class})
    @PropertySource("classpath:jdbcConfig.properties")
    @EnableTransactionManagement
    public class SpringConfiguration {
    }
    
    1. 配置Spring测试运行器
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(classes = SpringConfiguration.class)

编程式事务控制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值