【问题标题】:Unable to autowire dozer Mapper in component class in spring boot无法在 Spring Boot 的组件类中自动装配推土机 Mapper
【发布时间】:2014-11-11 20:46:16
【问题描述】:

我是 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
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

org.springframework.beans.factory.BeanCreationException:创建名为“mainWithStructure”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 myproject.business.MyService myproject.main.MainWithStructure.MyService;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“myService”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.dozer.Mapper myproject.business.MyService.mapper;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到 [org.dozer.Mapper] 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

以下是我的项目结构

演示项目

  • src/main/java

---myproject.main

-----MainWithStructure.java

------Server.java

---我的项目.business

-----MyService.java

---我的项目.rest

------MyController.java

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

如果我错了,请纠正我。谢谢你:)

【问题讨论】:

  • 它到底是怎么失败的? Spring 是在尝试创建 MyService 实例但未能成功,还是根本不尝试创建实例?你能用错误更新问题吗?另外,你的各种课程都在哪些包中?它们都需要位于名为 myproject 的包或其子包中。
  • 我已经更新了我的问题。 :)
  • 从您过去的一个帖子答案中得到它;)。刚刚在 MainWithStructure 类上写了 @ComponentScan 而不是 @ComponentScan({ "myproject.business" })

标签: spring spring-boot autowired dozer


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-10
      • 2018-12-27
      • 2021-04-02
      • 2015-04-06
      • 2017-09-07
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多