首页 文章

无法在spring boot中的组件类中自动装配dozer Mapper

提问于
浏览
1

我是Spring Boot的新手 . 我试图用neo4j数据库在spring boot mvc中开发小应用程序 . 以下是我的服务器

@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);
}

}

以下是我的主要课程

@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);
}

}

以下是我的Component类

@Component
public class MyService
{

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

.....
}

以下是我的Controller类

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

当我运行主类MainWithStructure时,我得到了以下Exception

org.springframework.beans.factory.BeanCreationException:创建名为'mainWithStructure'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private myproject.business.MyService myproject.main.MainWithStructure.MyService;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'myService'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.dozer.Mapper myproject.business.MyService.mapper;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.dozer.Mapper]的限定bean用于依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

以下是我的项目结构

demo_project src / main / java --- myproject.main ------ MainWithStructure.java ------Server.java ---myproject.business ------ MyService.java --- myproject .rest ------ MyController.java

如果我在Controller中自动装配 org.dozer.Mapper ,它就会成功地自动装配它 . 但如果我在Component类中自动装配 org.dozer.Mapper 则无法自动装配 . 为什么它只能在Component类上自动装配 org.dozer.Mapper

如果我错了,请让我纠正 . 谢谢 :)

2 回答

  • 2

    您的 Server 不在 @ComponentScan 的其中一个软件包中,因此 Mapper 确实不是bean . 尝试从 @ComponentScan 中删除显式包(因为所有内容都在主类的子包中,它将拾取所有组件) .

  • 1

    您可以在主类中添加@SpringBootApplication,它具有@ComponentScan,它将扫描项目中的所有子组件 .

相关问题