- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在创建一个简单的 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");
}
}
计算器.java
package program;
import org.springframework.stereotype.Component;
@Component
public class Calculator {
public int add(int i, int j) {
return i + j;
}
}
配置.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.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)
根据我的程序,我没有任何循环依赖,那么是什么导致了这个异常。
另外,如果我对我的代码做一些小改动,那么它工作正常。以下是我为使其正常工作所做的更改:
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.
}
最佳答案
我尝试使用帖子中提到的同一段代码,并在 Config.java 中定义了 Bean,它对我有用。我在使用这个程序时遇到的唯一错误是在 MyAspect 类中
'advice must be public'
@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();
关于java - 基本 AOP 程序抛出 BeanCurrentlyInCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984258/
我有以下 java 配置类: @Configuration public class MyConfig { @Autowired private List myInterfaces;
我该如何解决这个异常? 没有 Spring Boot 一切正常。 所以问题出在“应用”部分,对吗? 捕获异常 BeanCurrentlyInCreationException: Error creat
我正在开发我的 Spring Boot 应用程序,以将 Spring Security 与数据库身份验证集成。我对 Spring 相当陌生,并且不断遇到异常。我只是在关注这个教程https://www
创建新按钮我必须在新线程中运行代码。 通常我们使用 new Thread(....).start(); 但我想知道为什么我们不能使用 @Async-Annotation。 这是代码: package
我正在创建一个简单的 AOP 程序并开始获取 BeanCurrentlyInCreationException 异常。 这是我的代码: MyAspect.java package aspect; im
当我尝试连接配置类上的 channel 时,出现 BeanCurrentlyInCreationException 异常。我只是指Documentation这里。如果我理解有误,请指正。我使用 spr
我想在抽象类上有计划注释,以便子类不需要有注释。抽象类中有类似的东西 @EnableScheduling @SpringBootApplication public class DemoApplica
我正在尝试学习这个 spring-lemon 入门教程 ( https://naturalprogrammer.gitbooks.io/spring-lemon-getting-started/con
@Configuration public class Test1 { @Autowired private Test3 test3; } @Configuration public
在开发springboot项目的时候,就遇到了上面的错误。奇怪的是,项目在IDEA中运行良好,却报出运行为jar的异常。更奇怪的是,我的同事竟然可以打包一个无异常的jar!所以我认为是我自己的电脑环境
旧代码: @Component("someFactory") public class SomeFactoryImpl implements SomeFactory{ @Autowired
我正在使用 spring 和 hibernate 来配置 mysql db。 我的 we.xml 文件有以下代码: org.springframework.web.con
我是一名优秀的程序员,十分优秀!