注入的Bean在抽象类中为null

时间:2019-03-18 19:43:32

标签: java spring spring-boot dependency-injection

我有抽象的用户服务,在其中我自动连接了两个bean:Repository和AbstractMapper,但是当我对该类运行测试时,都由于NullPointerException而失败。例如,当我运行时,将服务的测试保存在dubug中,这表明两个bean均为空。有人遇到这个问题吗?这是我的代码:

AbstractService

CarparkData.csv

UserServiceImpl

package com.example.shop.service;

import com.example.shop.dto.AbstractMapper;
import com.example.shop.entity.Identifable;
import com.example.shop.repository.IRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service`enter code here`
public abstract class AbstractService<E extends Identifable, D> implements IService<E, D> {

private IRepository<E> repository;
private AbstractMapper<E, D> abstractMapper;

@Autowired
public AbstractService(IRepository<E> repository, AbstractMapper<E, D> abstractMapper) {
    this.repository = repository;
    this.abstractMapper = abstractMapper;
}

@Override
public D get(Long id) {
    E e = repository.get(id);
    return abstractMapper.entityToDto(e);
}

@Override
public List<D> getAll() {
    List<E> all = repository.getAll();
    List<D> allDtos = all.stream()
            .map(e -> abstractMapper.entityToDto(e))
            .collect(Collectors.toList());
    return allDtos;
}

@Override
public E save(D d) {
    E e = abstractMapper.dtoToEntity(d);
    return repository.save(e);
}

@Override
public E update(D d) {
    E e = abstractMapper.dtoToEntity(d);
    return repository.update(e);
}

@Override
public E delete(D d) {
    E e = abstractMapper.dtoToEntity(d);
    return repository.delete(e);
}

@Override
public void deleteAll() {
    repository.deleteAll();
}
}

抽象映射器

package com.example.shop.service;

import com.example.shop.dto.UserDto;
import com.example.shop.dto.UserMapper;
import com.example.shop.entity.User;
import com.example.shop.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
 public class UserServiceImpl extends AbstractService<User, UserDto> implements UserService {

private UserRepository repository;
private UserMapper userMapper;

@Autowired
public UserServiceImpl(UserRepository repository, UserMapper userMapper) {
    super(repository, userMapper);
}
}

用户映射器:

package com.example.shop.dto;

import org.springframework.stereotype.Component;

@Component
public interface AbstractMapper<E, D> {

 E dtoToEntity(D d);
 D entityToDto(E e);
 }

带有@SpringBootApplication的类

package com.example.shop.dto;

import com.example.shop.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserMapper implements AbstractMapper<User, UserDto> {

private AccountMapper accountMapper;

@Autowired
public UserMapper(AccountMapper accountMapper) {
    this.accountMapper = accountMapper;
}

@Override
public User dtoToEntity(UserDto dto) {
    if (dto == null) {
        return null;
    }
    User user = new User();
    user.setId(dto.getId());
    user.setEmail(dto.getEmail());
    user.setPassword(dto.getPassword());
    user.setLogin(dto.getLogin());
    user.setAccount(accountMapper.dtoToEntity(dto.getAccountDto()));
    return user;
}

@Override
public UserDto entityToDto(User user) {
    if (user == null) {
        return null;
    }
    UserDto dto = new UserDto();
    dto.setEmail(user.getEmail());
    dto.setLogin(user.getLogin());
    dto.setPassword(user.getPassword());
    dto.setId(user.getId());
    dto.setAccountDto(accountMapper.entityToDto(user.getAccount()));
    return dto;
}
}

我对服务的测试:

package com.example.shop;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShopApplication implements CommandLineRunner {

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

@Override
public void run(String... args) throws Exception {
    System.out.println("Test");
}
}

1 个答案:

答案 0 :(得分:1)

此代码存在几个问题。首先,您不需要使用服务或组件对抽象类进行注释。抽象类无法实例化,因此没有bean。 第二:具有泛型的类的自动装配不起作用。一旦有了几个bean,它就不再是唯一的了。  检出您的类是否被实例化。也许您需要添加@componentscan。

您的测试位于com.example.shop.service下,因此它仅扫描此程序包下的bean。您应该移动测试或通过使用componentcan批注添加Bean