构造函数中的第5个参数需要找到一个类型为’…Mapper’的bean,但找不到。

huangapple go评论72阅读模式
英文:

Parameter 5 of constructor in ... required a bean of type '...Mapper' that could not be found

问题

Description:

构造函数中的第 5 个参数在 com.example.springmysqlelastic.utils.ElasticSynchronizer 中需要一个类型为 'com.example.springmysqlelastic.mapper.FoodMapper' 的 bean,但找不到该类型的 bean。

Action:

考虑在您的配置中定义一个类型为 'com.example.springmysqlelastic.mapper.FoodMapper' 的 bean。

存在一个将 MySQL 同步到 Elasticsearch 的模块,对于 User 实体一切正常。然后我添加了新的 Food 实体。我设置了文件。但现在我在 FoodMapper 上遇到了 Bean 错误。
项目信息:使用 Spring 在 Elasticsearch 引擎上进行食品、餐厅和用户搜索。

FoodMapper.java

@Mapper(componentModel = "spring")
public interface FoodMapper {

    FoodDTO toFoodDTO(Food food);

    List<FoodDTO> toFoodDtos(List<Food> foods);

    Food toFood(FoodDTO foodDTO);

    List<Food> toFoods(List<FoodDTO> foodDTOS);

    FoodModel toFoodModel(Food food);
}

UserMapper.java

@Mapper(componentModel = "spring")
public interface UserMapper {

    UserDTO toUserDTO(User user);

    List<UserDTO> toUserDtos(List<User> users);

    User toUser(UserDTO userDTO);

    List<User> toUsers(List<UserDTO> userDTOS);

    UserModel toUserModel(User user);
}

FoodService.java

@Service
public class FoodService implements IFoodService {

    private IFoodDAO foodDAO;
    private FoodMapper foodMapper;

    @Autowired
    public FoodService(IFoodDAO foodDAO, FoodMapper foodMapper) {
        this.foodDAO = foodDAO;
        this.foodMapper = foodMapper;
    }

    @Override
    public FoodDTO save(FoodDTO foodDTO) {
        Food food = this.foodDAO.save(this.foodMapper.toFood(foodDTO));
        return this.foodMapper.toFoodDTO(food);
    }

    @Override
    public FoodDTO findById(Long id) {
        return this.foodMapper.toFoodDTO(this.foodDAO.findById(id).orElse(null));
    }

    @Override
    public List<FoodDTO> findAll() {
        return this.foodMapper.toFoodDtos(this.foodDAO.findAll());
    }
}

ElasticSynchronizer.java

@Service
public class ElasticSynchronizer {

    private IUserDAO userDAO;
    private IUserESRepo userESRepo;
    private UserMapper userMapper;
    private IFoodDAO foodDAO;
    private IFoodESRepo foodESRepo;
    private FoodMapper foodMapper;

    private static final Logger LOG = LoggerFactory.getLogger(ElasticSynchronizer.class);

    @Autowired
    public ElasticSynchronizer(IUserDAO userDAO, IUserESRepo userESRepo, UserMapper userMapper, IFoodDAO foodDAO, IFoodESRepo foodESRepo, FoodMapper foodMapper) {
        this.userDAO = userDAO;
        this.userESRepo = userESRepo;
        this.userMapper = userMapper;
        this.foodDAO = foodDAO;
        this.foodESRepo = foodESRepo;
        this.foodMapper = foodMapper;
    }

    @Scheduled(cron = "0 */3 * * * *")
    @Transactional
    public void sync() {
        LOG.info("开始同步用户 - {}", LocalDateTime.now());
        this.syncUsers();
        LOG.info("结束同步用户 - {}", LocalDateTime.now());
        LOG.info("开始同步食品 - {}", LocalDateTime.now());
        this.syncFoods();
        LOG.info("结束同步食品 - {}", LocalDateTime.now());
    }

    private void syncUsers() {

        Specification<User> userSpecification = (root, criteriaQuery, criteriaBuilder) ->
                getModificationDatePredicate(criteriaBuilder, root);
        List<User> userList;
        if (userESRepo.count() == 0) {
            userList = userDAO.findAll();
        } else {
            userList = userDAO.findAll(userSpecification);
        }
        for(User user: userList) {
            LOG.info("同步用户 - {}", user.getId());
            userESRepo.save(this.userMapper.toUserModel(user));
        }
    }
    private void syncFoods() {

        Specification<Food> foodSpecification = (root, criteriaQuery, criteriaBuilder) ->
                getModificationDatePredicate(criteriaBuilder, root);
        List<Food> foodList;
        if (foodESRepo.count() == 0) {
            foodList = foodDAO.findAll();
        } else {
            foodList = foodDAO.findAll(foodSpecification);
        }
        for(Food food: foodList) {
            LOG.info("同步食品 - {}", food.getId());
            foodESRepo.save(this.foodMapper.toFoodModel(food));
        }
    }
    private static Predicate getModificationDatePredicate(CriteriaBuilder cb, Root<?> root) {
        Expression<Timestamp> currentTime;
        currentTime = cb.currentTimestamp();
        Expression<Timestamp> currentTimeMinus = cb.literal(new Timestamp(System.currentTimeMillis() -
                (Constants.INTERVAL_IN_MILLISECONDS)));
        return cb.between(root.<Date>get(Constants.MODIFICATION_DATE),
                currentTimeMinus,
                currentTime
        );
    }
}

