對比AOP織入 方式一
<!-- 方式二:使用切面定義 自定義類 -->
<bean id="diy" class="com.kuang.diy.Diy"/>
<aop:config>
<!-- 自定義切面,ref要引入的類 -->
<aop:aspect ref="diy">
<!-- 切入點 -->
<aop:pointcut id="point" expression="execution(* com.kuang.services.ServiceImpl.*(..))"/>
<!-- 通知 -->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
?對比AOP織入 方式二
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注冊bean-->
<bean id="service" class="com.kuang.services.ServiceImpl"/>
<bean id="logBefore" class="com.kuang.log.LogBefore"/>
<bean id="logAfter" class="com.kuang.log.LogAfter"/>
<!-- 方式一:使用原生Spring API接口 -->
<!--配置AOP : 需要導入aop約束-->
<aop:config>
<!-- 切入點 :expression表達式 = execution(需要執(zhí)行的位置! 修飾符 返回值 類名.方法名.參數(shù) (* * * * *)) -->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.services.ServiceImpl.*(..))"/>
<!-- 執(zhí)行環(huán)繞增加 -->
<aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>
</aop:config>
</beans>
?注解實現(xiàn)AOP
package com.qf.common;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增強類 事務管理類
//切面=切點加通知
@Aspect
@Component
public class TransManger {
//定義切點,無參無返回值的方法 pc相當于切點的ID
@Pointcut("execution(* com.qf.service..*.*(..))")
public void pc(){}
@Before("pc()")
public void startTran(){
System.out.println("開啟事務~~~~~~~~~~~~");
}
@AfterReturning("pc()")
public void commitTran(){
System.out.println("提交事務.............");
}
@AfterThrowing("pc()")
public void rollBackTran(){
System.out.println("回滾事務~~~~~~~~~~~~~~");
}
@After("pc()")
public void afterMethod(){
System.out.println("測試AOP");
}
// 加給AOP:around環(huán)繞通知
@Around("pc()")
public Object aroundMethod(ProceedingJoinPoint pj) throws Throwable {
System.out.println("環(huán)繞執(zhí)行前");
long start = System.currentTimeMillis();
Object proceed = pj.proceed();
long end = System.currentTimeMillis();
System.out.println(".......delete_after");
System.out.println(end-start);
System.out.println("環(huán)繞執(zhí)行后");
return proceed;
}
}
spring-dao.xml?
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.qf"/>
<!-- 整合數(shù)據(jù)源屬性配置文件,參數(shù)化占位符 ,讀取參數(shù)${} -->
<context:property-placeholder location="classpath:druid.properties"/>
<!--配置數(shù)據(jù)源,DataSource就是一個對象,將對象交由spring容器創(chuàng)建并管理-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置需要配置別名的實體類包 -->
<property name="typeAliasesPackage" value="com.qf"/>
<!-- 配置mybatis映射文件加載路徑,如果映射文件與接口文件不在同一路徑下,需要配置 因為是在resource下所以得必須用/ -->
<property name="mapperLocations" value="classpath:com/qf/mapper/*.xml"/>
<!-- 加載mabatis 核心配置文件,需要時加載,如果配置為空,可以省略不寫 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置SqlSession和接口得代理對象 -->
<!-- bean默認是單例模式,它里面有一個私有的對象,只設置了一次 sqlSessionFactory 創(chuàng)建出一個sqlSessionTemplate 對象,這個對象相當于sqlSession 接口的代理對象,-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定sqlSessionFactory注入 String字符串 名字,不是bean getbean是在 MapperScannerConfigurer 類內(nèi)部被調(diào)用,拿這個變量去調(diào)用得
肯定設置了openSession(true) 默認開啟自動提交
用得同一個session ,將不能使用二級緩存了,一般同個session一級緩存就夠用了,一級緩存,增刪改會清除緩存,但是只是查詢,會找到一級緩存里之前查過的數(shù)據(jù)直接查出來,不調(diào)用sql,
只是用同一個session 創(chuàng)建getMapper不同得接口代理類,去實現(xiàn)不同的接口,代理這個代理類。
創(chuàng)建了這個代理對象注入進IOC容器里面。普通得getMapper不能創(chuàng)建 一個類, 他只是創(chuàng)建一個對象,
然后通過動態(tài)代理這個對象去實現(xiàn)共同的接口的一個類,把這個類才能加入IOC容器里,返回給我們使用這個代理對象。
-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 設置要創(chuàng)建mapper對象得接口所在得包名 因為代理的是接口,所以指定包名是接口的包名,
通過接口的字節(jié)碼對象,與代理的目標對象的字節(jié)碼對象,加上實現(xiàn)InvocationHandler的類對象,完成代理對象的創(chuàng)建,然后把bean創(chuàng)建給容器。 -->
<property name="basePackage" value="com.qf.mapper"/>
</bean>
<!--配置事務管理器-->
<bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入數(shù)據(jù)源對象,要求與sqlSessionFactory創(chuàng)建時所注入數(shù)據(jù)源對象是同一個 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <tx:annotation-driven transaction-manager="transactionManger"/>-->
<tx:advice id="txAdvice" transaction-manager="transactionManger">
<tx:attributes>
<tx:method name="insert*"/>
<tx:method name="delete*"/>
</tx:attributes>
</tx:advice>
<!--配置事務-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.qf.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
導入的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qf</groupId>
<artifactId>Spring-Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-version>5.3.20</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--spring相關依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<!--spring-aspects-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
文章來源地址http://www.zghlxwxcb.cn/news/detail-608572.html
文章來源:http://www.zghlxwxcb.cn/news/detail-608572.html
到了這里,關于Spring-Mybatis整合配置文件與AOP織入方式對比的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!