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创建容器时要扫描哪些包
-
注解类型
- 创建对象:@Component
- 注入数据:@Autowired+@Qualifier(“beanId”)=@Resource(name=“beanId”),@Value("${jdbc.driver}")spring的el表达式注入基本类型或String
- 改变作用范围:@Scope
- 生命周期:@PreDestory:destory-method@PostConstruct:init-method
- 配置类:@Configuration
- @ComponentScan(“包名”)相当于<context:component-scan basepackage=“包名”/>指定用注解注入
- @Bean(name=“beanId”)当前方法的返回值作为Bean注入spring容器
- @Import用于导入其他配置类
- @PropertySource(“classpath:org/example/jdbcconfig.properties”)读取.properties文件
- @RunWith(SpringJunit4ClassRunner.class)用spring-test中的运行器替代JunitTest中的main方法运行器
- @ContextConfiguration(locations = “classpath:bean.xml”)或者@ContextConfiguration(classes=SpringConfiguration.class)告诉spring测试运行器,springIOC配置是基于哪个xml还是基于哪个注解的
2020/2/17
aop
-
动态代理
- Proxy.newProxyInstance其中有个匿名内部类参数new InvocationHandler(){…}
- cglib:Enhancer.create其中有个匿名内部类参数new MethodInterceptor(){…}
-
解决的问题
- 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注解类型
- @Aspect表明当前类中方法是连接点
- @Pointcut(“execution(* wt…*.transfer(…))”)加在方法上表明切入点
- @EnableAspectJAutoProxy加在配置类上表明支持注解实现aop
- @Around环绕通知的方法参数:ProceddingJoinPoint
-
准备工作
- 额外pom.xml依赖aspectjweaver和spring-tx包
- 配置bean.xml导aop和tx约束
声明式事务控制
-
基于xml的声明式事务控制
- 拷xmlns:
bean.xml中的命名空间等:在spring文档中的Data access页面搜索xmlns:tx - 继承JdbcDaoSupport
dao层实现类继承 JdbcDaoSupport,并注入springframework包的DriverManagerDataSource,以从中获取JdbcTemplate执行query或update,参数为BeanPropertyRowMapper - 注入TransactionManager
bean.xml中配置事务管理器在其中注入springframework包中的DriverManagerDataSource - tx:advice
配置事务通知<tx:advice id=“txAdvice” transaction-manager=“transactionManager”> - aop:advicor
配置aop的切入点表达式和事务通知的联系
- 拷xmlns:
-
基于注解的声明式事务控制
- <tx:annotation-driven />
开启spring对注解事务的支持 - @Transactional
加在需要事务的类或方法上 - @EnableTransactionManagement
开启spring注解事务的支持 - 配置入口类
@Configuration @ComponentScan("wt") @Import({JdbcConfig.class,TransactionManagerConfig.class}) @PropertySource("classpath:jdbcConfig.properties") @EnableTransactionManagement public class SpringConfiguration { }
- 配置Spring测试运行器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
- <tx:annotation-driven />
编程式事务控制
略