Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring changes finding beans from version 4.3 to 5

I have a large spring applications using annotations that works fine in spring 4.3.13, and am looking to update to spring 5. I am getting all kinds of failures wiring beans, which look like the typical:

Unsatisfied dependency expressed through field 'pcoDAO'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.acme.dao.impl.contracts.PotentialChangeOrderDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

turning on spring debug logging, only nets this additional line

Failed to meta-introspect annotation interface org.springframework.beans.factory.annotation.Autowired: java.lang.NullPointerException

It's not like all @Autowired fields fail, just this one (so far). The bean is specified by an interface, and the implementation is in a sub package of the interface, but again this worked before. The interface's package is specified directly in the context:component-scan base-package="com.acme.package.of.interface"

Again this works fine in 4.3.13, and the only change is spring being upgraded to 5.0.5-RELEASE.

Are they any known changes to how spring finds beans? or any documentation about this?

like image 577
MeBigFatGuy Avatar asked Apr 10 '18 20:04

MeBigFatGuy


People also ask

Which version of Spring is compatible with Spring Boot?

Spring Boot 2.7. 5 requires Java 8 and is compatible up to and including Java 19. Spring Framework 5.3. 23 or above is also required.

Can we use @component instead of @bean?

@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.

Can we configure two beans of the same class with the different ID in Spring?

If you define two beans of same class, without different bean id or qualifiers ( identifier) , Spring container will not be able to understand which bean to be loaded , if you try to access the bean by passing the classname and you will get NoUniqueBeanDefinitionException as there are two qualifying TestBean.

Is @bean a class level annotation?

@Bean is a method-level annotation and a direct analog of the XML <bean/> element. The annotation supports most of the attributes offered by <bean/> , such as: init-method , destroy-method , autowiring , lazy-init , dependency-check , depends-on and scope .


1 Answers

Seems a bug of Spring 5.0.5, and has been fixed in 5.0.6, see this Null check needed in AnnotationUtils.getAnnotation

like image 147
xingbin Avatar answered Sep 20 '22 17:09

xingbin