Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化
前言
AOP(Aspect Oriented Program 面向切面編程)的實(shí)現(xiàn)基于Java的代理機(jī)制, 下面介紹Spring Aop的術(shù)語
1:切面(aspect)
切面是對象操作過程中的截面 如圖19.1所示 切面是指圖中的平行四邊形
2:連接點(diǎn)(join point)
連接點(diǎn)是指對象操作過程中的某個(gè)階段點(diǎn) 實(shí)際上是對象的一個(gè)操作 如圖19.2所示
3:切入點(diǎn)(pointcut)
切入點(diǎn)是連接點(diǎn)的集合 如圖19.3 切面與程序流程的交叉點(diǎn)便是程序的切入點(diǎn)
4:通知(advice)
通知是某個(gè)切入點(diǎn)被橫切后,所采取的邏輯,也就是說 在切入點(diǎn)處攔截程序后,通過通知來執(zhí)行切面
5:目標(biāo)對象(target)
所有被通知的對象都是目標(biāo)對象 被AOP所關(guān)注
6:織入(weaving)
織入是將切面功能應(yīng)用到目標(biāo)對象的過程 由代理工廠創(chuàng)建一個(gè)代理對象 這個(gè)代理可以為目標(biāo)對象執(zhí)行切面功能
7:引入(introduction)
對一個(gè)已編譯完類 在運(yùn)行時(shí)期 動態(tài)地向這個(gè)類中加載屬性和方法
一、AOP的簡單實(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)對象的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的簡單實(shí)現(xiàn)"); } }
二、Spring的切入點(diǎn)
1:靜態(tài)切入點(diǎn)
靜態(tài)意味著不變,無論在程序的任何位置調(diào)用 方法名都不會改變 配置文件如下
指定所有以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:動態(tài)切入點(diǎn)
動態(tài)切入點(diǎn)應(yīng)用在相對變化的位置
深入靜態(tài)切入點(diǎn)
實(shí)際上Spring使使用boolean matches(Method,Class)方法來匹配切入點(diǎn)的
public boolean matches(Method method,Class targetClass){ return(method.getName().equals("execute")); }
深入切入點(diǎn)底層
Pointcut接口是切入點(diǎn)的定義接口 用它來規(guī)定可切入的連接點(diǎn)的屬性 通過對該接口的擴(kuò)展可以處理其他類型的連接點(diǎn) 接口定義代碼如下
public interface Pointcut{ ClassFilter getClassFilter(); MethodMatcher getMethodMatcher(); }
使用ClassFilter接口來匹配目標(biāo)類 代碼如下
public interface ClassFilter{ boolean matches(Class class); }
使用MethodMatcher接口來匹配目標(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í)行動態(tài)切入點(diǎn)
三、Aspect對AOP的支持
Aspect是對系統(tǒng)中的對象操作過程中截面邏輯進(jìn)行模塊化封裝的AOP概念實(shí)體,在通常情況下,Aspect可以包含多個(gè)切入點(diǎn)和通知
Advisor就是Spring中的Aspect 是切入點(diǎn)的配置器 結(jié)構(gòu)如下
1:DefaultPointcutAdvisor切入點(diǎn)配置器
它可以把一個(gè)通知配給一個(gè)切入點(diǎn) 使用之前 首先要創(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)配置器
使用它可以更簡潔的將方法名設(shè)定為切入點(diǎn)
NameMatchMethodPointcutAdvisor advice=new NameMatchMethodPointcutAdvisor(new TestAdvice()); advice.addMethodName("targetMethod1name"); ...
四、Spring持久化
1:DAO模式
DAO代表數(shù)據(jù)訪問對象(Data Access Object) 它描述了一個(gè)應(yīng)用中的DAO角色 它提供了讀寫數(shù)據(jù)庫中數(shù)據(jù)的一種方法,把這個(gè)功能通過接口提供對外服務(wù) 使得解耦合,對于整個(gè)系統(tǒng)性能有巨大的提升
Spring DAO抽象提供了以下幾類
1:JdbcDaoSupport JDBC DAO抽象類 開發(fā)者需要為他設(shè)置數(shù)據(jù)源 通過子類 開發(fā)者能夠獲得JdbcTmeplate來訪問數(shù)據(jù)庫
2:HibernateDaoSupport 通過其子類 開發(fā)者能夠獲得Hibernate實(shí)現(xiàn)
3:JdoDaoSupport 通過其子類 開發(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)系 它通過AOP實(shí)現(xiàn)事務(wù)管理 Spring本身就是一個(gè)容器 相對于EJB容器而言 Spring顯得更為輕量級
在Spring中常用TransactionProxyFactoryBean完成聲明式管理
應(yīng)用JdbcTemplate操作數(shù)據(jù)庫
JdbcTemplate類式Spring的核心類之一 它在內(nèi)部類已經(jīng)處理完了數(shù)據(jù)庫資源的創(chuàng)建和銷毀 只需在代碼中提供sql語句即可
配置JdbcTemplate和數(shù)據(jù)源
<!--配置JdbcTemplate> <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean>
獲取JdbcTemplate對象 并利用它的update方法執(zhí)行數(shù)據(jù)庫的添加操作
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整合了對Hibernate的設(shè)定 Spring中提供了HibernateTemplate類和HibernateDaoSupport類以及相應(yīng)的子類 使用戶時(shí)可以簡化程序編寫的資源
到此這篇關(guān)于Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化的文章就介紹到這了,更多相關(guān)Spring AOP 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理
這篇文章主要為大家介紹了Java開發(fā)HashMap?key必須實(shí)現(xiàn)hashCode?equals方法原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03MyBatis源碼解析——獲取SqlSessionFactory方式
這篇文章主要介紹了MyBatis源碼解析——獲取SqlSessionFactory方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java8中Stream流求最大值最小值的實(shí)現(xiàn)示例
本文主要介紹了Java8中Stream流求最大值最小值的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04java開發(fā)MyBatis中常用plus實(shí)體類注解符詳解
這篇文章主要為大家介紹了java開發(fā)MyBatis常用的plus實(shí)體類注解符示例應(yīng)用詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10字節(jié)二面SpringBoot可以同時(shí)處理多少請求
這篇文章主要為大家介紹了字節(jié)二面之SpringBoot可以同時(shí)處理多少請求面試分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07RabbitMQ交換機(jī)使用場景和消息可靠性總結(jié)分析
這篇文章主要為大家介紹了RabbitMQ交換機(jī)使用場景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java中實(shí)現(xiàn)List分隔成子List詳解
大家好,本篇文章主要講的是Java中實(shí)現(xiàn)List分隔成子List詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01