文章目录
AOP注解
-
@Component 除了@Controller @Service @Repository 除了他们三个其他的类交给spring管理就用这个注解
-
@Aspect 就相当于是
<aop:config>
还需要开启aop注解的支持
<aop:aspectj-autoproxy/>
/**
* 定义切点
*/
@Pointcut(value = "execution(* cn.itsource._01aop_ann.service.I*Service.*(..))")
public void pointcut(){
}
//前置通知
// @Before(value = "pointcut()")
public void begin(){
System.out.println("开启事务");
}
//后置通知
// @After(value = "pointcut()")
public void commit(){
System.out.println("提交事务");
}
//异常通知
// @AfterThrowing(value = "pointcut()",throwing = "t")
public void rollback(Throwable t){
System.out.println("事务回滚"+t.getMessage());
}
/**
* 最终通知
*/
// @AfterReturning(value = "pointcut()")
public void close(){
System.out.println("关闭资源");
}
环绕通知
- @Around(value = “pointcut()”)
Spring创建bean的四种方式
- 方式一:使用无参构造方法创建对象
<bean class="cn.itsource._02create_bean.MyBean"/>
- 方式二:使用静态方法创建对象
<bean class="cn.itsource._02create_bean.CreateCar" factory-method="getCar"/>
- 方式三:使用实例方法创建对象
<bean id="createCar" class="cn.itsource._02create_bean.CreateCar"/>
<bean factory-bean="createCar" factory-method="newCar"/>
- 方式四:根据Factory创建对象
<bean class="cn.itsource._02create_bean.factorybean.MyFactoryBean"/>
SSJ集成
集成步骤
- 导入jar包
- 编写spring的核心配置文件
- jdbc.properties
- dataSource
- EntityManagerFactory
- doain,dao
- 测试
- service
- 事务
- 测试
- contrller
- springmvc的核心配置文件
- 扫描包
- 静态资源放行
- 开启springMvc特有注解的支持
- 配置视图解析器
- web.xml
- 启动spring容器
- 启动springmvc容器
- 解决post提交乱码的问题
readOnly = true:只读
事务传播机制
- propagation = Propagation.SUPPORTS 支持
- propagation = Propagation.REQUIRED 必须
- propagation = Propagation.NEVER 从不
- propagation = Propagation.REQUIRES_NEW 新开
propagation = Propagation.SUPPORTS 支持
用的非常多
A方法调用B方法 在B方法上加了此注解
- 如果在A方法上加的注解是REQUIRED,那么就是B方法加入A方法的事务
- 如果在A方法上也加的是SUPPORTS,那么A和B方法就没有事务
propagation = Propagation.REQUIRED 必须
用的非常多
A方法调用B方法 在A方法上加了此注解
- 那么不管B方法是否有事务 B方法都要加入到A方法的事务中
propagation = Propagation.NEVER 从不
用的不多
决不能有事务 有事务就报错
如果A方法调用B方法 ,如果B方法上面加了此注解
- A方法假的注解为必须有事务 将会报错
- A方法设有事务 效果 正常运行
propagation = Propagation.REQUIRES_NEW 新开
用的一般
A方法调用B方法 如果B方法上面加了此注解 不管A方法最终是否成功,B都要新开一个事务