使用AOP
- 什么是AOP
AOP是面向切面編程的縮寫进栽。在軟件開發(fā)中径密,散布于應(yīng)用中多處的功能被稱為橫切關(guān)注的(Cross-Cutting Concern)钾恢。
這些橫切關(guān)注點從概念上是與應(yīng)用程序的業(yè)務(wù)邏輯分離的(但是往往又要嵌入到應(yīng)用的邏輯中)解幽,把這些橫切關(guān)注點與業(yè)務(wù)邏輯分離開來就是AOP要解決的問題廷粒。
如果說依賴注入幫助我們解決了對象之間的耦合關(guān)系窘拯,那么AOP就是要把橫切關(guān)注功能和它們所影響的對象之間進(jìn)行解耦合。
- AOP的術(shù)語:
A.Advice(增強):定義切面的功能以及使用的時間坝茎。
- 前置增強(Before)
- 后置增強(After)
- 返回增強(AfterReturning)
- 異常增強(AfterThrowing)
- 環(huán)繞增強(Around)
B.Join Point(連接點):應(yīng)用程序的邏輯跟切面交匯的一個點涤姊。
C.Pointcut(切入點):切入用來定義切面使用的位置。定義切面時可以使用利用切點表達(dá)式來描述在什么地方應(yīng)用切面
AspectJ指示器 | 描述 |
---|---|
arg() | |
@args() | |
execution() | 連接到的執(zhí)行方法 |
this() | |
target() | |
@target() | |
within() | |
@within() | |
@annotation |
D.Aspect(切面):增強和切入點的集合嗤放。
E.Introduction(引入):允許我們給現(xiàn)有的類添加新方法或?qū)傩浴?br>
F.Weaving(織入):把切面應(yīng)用于目標(biāo)對象(創(chuàng)建代理對象)的過程思喊。
持久層使用JPA規(guī)范
1.在src目錄下新建文件夾META-INF
2.新建xml文件:persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="Demo">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.kygo.entity.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/hib?useUnicode=true&characterEncoding=utf8" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="123456" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
3.新建JPAUtil工具類
public class JPAUtil {
private static ThreadLocal<EntityManager> threadLocal =
new ThreadLocal<>();
private static EntityManagerFactory factory = null;
static {
factory = Persistence.createEntityManagerFactory("Demo");
}
private JPAUtil() {
throw new AssertionError();
}
public static EntityManager getCurrentEM() {
EntityManager entityManager = threadLocal.get();
if (entityManager == null) {
entityManager = factory.createEntityManager();
threadLocal.set(entityManager);
}
return entityManager;
}
}
事務(wù)切面
@Aspect
public class TxAspect {
// 切面執(zhí)行的位置
@Pointcut("execution(* com.kygo.biz.impl.*.*(..))")
public void foo() {}
// 切面執(zhí)行的時機
@Around("foo()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
EntityTransaction tx = JPAUtil.getCurrentEM().getTransaction();
try {
tx.begin();
Object retValue = joinPoint.proceed(joinPoint.getArgs());
tx.commit();
return retValue;
} catch (Throwable e) {
tx.rollback();
throw e;
}
}
}
xml配置
<bean id="tx" class="com.kygo.aspect.TxAspect" />
<aop:config>
<aop:aspect id="txAspect" ref="tx">
<aop:around method="around"
pointcut="execution(* com.kygo.biz.impl.*.*(..))"/>
</aop:aspect>
</aop:config>