Food.java

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Food {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private float price;
    private String category;
    private String description;
    private String imglink;
    private String restaurant;
    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date modificationDate;
    
}

FoodModel.java

@NoArgsConstructor
@AllArgsConstructor
@Data
@Document(indexName = "food") //type deprecate oldu hepsi artik _doc
public class FoodModel {
    private Long id;
    private String name;
    private float price;
    private String category;
    private String description;
    private String imglink;
    private String restaurant;
    private Date modificationDate;
}

FoodDTO.java

@NoArgsConstructor
@AllArgsConstructor
@Data
public class FoodDTO {
    private Long id;
    private String name;
    private float price;
    private String category;
    private String description;
    private String imglink;
    private String restaurant;
}

IFoodDAO.java

@Repository
public interface IFoodDAO extends JpaRepository<Food, Long>, JpaSpecificationExecutor<Food> {
    //List<User> findByNameContaining(String name);
}

IFoodESRepo.java

public interface IFoodESRepo extends ElasticsearchRepository<FoodModel, Long> {
      //List<Food> findByNameContaining(String name);
}

main class

@SpringBootApplication
@EnableElasticsearchRepositories("com.example.springmysqlelastic.repo.elastic")
@EnableScheduling
@EnableJpaRepositories("com.example.springmysqlelastic.repo")
//@ComponentScan(basePackages = {"com.example.springmysqlelastic"})
//@EnableAutoConfiguration

public class SpringMysqlElasticApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringMysqlElasticApplication.class, args);
    }

}

结构

