2014-02-10 77 views
9

我從來沒有使用Spring AOP並嘗試配置我的第一個bean。似乎我正確地配置了它,但是我得到了一個異常,發現這個bean沒有找到。java.lang.IllegalArgumentException:此類型名稱的警告不匹配:ru.sbt.filial.cards.aspect.SomeBean [Xlint:invalidAbsoluteTypeName]

我的方面是 -

@Aspect 
@Component 
public class IdentificationAspect { 

    @Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))") 
    public void logBefore(JoinPoint joinPoint) throws Throwable { 

     System.out.println("logBefore() is running!"); 
     System.out.println("hijacked : " + joinPoint.getSignature().getName()); 
     System.out.println("******"); 
    } 
} 

我的豆是AOP沒有發現是 -

package ru.sbt.filial.cards.aspect; 

import org.springframework.stereotype.Component; 

@Component 
public class SomeBean { 

    public void printSmth() { 
     System.out.println("!!!!!!!!!!!"); 
    } 

} 

我有以下異常 -

Caused by: java.lang.IllegalArgumentException: warning no match for this type name: ru.sbt.filial.cards.aspect.SomeBean [Xlint:invalidAbsoluteTypeName] 
       at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301) 
       at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207) 
       at org.springframework.aop.aspectj.AspectJExpressionPointcut.getFallbackPointcutExpression(AspectJExpressionPointcut.java:358) 
       at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:255) 
       at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208) 
       at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262) 
       at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294) 
       at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117) 
       at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87) 
       at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68) 
       at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:356) 
       at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:319) 
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:412) 
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1629) 
       at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:162) 
       ... 165 more 

我有遵循maven依賴關係 -

   <dependency> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-aop</artifactId> 
       <version>3.2.0.RELEASE</version> 
       </dependency> 
       <dependency> 
       <groupId>org.aspectj</groupId> 
       <artifactId>aspectjweaver</artifactId> 
       <version>1.7.3</version> 
       </dependency> 
       <dependency> 
       <groupId>org.aspectj</groupId> 
       <artifactId>aspectjrt</artifactId> 
       <version>1.7.3</version> 
       </dependency> 

我的春天的applicationContext.xml配置 -

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd 
         http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd 
         http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd 
         http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

<mvc:annotation-driven/> 
<aop:aspectj-autoproxy/> 

<bean id="someBean" class="ru.sbt.filial.cards.aspect.SomeBean"> 
</bean> 

UPDATE 我加入到我的Spring配置線

<context:component-scan base-package="ru.sbt.filial.cards.aspect"/> 

但還是同樣的信息。我也試圖詮釋不同的方式 - 如果我不指定bean即寫 -

@Before("execution(* ru.sbt.filial.cards.aspect.*.*(..))") 

代替

@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))") 

我對裝載沒有錯誤,但不調用AOP方法。

我也試圖詮釋這樣

@Before("this(ru.sbt.filial.cards.aspect.SomeBean) and execution(* printSmth(..))") 

但同樣的結果 - 敵不過這種類型的名稱。還有什麼想法?

+0

您錯過了在配置文件 – Rembo

+0

中添加bean IdentificationAspect仍然不起作用? – Rembo

回答

0

要成功運行,在你的applicationContext.xml春天配置文件中加

<context:component-scan base-package="ru.sbt.filial.cards.aspect"/> 

當你使用SomeBean類@Component註釋

更新:

嘗試通過添加方法名@Before :

@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))") 
+0

我添加了,但仍然是相同的消息。我也試着用不同的方式註釋 - 如果我沒有指定bean名稱 - –

12

我有同樣的pr你和我解決了我的問題。我給你兩個建議,你可以試試看。 請修改切面類中@Before註解爲以下之一:

@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))")

@Before("execution(* ru.sbt.filial.cards.aspect.SomeBean..*(..))")

希望它能幫助。

+3

對於我來說,從「。*」更改爲「.. *」解決了我在JBoss 7中部署時遇到的問題。但是,您能解釋爲什麼「.. *」是否需要? (「。*」在我的獨立單元測試中工作得很好) – caprica

+1

@caprica我想第一個'。'是一個點本身,但第二''。是匹配任何字符的通配符,'*'表示匹配任何數字。所以'SomeBean .. *(..)'意味着像SomeBean.methodA(..)或SomeBean.methodB(..)一樣匹配'SomeBean'的所有方法。 – CDT

相關問題