这让我抓狂-我研究并发现了有类似错误的帖子,但似乎没有一个解决方案对我有效。
输出:
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$1.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 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
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));
}
}
调用堆栈告诉您原因:
Initialization of bean failed; nested exception is
java.lang.IllegalArgumentException: warning no match for this type name:
coreservlets.aopaspj.service.DepartmentService [Xlint:invalidAbsoluteTypeName]
在我看来,您似乎在一个方面切入点中使用了Foo
这样的类名,但SpringAOP期望使用com.blah.Foo
这样的完全限定类名。由于您还没有发布方面代码,这很难说。我不是Spring用户,但这也是AspectJ中的一个常见缺陷,尤其是如果您不使用本机语法(可以使用常规导入来缩短切入点中的类名),而是使用注释样式语法。后者想要完全限定的类名。