Spring

Spring

  • Spring 是一个开源的,控制反转( IOC )的,面象切面( AOP )的容器框架.

使用Spring 的好处

  • 降低组件之间的耦合度,实现软件各层之间的解耦
  • 可以使用容器提供的众多服务,如:事务管理服务、消息服务等等。当我们使用容器管理事务时,开发人员就不再需要手工控制事务.也不需处理复杂的事务传播
  • 容器支持单例模式,开发人员不再需要自己编写实现代码
  • 容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能
  • 容器提供的众多辅作类,使用这些类能够加快应用的开发,如: JdbcTemplate、 HibernateTemplate
  • Spring对于主流的应用框架提供了集成支持,如:集成 Hibernate、JPA、Struts 等,这样更便于应用的开发

Spring 负责管理容器中所有的组件, 这些组件统称为 Bean , 在Spring 的概念里,一切都是Bean,其实Spring就是面向Bean的编程(BOP,Bean Oriented Programming )

spring 中的重要概念

  1. IOC (Inversion of control) 控制反转

    ​ 应用本身不负责依赖对象的创建,而是把它们的创建控制权交给外容器, 这样,对象创建的控制权就从应用转到了外部容器,控制权的转移,称为控制反转

  2. DI ( Dependency Injection )依赖注入

    ​ 在程序运行期, 由外部容器,将依赖对象传给应用的过程

  3. AOP ( Aspect Oriented Programming) 面象切面编程

    ​ AOP是OOP的延续,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

四种注入bean的方式

  1. setter方法注入
  2. 构造方法注入
  3. 静态工厂注入
  4. 实例工厂注入

bean的作用域

bean 的 生命周期

AOP 编程

Spring切面可以应用5种类型的通知:

  1. 前置通知(Before):在目标方法被调用之前调用通知功能;
  2. 后置通知(After):在目标方法完成之后调用通知,此时不会关心方法的输出是什么;
  3. 返回通知(After-returning):在目标方法成功执行之后调用通知;
  4. 异常通知(After-throwing):在目标方法抛出异常后调用通知;
  5. 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行
    自定义的行为。
@Component @Aspect
public class MyAspect {
	//例 得到方法相关的信息
	@Before("anyMethod()")
	public void beforeMethod() {
		System.out.println("前置通知触发了");
	}
	
	@AfterReturning("anyMethod()")
	public void finallyMethod() {
		System.out.println("最终通知触发了");
	}

	@AfterThrowing("anyMethod()")
	public void exceptionMethod() {
		System.out.println("例外通知触发了");
	}
	@After("anyMethod()")
	public void afterMethod() {
		System.out.println("后置通知触发了");
	}
	
	@Pointcut("execution(* com.dao.impl.*.*(..))")
	private void anyMethod() {}
	
}

spring进行事务管理

在Spring中,进行事务管理,主要有两种方式

  1. 注解方式
  2. xml配置的方式

注解方式

步骤一、在spring配置文件中引入tx:命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

步骤二、具有@Transactional 注解的bean自动配置为声明式事务支持

<context:component-scan base-package="com"  />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
	   <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
	   <property name="url" value="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false"/>
	   <property name="username" value="root"/>
	   <property name="password" value="root"/>	 
</bean>

<!-- 配置事务管理器 -->
<bean  name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
</bean>

<!-- 要声明使用注解处理事务 -->
<tx:annotation-driven   transaction-manager="txManager" />

步骤三、在接口或类的声明处 ,写一个@Transactional

这个注解可以加在方法上,或加在类上,如果加在类上,则表示类中的所有方法都开启这样的事务管理

//当标于类前时, 标示类中所有方法都进行事物处理
@Transactional
public class TestServiceBean implements TestService {}
//当类中某些方法不需要事物时:
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<Object> getAll() {
        return null;
    } 

事物传播行为介绍

@Transactional(propagation=Propagation.REQUIRED) (required 请求)
如果有事务, 那么加入事务, 没有的话新建一个(默认情况下)
@Transactional(propagation=Propagation.NOT_SUPPORTED) (supported 支持的)
容器不为这个方法开启事务
@Transactional(propagation=Propagation.REQUIRES_NEW) (requires_new)
不管是否存在事务,都创建一个新的事务,原来的挂起,新的执行完毕,继续执行老的事务
@Transactional(propagation=Propagation.MANDATORY) //(mandatory,强制的,命令的,受委托的)
必须在一个已有的事务中执行,否则抛出异常
@Transactional(propagation=Propagation.NEVER)
必须在一个没有的事务中执行,否则抛出异常(与Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.SUPPORTS)
如果其他bean调用这个方法,在其他bean中声明事务,那就用事务.如果其他bean没有声明事务,那就不用事务.

XML方式

<context:component-scan base-package="com"  />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
	   <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
	   <property name="url" value="jdbc:mysql://localhost:3306/shop?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false"/>
	   <property name="username" value="root"/>
	   <property name="password" value="root"/>	 
</bean>

<!-- 配置事务管理器 -->
<bean  name="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
</bean>

<aop:config>
	<aop:pointcut expression="execution(* com.dao.UserDaoImpl.*(..))" id="txPointCut"/>
	<aop:advisor advice-ref="txAdvice"  pointcut-ref="txPointCut" />	
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
	<tx:attributes>
		<tx:method name="get*" read-only="true"  propagation="NOT_SUPPORTED" />
		<tx:method name="*"/>
	</tx:attributes>
</tx:advice>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Abner-G-Zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值