內(nèi)容回顧 | spring零配置AOP踩坑指南

,而且由于早上吃的有點(diǎn)多,所以晚上都不想吃飯了
在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。
除了spring的jar包以外,還需要導(dǎo)入以下包:

1、Spring核心配置文件beans.xml(這里是大坑)
<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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!-- 啟動@AspectJ支持 --><!-- <beanclass="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> --><context:component-scanbase-package="com.sysker.bean,com.sysker.aspect"><context:include-filter type="annotation"expression="org.aspectj.lang.annotation.Aspect" /></context:component-scan><!-- 設(shè)置AOP為自動代理 --><aop:aspectj-autoproxy /></beans>
如果全部采用注解的話,bean是不需要在這里配置的,只需要在對應(yīng)的類上面寫上@Component("name")注解即可,前面不清楚,所以我在這個(gè)坑里呆了很久,書上也沒說,自己也沒去查
這里的另外一個(gè)坑就是這里:
<context:component-scanbase-package="com.sysker.bean,com.sysker.aspect"><context:include-filter type="annotation"expression="org.aspectj.lang.annotation.Aspect" /></context:component-scan>
如果這里路徑配置有問題的話,會提示找不到bean,錯(cuò)誤信息大概如下:
Loading XML bean definitions from class path resource [beans.xml]Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hello' availableat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686)at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1095)at com.sysker.test.AspectJTest.main(AspectJTest.java:22)
base-package里面可以配置多個(gè)路徑,一定要完整
2、 Aspect類(這里有另外的大坑)
package com.sysker.aspect;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;public class AuthAspect {public void authority() {System.out.println("模擬執(zhí)行權(quán)限檢查");}}
這里就有另外一個(gè)大坑在等著你,主要會出錯(cuò)的地方還是路徑的問題:
路徑必須正確,否則是沒有效果了,比如我的路徑,impl下的實(shí)現(xiàn)類要實(shí)現(xiàn)AOP,那么我的路徑是要配到impl,然后還要有.. ,不然就是各種無效果,而且還不報(bào)錯(cuò); 還要提一下,Aspect必須要注解,注解,注解,就這樣的@Aspect,然后后面的@Before("execution(* com.sysker.impl..(..))"),就是要執(zhí)行的操作 如果要針對某一個(gè)類,比如HelloImpl類,這需要寫出具體的類:
如果要針對某一個(gè)的具體方法,就需要寫出具體的類的方法:
3、service及Impl
package com.sysker.bean;public interface Hello {void foo();int addUser(String name, String pass);}package com.sysker.bean;public interface World {public void bar();}
service是不要注解的,也沒必要
package com.sysker.impl;import org.springframework.stereotype.Component;import com.sysker.bean.Hello;("hello")public class HelloImpl implements Hello {public void foo() {System.out.println("執(zhí)行Hello組件的foo()方法");}public int addUser(String name, String pass) {System.out.println("執(zhí)行Hello組件的addUser添加用戶:" + name);return 20;}}package com.sysker.impl;import org.springframework.stereotype.Component;import com.sysker.bean.World;("world")public class WorldImpl implements World {public void bar() {System.out.println("執(zhí)行World組件的bar()方法");}}
這里需要注意的就是上面提到的,要注解@Component("world"),后面通過Spring容器創(chuàng)建對象要用到 4、測試類
正常運(yùn)行結(jié)果:

總結(jié)
推薦閱讀
Copyright ?2021 云中志
評論
圖片
表情
