question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static inner class is not identified by @Autowired

See original GitHub issue

Kindly please let me know why the below code snippet does not work with 2.4.3 after upgrade from 1.5.9

@Component
public class test {     

      @Autowired
      LocationRegistry locReg;

     @Component
     public static class LocationRegistry {

        private final ConcurrentMap<String, Path> locationPerRequest = new ConcurrentHashMap<>();

        ConcurrentMap<String, Path> getLocationPerRequest() {
            return locationPerRequest;
        }
 
   }

}

Which was working with 1.5.x is not working with 2.4.3 please use the stacktrace below

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.recommind.AgreementCodeBasedFieldsAutoTest': Unsatisfied dependency expressed through field 'locationRegistry'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.xxxBasedFieldsAutoTest$LocationRegistry' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:399)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
    at com.xxx.DatabaseCreatingListener.injectDependencies(DatabaseCreatingListener.java:24)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:366)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:320)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.testng.TestRunner.privateRun(TestRunner.java:764)
    at org.testng.TestRunner.run(TestRunner.java:585)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mdeinumcommented, Mar 24, 2021

Running the sample class works with Spring Boot 2.4.3/2.4.4 as well as 1.5.x. as well as a test using jUnit5 and TestNG.

Everything seems to run. Looks like an issue with the test setup used in the project.

@pasumarthivijaykumar If there is anything in the zip different from your setup please let it know.

autowire-inner-class.zip

0reactions
spring-projects-issuescommented, Apr 7, 2021

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Anyway to @Inject/@Autowire an inner class into an outer ...
As an annotated @Component public static class, Spring complains that it find two beans - TestClass@TestClassInner and testClass.TestClassInner.
Read more >
Java – @Autowired not working in inner class - iTecNote
I have a class being @Autowired in inner class. But while executing it throws a Null Pointer Exception, whereas it works fine when...
Read more >
No longer possible to use inner class bean annotated ... - GitHub
After upgrading from Spring Boot 2.2.0.RELEASE to 2.2.1.RELEASE the following configuration class no longer works: import ...
Read more >
How to use the @TestConfigurations annotations using static ...
In this shot, we will focus on how to use @TestConfigurations annotations using a static inner class. Using @TestConfiguration with a static inner...
Read more >
Core Technologies - Spring
The class attribute defines the type of the bean and uses the fully qualified class name. The value of the id attribute can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found