四、BeanFactory和ApplicationContext
BeanFactory:顶级接口,使用对象时才创建
ApplicationContext:子接口,加载配置文件时就创建对象
五、spring bean五种作用域(bean放哪了)
singleton:单例,默认,ioc容器
prototype:多例,每次使用newInstance()
request:request域
session:session域
application:application域
单例和多例的使用场景?
单例:dao、service、sqlSessionFactory
多例:connection、sqlSession
六、spring bean的生命周期【重点】
单例:[容器启动]--->构造方法(实例化)--->set方法(注入)--->init方法(初始化)--->[关闭容器]--->destroy方法(销毁bean)
多例:[使用对象]--->构造方法(实例化)--->set方法(注入)--->init方法(初始化)--->[JVM垃圾回收]--->destroy方法(销毁bean)
七、spring基于注解的IOC
1、步骤
1)pom.xml
spring-context
2)applicationContext.xml
<context:component-scan base-package="com.jy"></context:component-scan>
3)IOC
@Repository
public class UserDaoImpl implements UserDao {
}
@Service
public class UserServiceImpl implements UserService {
}
4)DI
@Autowired
private UserDao userDao;
2、常用注解:
1)IOC
@Controller:web
@Service:service
@Repository:dao
@Component:三层架构之外
2)DI
@AutoWired:按类型
@Resources(name=""):按名称注入
@Value("${key}"):注入基本类型
八、AOP
1、为什么要使用AOP?
有一万个service,要求每个方法执行前打印日志,执行后打印日志,eg:
public class Service{
private Dao dao;
public void method(){
System.out.println("方法开始时间:"+new Date());
dao.method();
System.out.println("方法结束时间:"+new Date());
}
}
2、什么是AOP?
AOP(Aspect Oriented Programming)面向切面:把程序中重复的代码抽取出来,使用动态代理的技术去执行,从而实现
不修改源代码对方法进行增强
3、代理模式
1)静态代理
公共接口:Star
真实角色:周杰伦.唱歌
代理角色:宋吉吉.面谈.签合同.订机票[周杰伦.唱歌].收尾款
测试类:Client
缺点:①大量重复代码 ②只能代理Star的子类
2)动态代理
①jdk动态代理
公共接口:Star
真实角色:周杰伦.唱歌
代理工厂:宋吉吉.面谈.签合同.订机票[周杰伦.唱歌].收尾款
测试类:Client
关系:
Star
| |
| |
ProxyStar RealStar
②cglib动态代理
cglib和jdk动态代理的区别:cglib没有接口
关系:
RealStar
|
|
ProxyStar
4、aop的核心概念
切点(pointcut):要增强的方法,eg:add()、update()
切点表达式:
语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
eg:execution(* com.jy.service.*.*(..))
通知/增强(advice):方法执行前/后要搞的事情,eg:日志、控制事务
前置通知:aop:before
后置通知:aop:after-returning,【try】
环绕通知:aop:around
异常通知:aop:after-throwing【catch】
最终通知:aop:after【finally】
try{
.....
aop:after-returning
}catch(Exception e){
aop:after-throwing
}finally{
aop:after
}
切面(aspect):把增强应用到切点上
5、基于xml的aop配置
1)pom.xml
spring-context、spring-aspects
2)advice
public class LogAdvice {
public void before(){
System.out.println("前置通知");
}
}
3)applicationContext.xml
<!--增强:要搞的事情,eg:log-->
<bean id="logAdvice" class="com.jy.advice.LogAdvice"></bean>
<aop:config>
<!--切点:要增强的方法,eg:update()、delete()-->
<aop:pointcut id="pointcut" expression="execution(* com.jy.service.*.*(..))"/>
<!--切面:把增强应用到切点上-->
<aop:aspect ref="logAdvice">
<aop:before method="before" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
九、spring基于注解的AOP配置
1)pom.xml
spring-context、spring-aspects
2)advice
@Aspect
@Component
public class LogAdvice {
@Before("execution(* com.jy.service.*.*(..))")//切点:要增强的方法
public void before(){
System.out.println("前置通知");//增强
}
}
3)applicationContext.xml
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>//开启aop注解的扫描
十、spring整合junit
1、pom.xml
junit、spring-test
2、junit
@RunWith(SpringJUnit4ClassRunner.class)//运行时带上spring的环境
@ContextConfiguration("classpath:applicationContext.xml")//加载spring的配置文件
public class SpringMyBatisTest{
... ...
}
十一、事务介绍
1、什么是事务?
执行多条sql,要么全部执行,要么全部回滚
2、事务的特点
原子性:事务是最小的执行单位
一致性:事务前后的数据都是正确的
隔离性:事务之间看不到彼此的中间状态
持久性:事务一旦提交不可再回滚
3、mysql控制事务
START TRANSACTION; -- 记录log(老数据)
sql1;
sql2;
COMMIT; -- 删除log(老数据)
ROLLBACK; -- 根据log(老数据)回滚数据
4、jdbc
conn.setAutoCommit(false);//START TRANSACTION;
try{
... ...
conn.commit();//COMMIT;
}catch(){
conn.rollback();//ROLLBACK;
}
十二、spring事务控制的api
1、PlatformTransactionManager
作用:是一个事务管理器,负责开启、提交和回滚事务
实现类:DataSourceTransactionManager
2、TransactionDefinition
作用:定义事务的属性
实现类:DefaultTransactionDefinition
属性:
1)隔离级别【有默认值】
事务隔离级别 脏读 不可重复读 幻读
READ_UNCOMMITTED y y y
READ_COMMITTED n y y
REPEATABLE_READ n n y
SERIALIZABLE n n n
DEFAULT:默认值,等价于REPEATABLE_READ(mysql的默认隔离级别)
2)传播行为【有默认值】
REQUIRED:默认值,methodB()会加入到methodA()的事务中
SUPPORTS:支持当前事务,如果没有事务则以非事务的方式运行
3)超时【有默认值】
默认-1:永不超时
4)是否只读【有默认值】
false:默认值,记录log,适用于增删改
true:不记录log(效率快),适用于查询
5)回滚规则
可省略:运行时异常回滚,编译时异常不回滚
3、TransactionStatus
作用:代表一个事务