Spring是一款功能强大的的开源框架,知道Java的人也一定不会感到陌生,它的IOC(控制反转,底层是依赖注入)和AOP(面向切面编程)可以很好的处理对象之间的联系,让程序的耦合降到最低,AOP可以在不改变原程序的情况下扩展(插入)功能,它的声明式事务处理,使得程序的健壮性得到提升,开发者可以少些很多的try-catch-finally。
使用步骤:1.导包
<!-- AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
2.写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 扫描控制器组件 -->
<context:component-scan base-package="cn.tedu.note.aop"/>
<!-- 用于支持AOP注解 -->
<aop:aspectj-autoproxy/>
</beans>
3.Java代码:
@Component
@Aspect
public class XXX{
方法...
}
方法注解:
@Before 在业务方法之前调用@AfterReturning 在业务方法正常(没有抛异常)结束之后调用
@AfterThrowing 在业务方法出现以后异常调用
@Arter 无论业务方法是否出现异常,都会调用
@Around 在业务方法周围调用
其中,@Around的使用比较特殊,需要有Object返回值(业务方法返回值)和ProceedingJoinPoint 参数(用来获取方法)
@Around("bean(*Service)")
public Object around(ProceedingJoinPoint joinPoint)throws Throwable{
System.out.println("开始");
//执行业务方法
Signature m=joinPoint.getSignature();
System.out.println(m);//输出方法签名
Object value=joinPoint.proceed();
System.out.println("结束");
return value;//业务方法返回值
}
Spring 利用 AOP 技术实现了 声明式事务处理,我们可以用事务注解来简化事务程序的开发:
1.写配置文件
<!-- 配置Spring JDBC 中的数据源事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
用于支持事务处理注解
</bean><tx:annotation-driven transaction-manager="txManager"/>
2.Java上使用
@Transactional
:
@Transactional
public void aopTest(...) {...}
在业务执行过程中一旦出现异常,整个程序(包括方法里调用别的方法)都会回滚到方法调用之前的状态,只有在业务正常结束之后才会将业务完整提交,而这些回滚和提交的操作都由Spring帮我们完成。