Spring - java.lang.IllegalArgumentException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23024907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:07:26  来源:igfitidea点击:

Spring - java.lang.IllegalArgumentException

javaspringaopillegalargumentexception

提问by Jeff Levine

This is driving me nuts - I've researched and found posts with similar errors, but none of the solutions seem to be working for me.

这让我发疯 - 我已经研究并发现了有类似错误的帖子,但似乎没有一个解决方案对我有用。

Output:

输出:

Apr 11, 2014 8:59:07 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3bb2b8: startup date     
[Fri Apr 11 20:59:07 EDT 2014]; root of context hierarchy
Apr 11, 2014 8:59:07 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Apr 11, 2014 8:59:08 PM     
org.springframework.beans.factory.support.DefaultListableBeanFactory     preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@104c575: defining beans [departmentDAO,departmentService,securityAspect,org.springframework.aop.config.internalAuto    ProxyCreator]; root of factory hierarchy
Apr 11, 2014 8:59:08 PM     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in rg.springframework.beans.factory.support.DefaultListableBeanFactory@104c575: defining beans     [departmentDAO,departmentService,securityAspect,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'departmentDAO' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is     java.lang.IllegalArgumentException: warning no match for this type name:     coreservlets.aopaspj.service.DepartmentService [Xlint:invalidAbsoluteTypeName]
at     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at coreservlets.aopaspj.ExerciseRunner.main(ExerciseRunner.java:11)
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: coreservlets.aopaspj.service.DepartmentService [Xlint:invalidAbsoluteTypeName]
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:317)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:204)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:191)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:172)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:198)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:252)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:284)
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:359)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:404)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
... 11 more

SecurityAspect.java

安全方面

package coreservlets.aopaspj.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import coreservlets.aopaspj.exception.InvalidEmployeeException;

@Aspect
public class SecurityAspect {
    @SuppressWarnings("unused")
    @Pointcut("execution(* coreservlets.aopaspj.service.DepartmentService.getDepartment(..) )")
    private void getDepartmentPointcut(){}

    @Before("getDepartmentPointcut() && args(employeeId, lastName)")
        public void verifyEmployee(long employeeId, String lastName) throws InvalidEmployeeException {
            if (!((employeeId == 1) && lastName.equals("Smith") || (employeeId == 2) && lastName.equals("Jones"))) {
                throw new InvalidEmployeeException();
            }

        }

}

applicationContext.xml

应用上下文.xml

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

<bean id="departmentDAO" class="coreservlets.aopaspj.dao.DepartmentDAOImpl" />

<bean id="departmentService"
    class="coreservlets.aopaspj.service.DepartmentServiceImpl">
    <property name="departmentDAO" ref="departmentDAO" />
</bean>

<bean id="securityAspect" class="coreservlets.aopaspj.aspect.SecurityAspect" />

<aop:aspectj-autoproxy/>

ExerciseRunner.java

练习跑者.java

package coreservlets.aopaspj;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import coreservlets.aopaspj.service.DepartmentServiceImpl;

public class ExerciseRunner {
public static void main(String[] args) {

    ApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DepartmentServiceImpl departmentService = 
            springContext.getBean("departmentService", DepartmentServiceImpl.class);

    long employeeId = 1;
    String lastName = "Smith";

    System.out.println(departmentService.getDepartment(employeeId, lastName));

    }
}

采纳答案by kriegaex

Well, the callstack is telling you the reason:

好吧,调用堆栈告诉你原因:

Initialization of bean failed; nested exception is
    java.lang.IllegalArgumentException: warning no match for this type name:
    coreservlets.aopaspj.service.DepartmentService [Xlint:invalidAbsoluteTypeName]

To me it looks as if you use a class name like Fooin one of your aspect pointcuts, but Spring-AOP expects a fully-qualified one like com.blah.Foo. As you have not posted your aspect code, this is hard to tell. I am not a Spring user, but it is a common pitfall in AspectJ too, especially if you do not use native syntax (where you can use regular imports to shorten class names in pointcuts) but annotation-style syntax. The latter wants fully-qualified class names.

对我来说,看起来好像你Foo在一个方面切入点中使用了一个类名,但 Spring-AOP 需要一个像com.blah.Foo. 由于您尚未发布方面代码,因此很难说。我不是 Spring 用户,但它也是 AspectJ 中的一个常见陷阱,特别是如果您不使用本机语法(您可以使用常规导入来缩短切入点中的类名)而是使用注释样式的语法。后者需要完全限定的类名。