![enter image description

英文:

Description:

Parameter 5 of constructor in com.example.springmysqlelastic.utils.ElasticSynchronizer required a bean of type 'com.example.springmysqlelastic.mapper.FoodMapper' that could not be found.

Action:

Consider defining a bean of type 'com.example.springmysqlelastic.mapper.FoodMapper' in your configuration.

There is a MySQL to Elasticsearch sync module and all was working for User entity. Then i added new Food entity. I set files. But now im getting Bean error on FoodMapper.
Project info: food, restaurant, user search on Elasticsearch engine with Spring.

FoodMapper.java

@Mapper(componentModel = &quot;spring&quot;)
public interface FoodMapper {
FoodDTO toFoodDTO(Food food);
List&lt;FoodDTO&gt; toFoodDtos(List&lt;Food&gt; foods);
Food toFood(FoodDTO foodDTO);
List&lt;Food&gt; toFoods(List&lt;FoodDTO&gt; foodDTOS);
FoodModel toFoodModel(Food food);
}

UserMapper.java

@Mapper(componentModel = &quot;spring&quot;)
public interface UserMapper {
UserDTO toUserDTO(User user);
List&lt;UserDTO&gt; toUserDtos(List&lt;User&gt; users);
User toUser(UserDTO userDTO);
List&lt;User&gt; toUsers(List&lt;UserDTO&gt; userDTOS);
UserModel toUserModel(User user);
}

FoodService.java

@Service
public class FoodService implements IFoodService {
private IFoodDAO foodDAO;
private FoodMapper foodMapper;
@Autowired
public FoodService(IFoodDAO foodDAO, FoodMapper foodMapper) {
this.foodDAO = foodDAO;
this.foodMapper = foodMapper;
}
@Override
public FoodDTO save(FoodDTO foodDTO) {
Food food = this.foodDAO.save(this.foodMapper.toFood(foodDTO));
return this.foodMapper.toFoodDTO(food);
}
@Override
public FoodDTO findById(Long id) {
return this.foodMapper.toFoodDTO(this.foodDAO.findById(id).orElse(null));
}
@Override
public List&lt;FoodDTO&gt; findAll() {
return this.foodMapper.toFoodDtos(this.foodDAO.findAll());
}
}

ElasticSynchorizer.java

@Service
public class ElasticSynchronizer {
private IUserDAO userDAO;
private IUserESRepo userESRepo;
private UserMapper userMapper;
private IFoodDAO foodDAO;
private IFoodESRepo foodESRepo;
private FoodMapper foodMapper;
private static final Logger LOG = LoggerFactory.getLogger(ElasticSynchronizer.class);
@Autowired
public ElasticSynchronizer(IUserDAO userDAO, IUserESRepo userESRepo, UserMapper userMapper, IFoodDAO foodDAO, IFoodESRepo foodESRepo, FoodMapper foodMapper) {
this.userDAO = userDAO;
this.userESRepo = userESRepo;
this.userMapper = userMapper;
this.foodDAO = foodDAO;
this.foodESRepo = foodESRepo;
this.foodMapper = foodMapper;
}
@Scheduled(cron = &quot;0 */3 * * * *&quot;)
@Transactional
public void sync() {
LOG.info(&quot;Start Syncing Users - {}&quot;, LocalDateTime.now());
this.syncUsers();
LOG.info(&quot; End Syncing Users - {}&quot;, LocalDateTime.now());
LOG.info(&quot;Start Syncing Foods- {}&quot;, LocalDateTime.now());
this.syncFoods();
LOG.info(&quot; End Syncing Foods - {}&quot;, LocalDateTime.now());
}
private void syncUsers() {
Specification&lt;User&gt; userSpecification = (root, criteriaQuery, criteriaBuilder) -&gt;
getModificationDatePredicate(criteriaBuilder, root);
List&lt;User&gt; userList;
if (userESRepo.count() == 0) {
userList = userDAO.findAll();
} else {
userList = userDAO.findAll(userSpecification);
}
for(User user: userList) {
LOG.info(&quot;Syncing User - {}&quot;, user.getId());
userESRepo.save(this.userMapper.toUserModel(user));
}
}
private void syncFoods() {
Specification&lt;Food&gt; userSpecification = (root, criteriaQuery, criteriaBuilder) -&gt;
getModificationDatePredicate(criteriaBuilder, root);
List&lt;Food&gt; foodList;
if (userESRepo.count() == 0) {
foodList = foodDAO.findAll();
} else {
foodList = foodDAO.findAll(userSpecification);
}
for(Food food: foodList) {
LOG.info(&quot;Syncing Food - {}&quot;, food.getId());
foodESRepo.save(this.foodMapper.toFoodModel(food));
}
}
private static Predicate getModificationDatePredicate(CriteriaBuilder cb, Root&lt;?&gt; root) {
Expression&lt;Timestamp&gt; currentTime;
currentTime = cb.currentTimestamp();
Expression&lt;Timestamp&gt; currentTimeMinus = cb.literal(new Timestamp(System.currentTimeMillis() -
(Constants.INTERVAL_IN_MILLISECONDE)));
return cb.between(root.&lt;Date&gt;get(Constants.MODIFICATION_DATE),
currentTimeMinus,
currentTime
);
}
}

Food.java

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Food {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private float price;
private String category;
private String description;
private String imglink;
private String restaurant;
@Temporal(TemporalType.TIMESTAMP)
@UpdateTimestamp
private Date modificationDate;
}

Foodmodel.java

@NoArgsConstructor
@AllArgsConstructor
@Data
@Document(indexName = &quot;food&quot;) //type deprecate oldu hepsi artik _doc
public class FoodModel {
private Long id;
private String name;
private float price;
private String category;
private String description;
private String imglink;
private String restaurant;
private Date modificationDate;
}

FoodDTO.java

@NoArgsConstructor
@AllArgsConstructor
@Data
public class FoodDTO {
private Long id;
private String name;
private float price;
private String category;
private String description;
private String imglink;
private String restaurant;
}

IFoodDAO.java

@Repository
public interface IFoodDAO extends JpaRepository&lt;Food, Long&gt;, JpaSpecificationExecutor&lt;Food&gt; {
//List&lt;User&gt; findByNameContaining(String name);
}

IFoodESRepo.java

public interface IFoodESRepo extends ElasticsearchRepository&lt;FoodModel, Long&gt; {
//List&lt;Food&gt; findByNameContaining(String name);
}

main class

@SpringBootApplication
@EnableElasticsearchRepositories(&quot;com.example.springmysqlelastic.repo.elastic&quot;)
@EnableScheduling
@EnableJpaRepositories(&quot;com.example.springmysqlelastic.repo&quot;)
//@ComponentScan(basePackages = {&quot;com.example.springmysqlelastic&quot;})
//@EnableAutoConfiguration
public class SpringMysqlElasticApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMysqlElasticApplication.class, args);
}
}

structure
enter image description here
enter image description here

答案1

得分: 1

我发现我不能使用多个映射器。将第二个映射器移到第一个现在可以使用。
不要使用多于一个映射器。

英文:

I found that i can't use multiple mappers. Moved second mapper to first now works.
Don't use more than 1 mapper.

huangapple
  • 本文由 发表于 2020年8月6日 22:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63285513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定