如何获取和设置Spring的Autowired对象?

时间:2017-08-01 03:53:38

标签: spring

我正在实现JSF + Spring集成的Web应用程序,我有几个服务类,如下所示:

@Service
@Transactional
public class AccountService
{
    @Autowired
    private AccountRepository accountRepository;

    public AccountRepository getRepository()
    {                   
        return accountRepository;
    }

    public Account insertOrUpdate(Account entity) 
    {
        return accountRepository.save(entity);
    }

    public Account findOne(Account entiti)
    {
        return accountRepository.findOne(entiti.getId());
    }
    //some other methods
}

我想实现一个基类,它包含所有服务类的常用方法,如下所示:

@Service
@Transactional
public class BaseService
{
    JpaRepository repository;

    public void setRepository(JpaRepository repository)
    {
        this.repository = repository;
    }

    public BaseEntity insertOrUpdate(BaseEntity entity)
    {
        return (BaseEntity) repository.save(entity);
    }

    public BaseEntity findOne(BaseEntity entity)
    {
        return (BaseEntity) repository.findOne(entity.getId());
    }
}

但问题是如何动态地自动装配存储库?我试图从子类设置存储库,如下所示,但不起作用。当我在超类中使用NullPointerException对象时,它将导致repository。有关如何实现这一目标的任何建议吗?

@Service
@Transactional
public class AccountService extends BaseService
{
    @Autowired
    private AccountRepository accountRepository;

    public AccountService()
    {
        super.setRepository(accountRepository);
    }
}

2 个答案:

答案 0 :(得分:2)

如果您想要get and set autowired object并使用@Autowired注释,则需要在类链接@Service@RestController上使用注释,并使用代码<context:component-scan annotation-config="true" base-package="com.demo.test"/>和{ {1}}进入<context:annotation-config />文件扫描基础包,这个低音包扫描用于扫描注释。您还可以使用另一种方法,使用java类配置创建bean并为autowire创建对象。在java配置类中,您需要使用ApplicationContaxt.xml注释。您可以在下面找到示例代码以进行配置,其中我是@Configuration

的创建bean
modelMapper

您也可以从xml clas创建bean,您需要将bean创建为@Configuration public class ModelMapperConfig { @Bean(name = "modelMapper") public ModelMapper getModelMapper() { ModelMapper modelMapper = new ModelMapper(); return modelMapper; } @Bean(name = "modelMapperWithConverter") public ModelMapper getModelMapperWithConverter() { ModelMapper modelMapper = new ModelMapper(); return modelMapper; } 。你可以在下面找到示例代码:

ApplicationContacxt.xml

答案 1 :(得分:0)

可以通过创建通用基本服务类来解决此问题,如下所示:
BaseService.java ,您的服务方法的接口。

public interface BaseService<T extends BaseEntity, ID extends Serializable, R extends JpaRepository>
{
    public T insertOrUpdate(T entity);

    public Iterable<T> insertOrUpdate(Iterable<T> entities);

    public T findOne(ID id);

    public List<T> findAll();

    //.........
}

BaseServiceImpl ,一个实现BaseService接口的类

@Transactional
public abstract class BaseServiceImpl<T extends BaseEntity, ID extends Serializable, R extends JpaRepository<T, ID>> implements BaseService<T, ID, R>
{
    protected R repository;

    protected BaseServiceImpl(R repository)
    {
        this.repository = repository;
    }

    @Override
    public T insertOrUpdate(T entity)
    {
        return repository.save(entity);
    }

    @Override
    public Iterable<T> insertOrUpdate(Iterable<T> entities)
    {
        return repository.save(entities);
    }
    //.........
}

AccountService.java ,最后,您的服务类扩展了BaseServiceImpl

@Service
@Transactional
public class AccountService extends BaseServiceImpl<Account, Integer, AccountRepository> //implements BaseService<Account, Integer, AccountRepository>
{
    @Autowired
    public AccountService(AccountRepository repository) {
        super(repository);
    }

    public AccountRepository getRepository()
    {
        return super.repository;
    }

    //create your own service method here
}

最后,抱歉我的英语不好。