Spring – Unable to autowire dozer Mapper in component class in spring boot

autowireddozerspringspring-boot

I am new to Spring Boot. I was trying to develop small application in spring boot mvc with neo4j database. Following is my Server

@Configuration
@ComponentScan({ "myproject" })
@EnableAutoConfiguration
@EnableNeo4jRepositories(basePackages = "myproject")
public class Server extends Neo4jConfiguration implements CommandLineRunner
{

public Server()
{
    setBasePackage("myproject");
}

@Bean
SpringRestGraphDatabase graphDatabaseService()
{
    return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}

@Bean
Mapper mapper()
{
    return new DozerBeanMapper();
}

public void run(String... args) throws Exception
{
}

public static void main(String[] args) throws Exception
{
    SpringApplication.run(Server.class, args);
}

}

Following is my main class

@Configuration
@ComponentScan({ "myproject.business" })
@EnableNeo4jRepositories(basePackages = "myproject")
public class MainWithStructure extends Neo4jConfiguration implements CommandLineRunner
{
@Autowired
private MyService myService;

public MainWithStructure()
{
    setBasePackage("myproject");
}

@Bean
SpringRestGraphDatabase graphDatabaseService()
{
    return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}

.......
......
public static void main(String[] args) throws Exception
{
    FileUtils.deleteRecursively(new File("accessingdataneo4j.db"));

    SpringApplication app = new SpringApplication(MainWithStructure.class);
    app.setWebEnvironment(false);
    app.run(args);
}

}

Following is my Component class

@Component
public class MyService
{

@Autowired
private Mapper mapper;  //Fails to autowire org.dozer.Mapper

.....
}

Following is my Controller class

@RestController
@RequestMapping("/rest")
public class MyController
{
@Autowired
private Mapper mapper;  //autowire sucess org.dozer.Mapper
}

I got following Exception when I run main class MainWithStructure

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainWithStructure': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myproject.business.MyService myproject.main.MainWithStructure.MyService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.dozer.Mapper myproject.business.MyService.mapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.dozer.Mapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Following is my project structure

demo_project

  • src/main/java

—myproject.main

——MainWithStructure.java

——Server.java

—myproject.business

——MyService.java

—myproject.rest

——MyController.java

If I autowire org.dozer.Mapper in Controller, it sucessfuly autowired it. BUT if I autowire org.dozer.Mapper in Component class it fails to autowire.
Why it is failing to autowire org.dozer.Mapper only on Component class?

Please make me correct if I wrong. Thank you 🙂

Best Answer

Your Server is not in one of the packages that you @ComponentScan, so the Mapper is indeed not a bean. Try removing the explicit package from the @ComponentScan (since everything is in a subpackage of the main class that will pick up all the components).

Related Topic