AOP(面向切面编程)
AOP和OOP的比较
OOP语言提供了类与类之间纵向的关系(继承、接口),而AOP补充了横向的关系(比如在不改变目标类中源代码的情况下给com.john.demo.dao包下所有类中以insert和update开头的方法添加事务管理)
AspectJ(切面)
我们将自己需要插入到目标业务逻辑中的代码模块化, 通过AOP使之可以横切多个类的模块,称之为切面。
在Spring AOP配置中切面通常包含三部分:
- 切面模块本身
- 通知
- 切入点
示例:
<!-- 在beans标签中配置命名空间aop -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 目标业务逻辑代码 -->
<bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>
<!-- 切面模块化对象(代表我们要附加到原始业务逻辑中的代码) -->
<bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />
<!-- 示例说明: 将切面calcAspect中的代码插入到calc原始业务代码中 -->
<aop:config>
<!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
<aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
<aop:aspect ref="calcAspect">
<!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
<aop:around method="computeTime" pointcut-ref="all_calc_method" />
</aop:aspect>
</aop:config>
</beans>
在pom.xml文件中添加依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
切入点 (Pointcut)
在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。
| AspectJ 指示器 | 描述 |
|---|---|
| execution () | 用于匹配连接点的执行方法 最常用 |
| args () | 限制连接点的指定参数为指定类型的执行方法 |
| @args () | 限制连接点匹配参数类型由指定注解标注的执行方法 |
| this () | 限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类 |
| target () | 限制连接点匹配特定的执行对象,目标对象是指定的类型 |
| @target () | 限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解 |
| within() | 限制连接点匹配指定类型,比如哪个包下,或哪个类里面 |
| @within() | 限制连接点匹配指定注释所标注的类型(当使用 Spring AOP 时,方法定义在由指定的注解所标注的类里) |
| @annotation | 限制匹配带有指定注释的连接点 |
通知(Advice)
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>
<bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />
<aop:config>
<aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
<aop:aspect ref="calcAspect">
<aop:around method="aroundM" pointcut-ref="all_calc_method" />
<aop:before method="beforeM" pointcut-ref="all_calc_method" />
<aop:after-returning method="afterReturningM" pointcut-ref="all_calc_method" returning="retVal" />
<aop:after-throwing method="afterThrowing" pointcut-ref="all_calc_method" throwing="throwable" />
<aop:after method="afterFinallyM" pointcut-ref="all_calc_method" />
</aop:aspect>
</aop:config>
</beans>
配置logback.xml
<configuration>
<property name="HOME_LOG" value="logs"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<!-- 测试部署时使用如下配置 -->
<!-- 可让每天产生一个日志文件,最多 30 个,更早的删除 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME_LOG}/log-%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>10</maxHistory>
</rollingPolicy>
<!--
RollingFileAppender 一般情况下需要配置两个参数:
RollingPolicy,负责滚动。TriggeringPolicy,决定是否以及何时进行滚动
TimeBasedRollingPolicy比较特殊,它同时继承了RollingPolicy和TriggerPolicy。
-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
%msg%n
</pattern>
</encoder>
<!-- 正式部署时使用此配置 -->
<!--
<file>${app.home}/logs/log.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${app.home}/logs/log.%i.log.zip
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>7</maxIndex>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
%msg%n
</pattern>
</encoder>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
-->
</appender>
<logger name="com.lanou3g.spring" level="DEBUG"/>
<logger name="org.springframework" level="ERROR"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="RollingFile" />
</root>
</configuration>
环绕通知(around)
- 在目标方法执行前、后被通知, 可以获取连接点对象(ProceedingJoinPoint, 该对象可以获取被拦截方法的签名、参数、返回值、包括调用与否)
- 该方法的返回值,即代表了真正业务逻辑代码的返回值
- 可以选择终止或正常执行目标方法
前置通知(before)
在目标方法调用前通知切面, 什么参数也无法获取。也不能终止目标方法执行
后置(返回值)通知(after returning)
只有在目标方法 正常 执行结束后才会通知, 在通知方法中可以获取到方法的返回值
后置(最终)通知 (after)
在目标方法执行结束后通知切面, 什么参数也无法获取。无论目标方法是正常执行结束还是抛出异常终止,都会被通知
异常通知(after throwing)
只有在目标方法 出现异常 才会通知, 在通知方法中可以获取到抛出的异常信息
连接点(JoinPoint)
连接点有很多种,比如方法执行期间(开始执行、执行结束、抛出异常)、字段修饰符、字段值被更改…
在Spring AOP中只支持方法连接点(因为Spring AOP底层是通过动态代理实现的)。
连接点与切入点的关系可以简单理解为: 切入点一定是连接点, 连接点不一定是切入点。
织入(Weaver)
织入的过程其实就是Spring AOP帮我们把切面中的代码织入到目标代码中的过程。
用XML方式启用Spring AOP
添加依赖
首先我们需要先将aspectJ的依赖导入maven项目中
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
使用示例
这里给出一个需要被织入的Java类的示例
public class CalcImpl implements Calc {
/**
* 加法
*/
@Override
public int add(int num1, int num2) {
return num1 + num2;
}
/**
* 减法
*/
@Override
public int subtract(int minuend, int subtrahend) {
return minuend - subtrahend;
}
/**
* 乘法
*/
@Override
public int multiply(int num1, int num2) {
return num1 * num2;
}
/**
* 除法
*/
@Override
public int divide(int dividend, int divisor) {
return dividend / divisor;
}
}
以及切面类
@Slf4j//该注解用于下面使用log输出(不想使用log的换成控制台输出语句即可)
public class CalcAspect {
public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取连接点代表的方法的签名
Signature signature = joinPoint.getSignature();
// 获取目标方法名
String methodName = signature.getName();
// 获取目标方法的参数
Object[] args = joinPoint.getArgs();
log.debug("[aroundM] ---- 目标方法[" + methodName + "(" + Arrays.toString(args) + ")]开始执行");
long start = System.currentTimeMillis();
// 调用目标方法
Object retVal = joinPoint.proceed();
// 插入公共需要的逻辑代码
long timer = System.currentTimeMillis() - start;
log.debug("[aroundM] ---- 目标方法[" + methodName + "(" + Arrays.toString(args) + ")]" +
"执行结束,返回值: " + retVal + ",耗时: " + timer + "ms.");
// 正常返回目标方法的返回值
return retVal;
}
/**
* 前置通知
*/
public void beforeM(){
log.debug("[beforeM] ---- 目标方法开始执行");
}
/**
* 后置(返回值)通知
*/
public void afterReturningM(Object retVal){
log.debug("[afterReturningM] ---- 目标方法执行结束,返回值: " + retVal);
}
/**
* 后置(最终)通知
*/
public void afterFinallyM(){
log.error("[afterFinallyM] ---- 方法执行结束");
}
/**
* 异常通知
*/
public void afterThrowing(Throwable throwable){
log.error("[afterThrowing] ---- 方法执行出错", throwable);
}
}
准备工作做完了,就需要在spring的配置文件中进行配置了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建两个类的bean对象 -->
<!-- 目标业务逻辑代码 -->
<bean id="calc" class="com.spring.demo.CalcImpl"/>
<!-- 切面模块化对象(代表我们要附加到原始业务逻辑中的代码) -->
<bean id="calcAspect" class="com.spring.demo.CalcAspect"/>
<!-- 示例说明: 将切面calcAspect中的代码插入到calc原始业务代码中 -->
<!-- 使用aop进行配置,需要在文件的头部加入aop schema-->
<aop:config>
<!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
<aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.CalcImpl.*(..))"/>
<!-- 引用外部定义的aspect bean -->
<aop:aspect ref="calcAspect">
<!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
<!-- 前置通知 -->
<aop:before method="beforeM" pointcut-ref="all_calc_method"/>
<!-- 环绕通知 -->
<aop:around pointcut-ref="all_calc_method" method="aroundM"/>
<!-- 最终通知 -->
<aop:after method="afterFinallyM" pointcut-ref="all_calc_method"/>
<!-- 后置(返回值)通知 -->
<aop:after-returning method="afterReturningM" pointcut-ref="all_calc_method" returning="retVal"/>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="all_calc_method" throwing="throwable"/>
</aop:aspect>
</aop:config>
</beans>
最后通过获取Bean对象调用CalcImpl中的方法
/**
* 测试spring aop
*/
public class App {
public static void main(String[] args) {
ApplicationContext config = new ClassPathXmlApplicationContext("applicationConfig.xml");//配置文件换成你自己的
Calc calc = config.getBean("calc",Calc.class);
calc.add(800000000,900000000);
calc.subtract(894739429,482942849);
calc.multiply(843438,834993);
calc.divide(843822,223);
}
}
用注解的方式启用Spring AOP
开启自动织入支持
在xml中开启
<!-- 开启aop注解支持 -->
<aop:aspectj-autoproxy />
<!-- 开启注解支持,同时强制指定代理机制为cglib -->
<aop:aspectj-autoproxy proxy-target-class="true" />
通过注解开启
/**
* 纯注解方式入口类
*/
@Slf4j
@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.simple.say")
@EnableAspectJAutoProxy //开启对AOP相关注解的处理
public class AppByAnnotation {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByAnnotation.class);
ISayHello hello = ctx.getBean(ISayHello.class);
hello.sayHello("JinSaiSai");
}
}
定义切面类,添加@Aspect注解,@Component注解
由于@Aspect注解没有让Spring作为组件bean扫描的能力,所以我们需要额外添加@Component注解
/**
* 定义一个切面,负责收集方法调用的入参、出参(返回值)
*/
@Slf4j
@Aspect // 表示该类是一个切面
@Component // Aspect切面首先必须也是一个普通的bean
public class MethodInOutAspect {
// 指定该方法是一个环绕通知,通知注解的参数代表引用一个切入点表达式
@Around("com.lanou3g.spring.GlobalPointcut.say_all_method()")
public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取连接点方法的名称
String methodName = joinPoint.getSignature().getName();
// 获取连接点方法的参数
Object[] args = joinPoint.getArgs();
log.debug("[aroundM] "+methodName+"("+ Arrays.toString(args) +") 开始执行");
Object retuVal = joinPoint.proceed();
log.debug("[aroundM] "+methodName+"("+ Arrays.toString(args) +") 返回值: " + retuVal);
return retuVal;
}
@AfterReturning(pointcut = "com.lanou3g.spring.GlobalPointcut.say_all_method()", returning = "ret")
public Object afterRM(Object ret) {
log.debug("[afterRM] 返回值: " + ret);
return ret;
}
}
定义切入点(通过方法+注解)
/**
* 定义系统中所有用到的切入点表达式
*/
@Component
public class GlobalPointcut {
/**
* 通过@Pointcut注解定义切入点表达式
* 此处表达式含义:拦截com.lanou3g.spring.simple.say包下所有类(包括子包中所有类)中的所有方法
*/
@Pointcut("execution(* com.lanou3g.spring.simple.say..*.*(..))")
public void say_all_method() {}
}
定义通知(方法+注解)
在 MethodInOutAspect.java里写一个方法afterRM
@AfterReturning(pointcut = "com.lanou3g.spring.GlobalPointcut.say_all_method()", returning = "ret")
public Object afterRM(Object ret) {
log.debug("[afterRM] 返回值: " + ret);
return ret;
}
AOP的应用-通过xml方式和注解方式配置Spring声明式事务
通过xml方式配置Spring声明式事务
bean层实体类Teacher.java
@Getter
@Setter
public class Teacher {
private Integer id;
private String tname;
public Teacher() {
}
public Teacher(Integer id, String tname) {
this.id = id;
this.tname = tname;
}
public Teacher(String tname) {
this.tname = tname;
}
}
dao层实现类TeacherDaoImpl.java
@Setter
@Getter
public class TeacherDaoImpl {
private JdbcTemplate jdbcTemplate;
/**
* 查询teacher表所有数据
* @return
*/
public List<Teacher> queryAll() {
List<Teacher> teachers = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
return teachers;
}
/**
* 插入teacher
* @param teacher
* @return 返回影响行数
*/
public int insertTeacher(Teacher teacher) {
// 通过伪代码演示如果我们手写事务控制代码的套路
// setAutoCommit(false);
// beginTransaction
// try{
int result = jdbcTemplate.update("insert into teacher (tname) values (?)", teacher.getTname());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int retVal = 9 / 0;
// commit;
// } catch (Exception e) {
// rollback;
// }
//
return result;
}
}
数据库相关外部配置文件jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8
配置头文件,添加tx命名空间
<?xml version="1.0" encoding="UTF-8"?>
<!-- 通过xml配置声明式事务,我们需要添加tx命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
<!--开启注解支持并配置扫描包路径-->
<context:component-scan base-package="com.lanou3g.spring.transaction" />
<!--创建了一个JdbcTemplate类型的对象,JdbcTemplate是Spring提供给我们做jdbc CRUD增删改查
操作的类。将配置数据源创建的dataSource对象注入到jdbcTemplate对象中,那么我们的JdbcTemplate
知道要对哪个数据库进行增删改查的操作了。-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!--创建了一个TeacherDaoImpl类型的对象,将jdbcTemplate对象注入到TeacherDaoImpl对象中,那么TeacherDaoImpl对象知道了jdbcTemplate对象要对数据库进行哪些具体的操作了。-->
<bean id="teacherDao" class="com.lanou3g.spring.transaction.dao.TeacherDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
配置数据源
作用:连接数据库
数据源(DataSource)与数据库连接池(Database Connection Pool)的简单理解
<!-- 第一步:配置数据源 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="connectionProperties">
<props>
<prop key="characterEncoding">${jdbc.characterEncoding}</prop>
</props>
</property>
</bean>
配置事务管理器
作用:告诉你我要往对这个数据库的相关操作加入事务了
创建txManager对象,将dataSource对象注入到txManager对象中,那么我们的事务管理器就知道了要管理的是与哪个数据库相关的事务了。
<!-- 第二步:初始化事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
配置AOP通知(advice)
作用:告诉你我要往操作这个数据库的什么方法加入什么事务
创建txAdvice对象,将txManager对象注入到txAdvice对象中。那么我们的通知对象就知道了要往操作相应数据库
的什么方法加入事务管理器中内置的哪些事务了。
<!-- 第三步:配置事务AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" rollback-for="ArithmeticException" />
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
可以这样理解:Spring内置了一个切面类Aspect,里面包含了各种事务(例如rollback-for,read-only等),但是它处于闲置状态,没有哪一个事务被启用。配置事务AOP通知 (advice)相当于是激活了这个内置的切面类,并且指定了在什么方法加入什么具体的事务。然后再把这个切面类插入到下面的切入点,在切入点方法启用相应的事务。
配置AOP切入点
作用:让上面配置的这些事务生效,我要把事务插入到哪个切点,在哪里启用这些事务
<!-- 第四步:定义AOP配置(将上面的通知和表达式组装到一起) -->
<aop:config>
<aop:pointcut id="all_dao_method" expression="execution(* com.lanou3g.spring.transaction.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="all_dao_method" />
</aop:config>
<!-- 通过xml方式配置Spring声明式事务 配置结束 -->
xml方式入口类AppByTransactionXml.java
/**
* 纯xml方式入口类
*/
@Slf4j
public class AppByTransactionXml {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("tx_conf.xml");
testQuery(ctx);
// testTransaction(ctx);
}
static void testQuery(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
List<Teacher> teacherList = teacherDao.queryAll();
for(Teacher teacher : teacherList) {
System.out.println("id: " + teacher.getId()+", tname: " + teacher.getTname());
}
}
static void testTransaction(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
int rowEffect = teacherDao.insertTeacher(new Teacher("女儿国王"));
System.out.println("影响了" + rowEffect + "行.");
}
}
通过注解方式配置Spring声明式事务
配置数据源
对应xml方式中的"配置数据源"步骤。
@PropertySource此注解用于加载指定路径下的properties文件到环境中,然后我们就可以获取properties文件中定义的属性
@Component注解是一个通用注解,代表一个组件,可以用来标识所有希望让Spring管理的bean。
@Value(" j d b c . d r i v e r " ) 获 取 p r o p e r t i e s 文 件 中 定 义 的 d r i v e r 属 性 值 , 相 当 于 有 一 个 g e t 方 法 拿 到 了 这 个 d r i v e r 属 性 值 。 @ V a l u e ( " {jdbc.driver}")获取properties文件中定义的driver属性值,相当于有一个get方法拿到了这个driver属性值。 @Value(" jdbc.driver")获取properties文件中定义的driver属性值,相当于有一个get方法拿到了这个driver属性值。@Value("{jdbc.url}"),@Value(" j d b c . u s e r " ) , @ V a l u e ( " {jdbc.user}"),@Value(" jdbc.user"),@Value("{jdbc.password}"),@Value("${jdbc.characterEncoding}")与之类似。
/**
* 配置数据源
* (连接数据库)
*/
@PropertySource("classpath:jdbc.properties")
@Component
public class MyDataSource extends DriverManagerDataSource{
public MyDataSource(@Value("${jdbc.driver}") String driver, @Value("${jdbc.url}") String url, @Value("${jdbc.user}") String userName, @Value("${jdbc.password}") String password, @Value("${jdbc.characterEncoding}") String characterEncoding) {
super.setDriverClassName(driver);
super.setUrl(url);
super.setUsername(userName);
super.setPassword(password);
Properties conProperties = new Properties();
conProperties.setProperty("characterEncoding", characterEncoding);
super.setConnectionProperties(conProperties);
}
}
开启事务相关注解支持
@Configuration
@EnableTransactionManagement
定义事务管理器bean
/**
* 通过注解的方式配置Spring声明式事务
* 也可以将事务管理器定义到xml中,注解事务会按类型自动找到xml中配置的事务管理器
*/
@Configuration
public class MyTransactionConf {
/**
* 定义TransactionManager bean
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
@Configuration 注解的类相当于xml配置文件中的
类上面必须添加此注解,否则类里面无法用@Bean注解创建对象。
@Bean注解允许我们通过注解的方式定义在Java代码中定义bean的配置元数据,
相当于xml配置文件中的
/**
* 纯注解方式入口类
*/
@Slf4j
//日志相关注解
@Configuration
//相当于xml配置文件中的<beans />
@ComponentScan(basePackages = "com.lanou3g.spring.transaction.annotation")
//配置扫描包路径
@EnableTransactionManagement
// 开启事务相关注解支持
public class AppByTransactionAnnotation {
/**
* 创建JdbcTemplate对象,Spring给我们封装了所有的JDBC操作
* 将DataSource对象注入到JdbcTemplate对象中,那么我们的JdbcTemplate
* 对象知道要对哪个数据库进行增删改查的操作了。
* @param dataSource
* @return
*/
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* 创建事务管理器对象(告诉你我要往哪里加入事务)
* 将DataSource对象注入到PlatformTransactionManager对象中,那么
* PlatformTransactionManager对象就知道了要往对哪个数据库的操作
* 加入事务管理器中内置的事务了。
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByTransactionAnnotation.class);
testQuery(ctx);
//testTransaction(ctx);
}
static void testQuery(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
List<Teacher> teacherList = teacherDao.queryAll();
for(Teacher teacher : teacherList) {
System.out.println("id: " + teacher.getId()+", tname: " + teacher.getTname());
}
}
static void testTransaction(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
int rowEffect = teacherDao.insertTeacher(new Teacher("周伯通"));
System.out.println("影响了" + rowEffect + "行.");
}
}
在需要事务的方法上添加事务注解
要了解@Repository的作用,我们得先了解@Component的作用。
@Component、@Repository、@Service、@Controller
@Component注解是一个通用注解,代表一个组件,可以用来标识所有希望让Spring管理的bean。
@Repository、@Service、@Controller三个注解扩展自@Component,带有具体的语义色彩,专门用于修饰dao、service、controller层的类
上面这几个注解都可以通过给定value参数来指定bean的name,如果没有给定参数。那将由Spring的AnnotationBeanNameGenerator来自动生成(其实底层就是调用了java.beans.Introspector#decapitalize()方法来实现)
@Autowired自动注入。根据依赖的类型自动注入。该注解可以用于标注在 属性、setter方法、构造方法、或带参数的普通方法之上。Spring会将符合的参数自动注入到属性、或方法参数中。
@Repository相当于创建了一个TeacherDaoImpl对象,入口类就可以用这个对象调用类TeacherDaoImpl里面的方法了。
@Autowired相当于创建了一个JdbcTemplate对象,这个对象就可以用来对相应的数据库进行增删改查的操作了。
// 此注解和@Component作用一样, 只是含有特定的语义(一般用来标注dao层的类)
@Setter
@Getter
@Repository
public class TeacherDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate;
/**DataSource对象已经注入到了JdbcTemplate对象中,就是说我们的JdbcTemplate
* 对象已经知道要对哪个数据库进行增删改查的操作了。
*/
/**
* 查询teacher表所有数据
* @return
*/
// 通过@Transactional 开启事务、设置事务属性
@Transactional(readOnly = true)
public List<Teacher> queryAll() {
List<Teacher> teachers = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
return teachers;
//int rows = jdbcTemplate.update("delete from teacher;");
//System.out.println("影响了" + rows + "行.");
//return null;
}
/**
* 插入teacher
* @param teacher
* @return 返回影响行数
*/
// 通过@Transactional 开启事务、设置事务属性
@Transactional(rollbackFor = {ArithmeticException.class})
public int insertTeacher(Teacher teacher) {
// 通过伪代码演示如果我们手写事务控制代码的套路
// setAutoCommit(false);
// beginTransaction
// try{
int result = jdbcTemplate.update("insert into teacher (tname) values (?)", teacher.getTname());
// int retVal = 9 / 0;
// commit;
// } catch (Exception e) {
// rollback;
// }
//
return result;
}
}
Mybatis
mybatis概述
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的XML或注解来配置和映射原生信息,将接口和java的POJO(Plain Old Java Objects,普通老式java对象)映射成数据库中的记录。
mybatis基础依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
基于XML方式映射SQL
定义数据库相关外部配置文件jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8
定义 MyBatis 系统核心设置mybatis_conf.xml
配置文件中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。当然,还有很多属性可以在 XML 文件中进行配置,下面的示例指出的则是最关键的部分。 要注意 XML 头部的声明,它用来验证 XML 文档正确性。environment 元素体中包含了事务管理和连接池的配置。mappers 元素则是包含一组映射器(mapper),这些映射器的 XML 映射文件包含了 SQL 代码和映射定义信息。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties" />
<typeAliases>
<typeAlias type="com.lanou3g.mybatis.bean.Teacher" alias="Teacher" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/TeacherMapper.xml" />
</mappers>
</configuration>
定义XML SQL配置文件TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace对应空Dao接口的全名 -->
<mapper namespace="com.lanou3g.mybatis.dao.TeacherDao">
<!-- 此处的id是查询语句的名称,对应接口中的方法名 -->
<select id="queryAll" resultType="Teacher">
select * from teacher;
</select>
</mapper>
定义对应的Mapper接口TeacherDao.java
public interface TeacherDao {
List<Teacher> queryAll();
}
定义入口类App.java
@Slf4j
public class App {
public static void main(String[] args) throws Exception {
// 1. 读入配置文件
String confPath = "mybatis_conf.xml";
InputStream in = Resources.getResourceAsStream(confPath);
// 2. 构建SqlSessionFactory(用于获取sqlSession)
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3. 获取sqlSession对象(用于具体的CRUD)
SqlSession sqlSession = sessionFactory.openSession();
TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);
System.out.println(teacherDao.queryAll());
}
}
基于注解方式映射SQL
mybatis最初设计时就是一个基于XML驱动的框架。虽然从Mybatis3开始Mybatis支持了很多基于注解方式的配置,但从实际使用来看,也仅适用于比较简单的场景下。像一些比较复杂的结果集映射用注解写出来可读性和可维护性就很低。
上面并不是说注解配置就一无是处, 比如一些简单的单表CRUD语句我们就可以直接通过注解来配置, 而不在需要单独写一个XML映射文件。
定义数据库相关外部配置文件jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8
定义 MyBatis 系统核心设置mybatis_conf.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.lanou3g.mybatis.dao.StudentDao" />
</mappers>
</configuration>
定义对应的Mapper接口StudentDao.java
public interface StudentDao {
@Select("select sname, nick_name as nickName from student;")
List<Student> queryAll();
}
定义入口类App.java
@Slf4j
public class App {
public static void main(String[] args) throws Exception {
// 1. 读入配置文件
String confPath = "mybatis_conf.xml";
InputStream in = Resources.getResourceAsStream(confPath);
// 2. 构建SqlSessionFactory(用于获取sqlSession)
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3. 获取sqlSession对象(用于具体的CRUD)
SqlSession sqlSession = sessionFactory.openSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> studentList = studentDao.queryAll();
log.info("studentList: " + studentList);
//System.out.println(studentList);
}
}
本文详细介绍了AOP(面向切面编程)的核心概念,包括AOP与OOP的区别、AspectJ、切入点、通知类型以及Spring AOP的XML和注解配置方式。同时,文章还阐述了Spring声明式事务的配置,并给出了基于XML和注解的示例。此外,文章还简要探讨了Mybatis的基本依赖和两种方式(XML与注解)的映射SQL,包括数据库配置、Mapper接口和配置文件等内容。
681

被折叠的 条评论
为什么被折叠?



