Spring AOP error at ::0 formal unbound in pointcut

阅读更多

Spring4.2.4 使用XML配置方式实现AOP切入时出现错误:error at ::0 formal unbound in pointcut

测试代码如下:

 

public interface IUserService {

    public void login(String name, String pwd);

    public void setAdministratorUserName(String name);
}

public interface IStockService {

    public long getStockLevel(String level);

    public void applyDiscounts(Date date, BigDecimal bigDecimal);
}

public class DefaultUserService implements IUserService {

    private String administratorUserName;
    public DefaultUserService() {
    }

    public void login() {
    }

    public void login(String name) {
        SecurityContext.setUserName(name);
    }

    public void login(String name, String pwd) {
        SecurityContext.setUserName(name);
    }

    public String getAdministratorUserName() {
        return this.administratorUserName;
    }

    public void setAdministratorUserName(String name) {
        this.administratorUserName = name;
    }
}

public class DefaultStockService implements IStockService {

    public long getStockLevel(String level) {
        try {
            if(level == null) {
                    throw new IOException("stock level not null");
            }
        } catch (IOException e) {
                e.printStackTrace();
        }
        return 1;
    }

    public void applyDiscounts(Date date, BigDecimal bigDecimal) {
        System.out.println(date + " >> " + bigDecimal.byteValue());
    }
}
public class SecurityContext {

    private static SecurityContextHolder holder = new SecurityContextHolder();

    public static String getUserName() {
        return holder.getUserName();
    }

    public static void setUserName(String userName) {
        holder.setUserName(userName);
    }

    private static class SecurityContextHolder {

        private ThreadLocal currentUser = new ThreadLocal();
        public String getUserName() {
            return currentUser.get();
        }

        public void setUserName(String userName) {
            currentUser.set(userName);
        }
    }
}

 

public class AspectBean {

    public void logCall(Joinpoint joinpoint) {
        System.out.println("--------------------");
        System.out.println(joinpoint);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/ch6/aspectBean.xml");
        IUserService userService = (DefaultUserService) ctx.getBean("userService");
        IStockService stockServic = (DefaultStockService) ctx.getBean("stockService");
        userService.login("admin", "****");
        stockServic.getStockLevel("a");
    }
}

 




    
    
    

    
        
        
        
        
            
        
    

 
错误日志:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [conf/ch6/aspectBean.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [conf/ch6/aspectBean.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:478)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)

at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)

at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)

at com.ycx.ch6.section3.AspectBean.main(AspectBean.java:25)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:275)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)

at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)

at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)

at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:248)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:988)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:959)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:472)

... 10 more

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)

at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267)

... 25 more

Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut 

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.checkReadyToMatch(AspectJExpressionPointcut.java:193)

at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:176)

at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:187)

at org.springframework.aop.aspectj.AspectJPointcutAdvisor.(AspectJPointcutAdvisor.java:51)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

 

 注:经过debug Spring使用annotation注解时使用的是aopalliance.jar中的Joinpoint接口声明

org.aopalliance.intercept.Joinpoint;

而xml配置时是用的aspectjrt.jar中的Joinpoint接口声明

org.aspectj.lang.JoinPoint;

 

修改后的代码:

 

public class AspectBean {
    // import org.aopalliance.intercept.Joinpoint;   // wrong interface import
    import org.aspectj.lang.JoinPoint                // right interface import
    public void logCall(org.aspectj.lang.JoinPoint joinpoint) {
        System.out.println("--------------------");
        System.out.println(joinpoint);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/ch6/aspectBean.xml");
        IUserService userService = (DefaultUserService) ctx.getBean("userService");
        IStockService stockServic = (DefaultStockService) ctx.getBean("stockService");
        userService.login("admin", "****");
        stockServic.getStockLevel("a");
    }
}
 

 

 

你可能感兴趣的:(Spring AOP error at ::0 formal unbound in pointcut)