基本AOP程序抛出BeanCurrentlyInCreationException [英] Basic AOP program throws BeanCurrentlyInCreationException
问题描述
我正在创建一个简单的AOP程序,并开始用它来获取 BeanCurrentlyInCreationException
异常。
I am creating a simple AOP program and starting getting BeanCurrentlyInCreationException
exception with it.
这是我的代码:
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 - 它包含主程序:
App.java - It contains the main program:
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);
}
}
如果我运行程序,我会收到此异常:
If I run my program, I am getting this exception:
Aug 13, 2015 2:44:10 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@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)
根据我的程序,我没有任何循环依赖,然后导致此异常的原因。
As per my program I do not have any circular dependency, then what is causing this exception.
此外,如果我对我的代码进行小的更改,那么它我工作正常。以下是我为使其工作所做的更改:
Also if I do small changes to my code, then it is working fine. Here are the changes that I have done to make it work:
1)在Config.java代码中注释bean声明:
1) Commented the bean declaration in Config.java code:
@Configuration
//Enable AspectJ auto proxying
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"aspects","program"})
public class Config {
// bean declaration is removed here and updated basePackages for @ComponentScan
}
在我的方面类上启用 @Component
注释,如下所示:
Enabled @Component
annotation on my aspect class like this:
@Aspect
@Component
public class MyAspect {
// same as earlier code.
}
推荐答案
我试过同样的作品在帖子中提到的代码并在Config.java中定义了Bean,它对我有用。我使用这个程序的唯一错误是在MyAspect类中
I tried with the same piece of code mentioned in the post and defined the Bean in Config.java and it worked for me. The only error i got using this program is in the MyAspect class the
'建议必须公开'
'advice must be public'
@Before("anyPublicOperation()")
public void beforePointCut(){
System.out.println("Inside before pointcut of MyAspect");
}
休息执行正常。
此外,您可以在使用前刷新上下文来尝试以前的代码
Rest executed fine. Also you could try the previous code by refreshing the context before using
ApplicationContext context = new AnnotationConfigApplicationContext();
((AnnotationConfigApplicationContext) context).register(Config.class);
((AbstractApplicationContext) context).refresh();
这篇关于基本AOP程序抛出BeanCurrentlyInCreationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!