Jigar Naik
Jigar Naik

Reputation: 1994

IntelliJ Idea + Could not autowire. No beans of type found

I keep seeing below error in my IntelliJ Idea, however the code works fine during execution.

Could not autowire. No beans of 'PortfolioRequestHandler' type found. less... (Ctrl+F1) 
Inspection info:Checks autowiring problems in a bean class.

Sample Code

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest(classes = {Application.class})
public class PortfolioRequestHandlerTest {

    @Autowired
    private PortfolioRequestHandler portfolioRequestHandler;

    ...
    ...
}

How do I get rid of this? I am using IntelliJ Idea ULTIMATE 2018.2

Upvotes: 4

Views: 23972

Answers (2)

Eduardo Toural
Eduardo Toural

Reputation: 79

If by any chance PortfolioRequestHandler is declared in a different module, you can let Spring to scan and find your Bean(s) specifying the package path. i.e.

@SpringBootTest
@ComponentScan(basePackages = "PATH TO THE MODULE WHERE YOUR BEAN IS DECLARED") {

}

Upvotes: 0

Tommy Brettschneider
Tommy Brettschneider

Reputation: 1490

Are you sure that your Spring beans are wired correctly and that it's an IDE problem?

  1. check if your PortfolioRequestHandler class is annotated with @Service, @Component or @Repository (bean config via component scanning)

  2. otherwise check if your bean is wired in a @Configuration annotated class -> in that case there should be a method that returns an instance of type PortfolioRequestHandler and that's annotated with @Bean

  3. try adding a configuration class (as mentioned in 2.) and add this class to your @SpringBootTest(classes = {...} annotation; see example below

@Configuration
public class CustomBeanConfig {

   @Bean
   public PortfolioRequestHandler get PortfolioRequestHandler() {
       return new PortfolioRequestHandler();
   }
}

@SpringBootTest(classes = {Application.class, CustomBeanConfig.class})

  1. have a look at this one, maybe helps: https://stackoverflow.com/a/50267869/150623

Upvotes: 8

Related Questions