在类路径资源中定义名称为“as”的bean时出错

时间:2014-10-30 17:46:38

标签: java spring aop spring-aop

我有一个帐户业务服务,我在其中编写业务要求。对于该商业服务,我有一个SecurityCheck的建议类

package com.jlcindia.spring.bservices;

public class AccountService {
    public void getBal() {
        System.out.println("Get bal is successfull");
    }
    public void myDeposit() {
        System.out.println("MY Deposit is successfull");
    }
    public void myWithdraw() throws Exception {
        System.out.println("WIthdraw begin");
        int i=0;
        if (i == 1){
            throw new InSufficientFundsException();
        }
        System.out.println("WIthdraw end");
    }

}

建议类如下:

package com.jlcindia.spring.mservices;

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

@Aspect
public class SecurityService {

    @Pointcut(value="execution(* com.jlcindia.spring.bservices.AccountService.my*(..))")
    public void pc1(){}
    @Pointcut(value="execution(* com.jlcindia.spring.bservices.CustomerService.del*(..))")
    public void pc2(){}

    @Around("pc1() or pc2()")
    public void verifyUser(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("SS-verifyUser()-Begin");
        pjp.proceed();
        System.out.println("SS-verifyUser()-END");
    }
}

我使用 annotation-config.xml 文件来配置Business Service和Advice类,如下所示:

<bean id="as" class="com.jlcindia.spring.bservices.AccountService" />
<bean id="security" class="com.jlcindia.spring.mservices.SecurityService"/>

以下是测试它的代码:

package com.jlcindia.spring.aop;

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

import com.jlcindia.spring.bservices.AccountService;

public class Client {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
        AccountService ast = (AccountService)ctx.getBean("as");
        ast.myDeposit();
        ast.getBal();
        try{
            ast.myWithdraw();
        }catch(Exception e){
            System.out.println("***sorry****");
        }
    }
}

当我运行我的客户端代码来测试服务时,建议是否适用我得到了这个例外;

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'as' defined in class path resource [application-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut pc1

完整堆栈跟踪是:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'as' defined in class path resource [application-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut pc1
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.jlcindia.spring.aop.Client.main(Client.java:11)
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut pc1
    at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:316)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:195)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:181)
    at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:162)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:200)
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:254)
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:286)
    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:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 11 more</h3>

请帮助弄清楚为什么在创建bean'as'(AccountService)时发生此错误。我对AOP知之甚少,我现在学习了...所以请在这个问题上需要一些帮助。感谢您耐心阅读本文...

1 个答案:

答案 0 :(得分:1)

切入点中存在语法错误。请更换

@Around("pc1() or pc2()")

@Around("pc1() || pc2()")

PS:请在下次使用较少混乱的源代码和散文文本格式,我花了5分多钟时间编辑你的问题,以使其对我自己和他人更具可读性。