Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化
前言
AOP(Aspect Oriented Program 面向切面編程)的實(shí)現(xiàn)基于Java的代理機(jī)制, 下面介紹Spring Aop的術(shù)語(yǔ)
1:切面(aspect)
切面是對(duì)象操作過(guò)程中的截面 如圖19.1所示 切面是指圖中的平行四邊形
2:連接點(diǎn)(join point)
連接點(diǎn)是指對(duì)象操作過(guò)程中的某個(gè)階段點(diǎn) 實(shí)際上是對(duì)象的一個(gè)操作 如圖19.2所示

3:切入點(diǎn)(pointcut)
切入點(diǎn)是連接點(diǎn)的集合 如圖19.3 切面與程序流程的交叉點(diǎn)便是程序的切入點(diǎn)
4:通知(advice)
通知是某個(gè)切入點(diǎn)被橫切后,所采取的邏輯,也就是說(shuō) 在切入點(diǎn)處攔截程序后,通過(guò)通知來(lái)執(zhí)行切面

5:目標(biāo)對(duì)象(target)
所有被通知的對(duì)象都是目標(biāo)對(duì)象 被AOP所關(guān)注
6:織入(weaving)
織入是將切面功能應(yīng)用到目標(biāo)對(duì)象的過(guò)程 由代理工廠創(chuàng)建一個(gè)代理對(duì)象 這個(gè)代理可以為目標(biāo)對(duì)象執(zhí)行切面功能
7:引入(introduction)
對(duì)一個(gè)已編譯完類 在運(yùn)行時(shí)期 動(dòng)態(tài)地向這個(gè)類中加載屬性和方法
一、AOP的簡(jiǎn)單實(shí)現(xiàn)
利用Spring AOP使日志輸出與方法分離 讓在調(diào)用目標(biāo)方法之前執(zhí)行日志輸出
public class Target{
public void execute(String name){
System.out.printIn("程序開始執(zhí)行"+name);
}
}通知可以攔截目標(biāo)對(duì)象的execute方法 并執(zhí)行日志輸出
public class LoggerExecute implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable{
before();
invocation.proceed();
return null;
}
private void before(){
System.out.printIn("程序開始執(zhí)行");
}
}創(chuàng)建代理從而使用AOP功能
public class Manger{
public static void main(String[] args){
Target target=new Target();
ProxyFactory di=new ProxyFactory();
di.addAdvice(new LoggerExecute());
di.setTarget(target);
Target proxy=(Target)di.getProxy();
proxy.execute("AOP的簡(jiǎn)單實(shí)現(xiàn)");
}
}二、Spring的切入點(diǎn)
1:靜態(tài)切入點(diǎn)
靜態(tài)意味著不變,無(wú)論在程序的任何位置調(diào)用 方法名都不會(huì)改變 配置文件如下
指定所有以getConn開頭的方法名都是切入點(diǎn)
<bean id="pointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref bean="MyAdvisor"/> </property> <property name="patterns"> <list> <value>.*getConn*.</value> <value>.*closeConn*.</value> </list> </property> </bean>
2:動(dòng)態(tài)切入點(diǎn)
動(dòng)態(tài)切入點(diǎn)應(yīng)用在相對(duì)變化的位置
深入靜態(tài)切入點(diǎn)
實(shí)際上Spring使使用boolean matches(Method,Class)方法來(lái)匹配切入點(diǎn)的
public boolean matches(Method method,Class targetClass){
return(method.getName().equals("execute"));
}深入切入點(diǎn)底層
Pointcut接口是切入點(diǎn)的定義接口 用它來(lái)規(guī)定可切入的連接點(diǎn)的屬性 通過(guò)對(duì)該接口的擴(kuò)展可以處理其他類型的連接點(diǎn) 接口定義代碼如下
public interface Pointcut{
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}使用ClassFilter接口來(lái)匹配目標(biāo)類 代碼如下
public interface ClassFilter{
boolean matches(Class class);
}使用MethodMatcher接口來(lái)匹配目標(biāo)類的方法或方法的參數(shù)
public interface MethodMatcher{
boolean matches(Method m,Class targetClass);
boolean isRuntime();
boolean matches(Method m,Class targetClass,Object[]args);
}isRuntime方法返回false則執(zhí)行靜態(tài)切入點(diǎn) true則執(zhí)行動(dòng)態(tài)切入點(diǎn)
三、Aspect對(duì)AOP的支持
Aspect是對(duì)系統(tǒng)中的對(duì)象操作過(guò)程中截面邏輯進(jìn)行模塊化封裝的AOP概念實(shí)體,在通常情況下,Aspect可以包含多個(gè)切入點(diǎn)和通知
Advisor就是Spring中的Aspect 是切入點(diǎn)的配置器 結(jié)構(gòu)如下

