If you are using the Spring framework in your Java application and getting this error during startup it means Spring is not able to initialize the bean X and add it into its application context, Why? There could be multiple reasons like a typo on the spring bean name. Let's take a closer look at the stack trace to find out the real reason: BeanInstantiationException: Could not instantiate bean class [X]: No default constructor found; nested exception is java.lang.NoSuchMethodException: X.() Here X is the class, which is declared as Spring bean. The error clearly says that the default constructor is not present in class X. If you remember, Java by default adds a default constructor in every class but many programmers don't know that it only does that if you don't have any constructor in the class if by any chance you have defined a constructor which takes a parameter, then Java will not add it. This is also one of the reasons I suggest always add a default constructor in the Java class, no matter whether you have more constructors or not. Java allows constructor overloading and you should take advantage of that. I also suggest you go through a comprehensive Spring online training course to understand how Spring works, how it instantiates objects, where does it keep the object reference, dependency injection, IOC container, and much more. If you need a recommendation then I suggest you join one of the courses in this list of best Spring courses for Java developers. It's one of the most up-to-date resources to learn in Spring and covers Spring 5.0 and Reactive Programming as well. How to solve Error creating bean with name X [Java Spring]Now, it depends on how you are instructing Spring to instantiate your bean? If you are using auto-wiring like @Autowired annotation, and you also have a suitable constructor define then just annotated that with @Autwired annotation, as shown below: public class X{ private D d; @Autowired public X(Y dependency){ this.d = depenency; } }Your problem will be solved. Btw, if you are using XML based configuration then make sure you are using the right constructor on your config. Also, as per Spring Java documentation, A BeanCrationException is thrown when a BeanFactory encounters an error while attempting to create a bean from bean definition. It doesn't say anything about the error, that's why you need to look at the nested exception to find the actual cause. For example, in this case, the nested error was BeanInstantiationException, which indicates the instantiation of a bean failed. You should always pay attention to detailed stack trace as It also carries the offending bean class and reason like in this case, it was trying to instantiate bean by invoking default constructor and couldn't find it. By looking at the error, you can conclude that whether you need to add a default constructor or Spring is invoking a wrong constructor, which means a missing config somehow. Sometimes BeanInstantiationException is also throwing in Spring is not able to find the bean class in the classpath. That time you will see either NoClassDefFoundError or ClassNotFoundException as a nested exception in the stack trace. Error creating bean with name 'reactiveMongoDatabaseFactory' defined in class path resource org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reactiveMongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6] 이렇게 길게 나오면서 안되는문제 gradle사용중인데, 라이브러리를 사용해놓고, @Configuration를 등록하지 않으면 스프링이 자동으로 빈을 등록하려고 한다. 그런데 설정해둔 파일이 없으니 에러가 발생한다. 또는 primary 설정이 안되어있을때 발생 @EnableAutoConfiguration(exclude={MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class}) 이런식으로 자동설정을 빼줘야 문제 없다. https://cnpnote.tistory.com/entry/SPRING-%EB%B4%84-%EB%B6%80%ED%8C%85-Exclude%EA%B0%80-%EC%9E%91%EB%8F%99%ED%95%98%EC%A7%80-%EC%95%8A%EB%8A%94-EnableAutoConfiguration https://stackoverflow.com/questions/28158094/spring-boot-enableautoconfiguration-with-exclude-not-working 저작자표시 'Web > SpringBoot' 카테고리의 다른 글
|