2015-08-13 30 views
3

我正在創建一個簡單的AOP程序,並開始得到BeanCurrentlyInCreationException異常。基本的AOP程序拋出BeanCurrentlyInCreationException

這裏是我的代碼:

MyAspect.java

package aspect; 

import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 

@Aspect 
//@Component 
public class MyAspect { 

    @Pointcut("execution(public * *(..))") 
    private void anyPublicOperation() { 
    } 

    @Before("anyPublicOperation()") 
    private void beforePointCut(){ 
     System.out.println("Inside before pointcut of MyAspect"); 
    } 
} 

Calculator.java

package program; 

import org.springframework.stereotype.Component; 

@Component 
public class Calculator { 

    public int add(int i, int j) { 
     return i + j; 
    } 
} 

Config.java

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 

import aspect.MyAspect; 


@Configuration 
//Enable AspectJ auto proxying 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages={"program"}) 
public class Config { 

    //Declare a bean 
    @Bean 
    public MyAspect aspect() { 
     return new MyAspect(); 
    } 
} 

App.java - 它包含的主要程序:

import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

import program.Calculator; 

public class App { 
    public static void main(String[] args) throws Exception { 

     ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); 

     System.out.println("=======Calling methods========"); 

     Calculator cal = ctx.getBean(Calculator.class); 
     int result = cal.add(10,20); 
     System.out.println(result); 
    } 
} 

如果我跑我的程序,我得到這個異常:

Aug 13, 2015 2:44:10 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 
INFO: Refreshing org.spring[email protected]17ab5d6d: startup date [Thu Aug 13 14:44:10 IST 2015]; root of context hierarchy 
Aug 13, 2015 2:44:10 PM org.springframework.context.annotation.AnnotationConfigApplicationContext refresh 
WARNING: Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aspect' defined in Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [aspect.MyAspect]: Factory method 'aspect' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'aspect': Requested bean is currently in creation: Is there an unresolvable circular reference? 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84) 
    at App.main(App.java:10) 

按我的節目,我不有任何循環依賴,那麼是什麼導致這個異常。

此外,如果我對我的代碼做了小的更改,那麼它工作正常。下面是我做,以使更改它的工作:

1)評論在Config.java代碼的bean聲明:

@Configuration 
//Enable AspectJ auto proxying 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages={"aspects","program"}) 
public class Config { 

    // bean declaration is removed here and updated basePackages for @ComponentScan 

} 

啓用@Component註釋上我的切面類是這樣的:

@Aspect 
@Component 
public class MyAspect { 

    // same as earlier code. 
} 

回答

0

我嘗試了與帖子中提到的相同的一段代碼,並在Config.java中定義了Bean,它對我很有用。我得到了使用該程序的唯一的錯誤是在MyAspect類 的

的意見必須是公開的「

@Before("anyPublicOperation()") 
public void beforePointCut(){ 
    System.out.println("Inside before pointcut of MyAspect"); 
} 

休息執行罰款。 您也可以在使用之前通過更新上下文來嘗試以前的代碼

ApplicationContext context = new AnnotationConfigApplicationContext(); 
((AnnotationConfigApplicationContext) context).register(Config.class); 
((AbstractApplicationContext) context).refresh(); 
相關問題