1:DefaultPointcutAdvisor切入點(diǎn)配置器
它可以把一個(gè)通知配給一個(gè)切入點(diǎn) 使用之前 首先要?jiǎng)?chuàng)建一個(gè)切入點(diǎn)和通知
首先創(chuàng)建一個(gè)通知
public TestAdvice implements MethodInterceptor{
public Object invoke(MethodInvocation mi)thorws Throwable{
Object Val=mi.proceed();
return Val;
}
}創(chuàng)建自定義切入點(diǎn) 重寫matches和getclassfilter方法
public class TestStaticPointcut extends StaticMethodMatcherPointcut{
public boolean matches(Method method Class targetClass){
return("targetMethod".equals(method.getName());
}
public ClassFilter getClassFilter(){
return new ClassFilter()
{
public boolean matches(Class clazz){
return(clazz==targetClass.class);
}
};
}
}分別創(chuàng)建一個(gè)通知和切入點(diǎn)實(shí)例
Pointcut pointcut=new TestStaticPointcut(); Advice advice=new TestAdvice();
2:NameMatchMethodPointcutAdvisor 切入點(diǎn)配置器
使用它可以更簡(jiǎn)潔的將方法名設(shè)定為切入點(diǎn)
NameMatchMethodPointcutAdvisor advice=new NameMatchMethodPointcutAdvisor(new TestAdvice());
advice.addMethodName("targetMethod1name");
...四、Spring持久化
1:DAO模式
DAO代表數(shù)據(jù)訪問(wèn)對(duì)象(Data Access Object) 它描述了一個(gè)應(yīng)用中的DAO角色 它提供了讀寫數(shù)據(jù)庫(kù)中數(shù)據(jù)的一種方法,把這個(gè)功能通過(guò)接口提供對(duì)外服務(wù) 使得解耦合,對(duì)于整個(gè)系統(tǒng)性能有巨大的提升
Spring DAO抽象提供了以下幾類
1:JdbcDaoSupport JDBC DAO抽象類 開發(fā)者需要為他設(shè)置數(shù)據(jù)源 通過(guò)子類 開發(fā)者能夠獲得JdbcTmeplate來(lái)訪問(wèn)數(shù)據(jù)庫(kù)
2:HibernateDaoSupport 通過(guò)其子類 開發(fā)者能夠獲得Hibernate實(shí)現(xiàn)
3:JdoDaoSupport 通過(guò)其子類 開發(fā)者能夠獲得JdoTemplate

事務(wù)應(yīng)用的管理
1:編程式事務(wù)管理
在Spring中主要有兩種編程式事務(wù)的實(shí)現(xiàn)方法 分別使用PlatfromTransactionManager接口的事務(wù)管理器或者TransactionTemplate實(shí)現(xiàn) 但是推薦使用Transaction Template實(shí)現(xiàn)方式 因?yàn)樗蟂pring的模板模式
2:聲明式事務(wù)管理
Spring的聲明式事務(wù)管理不涉及組建依賴關(guān)系 它通過(guò)AOP實(shí)現(xiàn)事務(wù)管理 Spring本身就是一個(gè)容器 相對(duì)于EJB容器而言 Spring顯得更為輕量級(jí)
在Spring中常用TransactionProxyFactoryBean完成聲明式管理
應(yīng)用JdbcTemplate操作數(shù)據(jù)庫(kù)
JdbcTemplate類式Spring的核心類之一 它在內(nèi)部類已經(jīng)處理完了數(shù)據(jù)庫(kù)資源的創(chuàng)建和銷毀 只需在代碼中提供sql語(yǔ)句即可
配置JdbcTemplate和數(shù)據(jù)源
<!--配置JdbcTemplate> <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean>
獲取JdbcTemplate對(duì)象 并利用它的update方法執(zhí)行數(shù)據(jù)庫(kù)的添加操作
DriverManagerSource ds=null;
JdbcTemplate jtl=null;
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
jtl=(JdbcTemplate)factory.getBean("jdbcTemplate");
String sql="insert into table()values()";
jtl.update(sql);與Hibernate整合
Spring整合了對(duì)Hibernate的設(shè)定 Spring中提供了HibernateTemplate類和HibernateDaoSupport類以及相應(yīng)的子類 使用戶時(shí)可以簡(jiǎn)化程序編寫的資源
到此這篇關(guān)于Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化的文章就介紹到這了,更多相關(guān)Spring AOP 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring 靜態(tài)變量/構(gòu)造函數(shù)注入失敗的解決方案
我們經(jīng)常會(huì)遇到一下問(wèn)題:Spring對(duì)靜態(tài)變量的注入為空、在構(gòu)造函數(shù)中使用Spring容器中的Bean對(duì)象,得到的結(jié)果為空。不要擔(dān)心,本文將為大家介紹如何解決這些問(wèn)題,跟隨小編來(lái)看看吧2021-11-11
如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題
這篇文章主要介紹了如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
關(guān)于Spring?Ioc和DI注解的問(wèn)題
這篇文章主要介紹了Spring?Ioc和DI注解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Java使用BigDecimal進(jìn)行高精度計(jì)算的示例代碼
本篇文章主要介紹了Java使用BigDecimal進(jìn)行高精度計(jì)算的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Java中為什么this可以調(diào)用當(dāng)前實(shí)例
本文主要介紹了為什么可以通過(guò)this關(guān)鍵字訪問(wèn)到當(dāng)前對(duì)象呢,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
自己動(dòng)手在Spring-Boot上加強(qiáng)國(guó)際化功能的示例
這篇文章主要介紹了自己動(dòng)手在Spring-Boot上加強(qiáng)國(guó)際化功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

