本文目錄一覽:
- 1、關於spring3使用AOP編程時需要引入哪些jar包的問題
- 2、Error creating bean with name ‘org.springframework.web.servlet.mvc.annotation
- 3、spring aop 幹什麼用
- 4、spring已經實現了aop,為什麼還要用“aspectj”?
- 5、spring添加aspectjweaver包後依舊報錯
- 6、缺少aspectjweaver-1.6.5.jar包會報什麼錯誤
關於spring3使用AOP編程時需要引入哪些jar包的問題
您好,我來為您解答:
初次接觸spring框架,網上最新版的spring已經是3.1了
經過多次摸索,除了引入spring框架dist目錄下的org.springframework.aop-3.1.1.RELEASE.jar之外,
還需要自己下載第三方依賴包:
aspectjrt.jar,aspectjweaver.jar(最新發布版是1.6.2)
以及aopalliance.jar(最新發布版是1.0)
引入以上jar包之後,就可以通過@Aspect等註解方式進行AOP編程了;
依此記錄下來,供其他遇到這類問題的朋友參考。
如果我的回答沒能幫助您,請繼續追問。
Error creating bean with name ‘org.springframework.web.servlet.mvc.annotation
原因可能如下:
1、可能是使用了外部屬性文件,沒有導入外部資源文件報錯;
解決方案:引入外部資源文件,在Spring配置文件中加入:context:property-placeholder location=”classpath:db.properties”/
2、Spring配置文件正確,是因為沒有導入aspectjweaver.jar包;
解決方案:導入aspectjweaver.jar包就不報錯啦,沒有導入aspectjweaver.jar可能會報錯:Caused by: java.lang.NoClassDefFoundError:org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException,aspectjweaver.jar是和Spring AOP有關的jar包;
spring aop 幹什麼用
AOP
AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向對象編程)的補充和完善。OOP引入封裝、繼承、多態等概念來建立一種對象層次結構,用於模擬公共行為的一個集合。不過OOP允許開發者定義縱向的關係,但並不適合定義橫向的關係,例如日誌功能。日誌代碼往往橫向地散布在所有對象層次中,而與它對應的對象的核心功能毫無關係對於其他類型的代碼,如安全性、異常處理和透明的持續性也都是如此,這種散布在各處的無關的代碼被稱為橫切(cross cutting),在OOP設計中,它導致了大量代碼的重複,而不利於各個模塊的重用。
AOP技術恰恰相反,它利用一種稱為”橫切”的技術,剖解開封裝的對象內部,並將那些影響了多個類的公共行為封裝到一個可重用模塊,並將其命名為”Aspect”,即切面。所謂”切面”,簡單說就是那些與業務無關,卻為業務模塊所共同調用的邏輯或責任封裝起來,便於減少系統的重複代碼,降低模塊之間的耦合度,並有利於未來的可操作性和可維護性。
使用”橫切”技術,AOP把軟件系統分為兩個部分:核心關注點和橫切關注點。業務處理的主要流程是核心關注點,與之關係不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發生在核心關注點的多處,而各處基本相似,比如權限認證、日誌、事物。AOP的作用在於分離系統中的各種關注點,將核心關注點和橫切關注點分離開來。
AOP核心概念
1、橫切關注點
對哪些方法進行攔截,攔截後怎麼處理,這些關注點稱之為橫切關注點
2、切面(aspect)
類是對物體特徵的抽象,切面就是對橫切關注點的抽象
3、連接點(joinpoint)
被攔截到的點,因為Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構造器
4、切入點(pointcut)
對連接點進行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點之後要執行的代碼,通知分為前置、後置、異常、最終、環繞通知五類
6、目標對象
代理的目標對象
7、織入(weave)
將切面應用到目標對象並導致代理對象創建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運行期為類動態地添加一些方法或字段
Spring對AOP的支持
Spring中AOP代理由Spring的IOC容器負責生成、管理,其依賴關係也由IOC容器負責管理。因此,AOP代理可以直接使用容器中的其它bean實例作為目標,這種關係可由IOC容器的依賴注入提供。Spring創建代理的規則為:
1、默認使用Java動態代理來創建AOP代理,這樣就可以為任何接口實例創建代理了
2、當需要代理的類不是代理接口的時候,Spring會切換為使用CGLIB代理,也可強制使用CGLIB
AOP編程其實是很簡單的事情,縱觀AOP編程,程序員只需要參與三個部分:
1、定義普通業務組件
2、定義切入點,一個切入點可能橫切多個業務組件
3、定義增強處理,增強處理就是在AOP框架為普通業務組件織入的處理動作
所以進行AOP編程的關鍵就是定義切入點和定義增強處理,一旦定義了合適的切入點和增強處理,AOP框架將自動生成AOP代理,即:代理對象的方法=增強處理+被代理對象的方法。
下面給出一個Spring AOP的.xml文件模板,名字叫做aop.xml,之後的內容都在aop.xml上進行擴展:
?xml version=”1.0″ encoding=”UTF-8″?beans xmlns=””
xmlns:xsi=””
xmlns:aop=””
xmlns:tx=””
xsi:schemaLocation=”
“
/beans
基於Spring的AOP簡單實現
注意一下,在講解之前,說明一點:使用Spring AOP,要成功運行起代碼,只用Spring提供給開發者的jar包是不夠的,請額外上網下載兩個jar包:
1、aopalliance.jar
2、aspectjweaver.jar
開始講解用Spring AOP的XML實現方式,先定義一個接口:
public interface HelloWorld
{ void printHelloWorld(); void doPrint();
}
定義兩個接口實現類:
public class HelloWorldImpl1 implements HelloWorld
{ public void printHelloWorld()
{
System.out.println(“Enter HelloWorldImpl1.printHelloWorld()”);
}
public void doPrint()
{
System.out.println(“Enter HelloWorldImpl1.doPrint()”); return ;
}
}
public class HelloWorldImpl2 implements HelloWorld
{ public void printHelloWorld()
{
System.out.println(“Enter HelloWorldImpl2.printHelloWorld()”);
}
public void doPrint()
{
System.out.println(“Enter HelloWorldImpl2.doPrint()”); return ;
}
}
橫切關注點,這裡是打印時間:
public class TimeHandler
{ public void printTime()
{
System.out.println(“CurrentTime = ” + System.currentTimeMillis());
}
}
有這三個類就可以實現一個簡單的Spring AOP了,看一下aop.xml的配置:
?xml version=”1.0″ encoding=”UTF-8″?beans xmlns=””
xmlns:xsi=””
xmlns:aop=””
xmlns:tx=””
xsi:schemaLocation=”
“
bean id=”helloWorldImpl1″ class=”com.xrq.aop.HelloWorldImpl1″ /
bean id=”helloWorldImpl2″ class=”com.xrq.aop.HelloWorldImpl2″ /
bean id=”timeHandler” class=”com.xrq.aop.TimeHandler” /
aop:config
aop:aspect id=”time” ref=”timeHandler”
aop:pointcut id=”addAllMethod” expression=”execution(* com.xrq.aop.HelloWorld.*(..))” /
aop:before method=”printTime” pointcut-ref=”addAllMethod” /
aop:after method=”printTime” pointcut-ref=”addAllMethod” /
/aop:aspect
/aop:config/beans
寫一個main函數調用一下:
public static void main(String[] args)
{
ApplicationContext ctx =
new ClassPathXmlApplicationContext(“aop.xml”);
HelloWorld hw1 = (HelloWorld)ctx.getBean(“helloWorldImpl1”);
HelloWorld hw2 = (HelloWorld)ctx.getBean(“helloWorldImpl2”);
hw1.printHelloWorld();
System.out.println();
hw1.doPrint();
System.out.println();
hw2.printHelloWorld();
System.out.println();
hw2.doPrint();
}
運行結果為:
CurrentTime = 1446129611993Enter HelloWorldImpl1.printHelloWorld()
CurrentTime = 1446129611993CurrentTime = 1446129611994Enter HelloWorldImpl1.doPrint()
CurrentTime = 1446129611994CurrentTime = 1446129611994Enter HelloWorldImpl2.printHelloWorld()
CurrentTime = 1446129611994CurrentTime = 1446129611994Enter HelloWorldImpl2.doPrint()
CurrentTime = 1446129611994
看到給HelloWorld接口的兩個實現類的所有方法都加上了代理,代理內容就是打印時間
基於Spring的AOP使用其他細節
1、增加一個橫切關注點,打印日誌,Java類為:
public class LogHandler
{ public void LogBefore()
{
System.out.println(“Log before method”);
}
public void LogAfter()
{
System.out.println(“Log after method”);
}
}
?xml version=”1.0″ encoding=”UTF-8″?beans xmlns=””
xmlns:xsi=””
xmlns:aop=””
xmlns:tx=””
xsi:schemaLocation=”
“
bean id=”helloWorldImpl1″ class=”com.xrq.aop.HelloWorldImpl1″ /
bean id=”helloWorldImpl2″ class=”com.xrq.aop.HelloWorldImpl2″ /
bean id=”timeHandler” class=”com.xrq.aop.TimeHandler” /
bean id=”logHandler” class=”com.xrq.aop.LogHandler” /
aop:config
aop:aspect id=”time” ref=”timeHandler” order=”1″
aop:pointcut id=”addTime” expression=”execution(* com.xrq.aop.HelloWorld.*(..))” /
aop:before method=”printTime” pointcut-ref=”addTime” /
aop:after method=”printTime” pointcut-ref=”addTime” /
/aop:aspect
aop:aspect id=”log” ref=”logHandler” order=”2″
aop:pointcut id=”printLog” expression=”execution(* com.xrq.aop.HelloWorld.*(..))” /
aop:before method=”LogBefore” pointcut-ref=”printLog” /
aop:after method=”LogAfter” pointcut-ref=”printLog” /
/aop:aspect
/aop:config/beans
測試類不變,打印結果為:
CurrentTime = 1446130273734
Log before method
Enter HelloWorldImpl1.printHelloWorld()
Log after method
CurrentTime = 1446130273735
CurrentTime = 1446130273736
Log before method
Enter HelloWorldImpl1.doPrint()
Log after method
CurrentTime = 1446130273736
CurrentTime = 1446130273736
Log before method
Enter HelloWorldImpl2.printHelloWorld()
Log after method
CurrentTime = 1446130273736
CurrentTime = 1446130273737
Log before method
Enter HelloWorldImpl2.doPrint()
Log after method
CurrentTime = 1446130273737
要想讓logHandler在timeHandler前使用有兩個辦法:
(1)aspect裡面有一個order屬性,order屬性的數字就是橫切關注點的順序
(2)把logHandler定義在timeHandler前面,Spring默認以aspect的定義順序作為織入順序
2、我只想織入接口中的某些方法
修改一下pointcut的expression就好了:
?xml version=”1.0″ encoding=”UTF-8″?beans xmlns=””
xmlns:xsi=””
xmlns:aop=””
xmlns:tx=””
xsi:schemaLocation=”
“
bean id=”helloWorldImpl1″ class=”com.xrq.aop.HelloWorldImpl1″ /
bean id=”helloWorldImpl2″ class=”com.xrq.aop.HelloWorldImpl2″ /
bean id=”timeHandler” class=”com.xrq.aop.TimeHandler” /
bean id=”logHandler” class=”com.xrq.aop.LogHandler” /
aop:config
aop:aspect id=”time” ref=”timeHandler” order=”1″
aop:pointcut id=”addTime” expression=”execution(* com.xrq.aop.HelloWorld.print*(..))” /
aop:before method=”printTime” pointcut-ref=”addTime” /
aop:after method=”printTime” pointcut-ref=”addTime” /
/aop:aspect
aop:aspect id=”log” ref=”logHandler” order=”2″
aop:pointcut id=”printLog” expression=”execution(* com.xrq.aop.HelloWorld.do*(..))” /
aop:before method=”LogBefore” pointcut-ref=”printLog” /
aop:after method=”LogAfter” pointcut-ref=”printLog” /
/aop:aspect
/aop:config/beans
表示timeHandler只會織入HelloWorld接口print開頭的方法,logHandler只會織入HelloWorld接口do開頭的方法
3、強制使用CGLIB生成代理
前面說過Spring使用動態代理或是CGLIB生成代理是有規則的,高版本的Spring會自動選擇是使用動態代理還是CGLIB生成代理內容,當然我們也可以強制使用CGLIB生成代理,那就是aop:config裡面有一個”proxy-target-class”屬性,這個屬性值如果被設置為true,那麼基於類的代理將起作用,如果proxy-target-class被設置為false或者這個屬性被省略,那麼基於接口的代理將起作用
spring已經實現了aop,為什麼還要用“aspectj”?
根據我看spring官方文檔的理解(不出意外是最正確的答案):
①選擇spring的AOP還是AspectJ?
spring確實有自己的AOP。功能已經基本夠用了,除非你的要在接口上動態代理或者方法攔截精確到getter和setter。這些都是寫奇葩的需求,一般不使用。
②在使用AOP的時候,你是用xml還是註解的方式(@Aspect)?
1)如果使用xml方式,不需要任何額外的jar包。
2)如果使用@Aspect方式,你就可以在類上直接一個@Aspect就搞定,不用費事在xml里配了。但是這需要額外的jar包( aspectjweaver.jar)。因為spring直接使用AspectJ的註解功能,注意只是使用了它 的註解功能而已。並不是核心功能 !!!
注意到文檔上還有一句很有意思的話:文檔說到 是選擇spring AOP還是使用full aspectJ?
什麼是full aspectJ?如果你使用”full aspectJ”。就是說你可以實現基於接口的動態代理,等等強大的功能。而不僅僅是aspectj的 注-解-功-能 !!!
如果用full AspectJ。比如說Load-Time Weaving的方式 還 需要額外的jar包 spring-instrument.jar
當然,無論是使用spring aop還是 aspectj都需要aspectjweaver.jar spring-aop.jar這兩個jar包。
spring添加aspectjweaver包後依舊報錯
從英文角度來說:就是找不到這個類
你導入的是包,就意味着你這個包是有這個類,哪怕版本不匹配也是有這個類
那麼應該是你導入的路徑錯誤,或者你沒有導入
導入過程請參考:
缺少aspectjweaver-1.6.5.jar包會報什麼錯誤
Spring缺少aspectjweaver.jar異常
1、異常信息:org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException;
2、異常原因:缺少aspectjweaver.jar這個包,該包是spring集成AspectJ LTW織入器所需包;
3、解決方案:在工程中加入 aspectjweaver.jar 包。
異常代碼
java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
at orre.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:136)
at orntext.annotation.ConfigurationClassBeanDefinitionReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader.java:318)
at orntext.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:175)
at orntext.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:144)
at orntext.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
at orntext.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframeworntext.ContextLoader.createWebApplicationContext(ContextLoader.java:282)
at org.springframeworntext.ContextLoader.initWebApplicationContext(ContextLoader.java:204)
at org.springframeworntext.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at orre.StandardContext.listenerStart(StandardContext.java:4205)
at orre.StandardContext.start(StandardContext.java:4704)
at orre.ContainerBase.addChildInternal(ContainerBase.java:799)
at orre.ContainerBase.addChild(ContainerBase.java:779)
at orre.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at orre.ContainerBase.start(ContainerBase.java:1061)
at orre.StandardHost.start(StandardHost.java:840)
at orre.ContainerBase.start(ContainerBase.java:1053)
at orre.StandardEngine.start(StandardEngine.java:463)
at orre.StandardService.start(StandardService.java:525)
at orre.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
… 36 more
Pom解決方案代碼
dependency$amp;amp;$nbsp;
groupIdorg.aspectj/groupId$amp;amp;$nbsp;
artifactIdaspectjweaver/artifactId$amp;amp;$nbsp;
version1.6.12/version$amp;amp;$nbsp;
/dependency$amp;amp;$nbsp;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/306236.html