gpt4 book ai didi

java - 如何在 NoRepositoryBean 的实现中 Autowiring bean

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:07 25 4
gpt4 key购买 nike

使用 Spring Boot/Spring Data,我向所有存储库添加了自定义功能。这是我所做的一个片段:

所以我有我的存储库界面:

@NoRepositoryBean
public interface RepositoryBase<T, ID extends Serializable> extends JpaRepository<T, ID> {

及其实现

public class RepositoryBaseImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements RepositoryBase<T, ID> {

@Autowired
MessageLocale messageLocale; // <- this is a classic spring bean which is not injected in this spot (always null)

private EntityManager entityManager;

public RepositoryBaseImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}

//... My custom methods here

和我的配置:

@Configuration
@EnableJpaRepositories(basePackages = "my.base.pkg", repositoryBaseClass = RepositoryBaseImpl.class)
public class RepositoryConfig {
}

我的自定义方法工作正常,但我需要注入(inject) messageLocal

Autowired 在 RepositoryBaseImpl 中不起作用(我认为这是因为它不是 bean)

我无法在 RepositoryBaseImpl 上添加 @Repository,因为我在其父接口(interface)上使用 @NoRepositoryBean

那么有没有办法注入(inject) messageLocale ?

最佳答案

根据@Prabhakar D评论,我将根据我的需要发布我的答案(除了@EnableMongoRepositories和其他一些小修改之外,还使用@EnableJpaRepositories)

在@EnableJpaRepositories注释中,添加repositoryFactoryBeanClass:

@EnableJpaRepositories(basePackages = "my.base.pkg", repositoryBaseClass = RepositoryBaseImpl.class, repositoryFactoryBeanClass = MyRepositoryFactoryBean.class)

关键是你可以在repositoryFactoryBeanClass中使用@Autowire

创建一个repositoryFactoryBeanClass并在其中 Autowiring 您的bean。这是一个 spring bean,它在重写的方法 createRepositoryFactory 中创建自定义 JpaRepositoryFactory :

public class MyRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable> extends JpaRepositoryFactoryBean<T, S, ID> {

@Autowired
private MessageLocale messageLocale;

public MyRepositoryFactoryBean(Class repositoryInterface) {
super(repositoryInterface);
}

@Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new MyJpaRepositoryFactory(entityManager, messageLocale);
}
}

现在,创建自定义工厂 (MyJpaRepositoryFactory) 并使用重写的方法 getTargetRepository 创建基础存储库 (RepositoryBaseImpl) 的实例。在这里你可以将你的bean注入(inject)到它的构造函数中:

public class MyJpaRepositoryFactory extends JpaRepositoryFactory {

private EntityManager entityManager;
private MessageLocale messageLocale;

public MyJpaRepositoryFactory(EntityManager entityManager, MessageLocale messageLocale) {
super(entityManager);
this.entityManager = entityManager;
this.messageLocale = messageLocale;
}

//****************** Edit ********************
//starting from spring boot 2.1.0, getTargetRepository(RepositoryInformation information) was made final. So you can't override it anymore. You will need to override getTargetRepository(RepositoryInformation information, EntityManager entityManager)
//@Override
//protected Object getTargetRepository(RepositoryInformation information) {
// return new RepositoryBaseImpl(getEntityInformation(information.getDomainType()), entityManager, messageLocale);
//}
@Override
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
return new RepositoryBaseImpl(getEntityInformation(information.getDomainType()), entityManager, messageLocale);
}
//****************** End Edit ********************
}

现在,只需修改 RepositoryBaseImpl 的构造函数,以便它可以接受所需的 Bean:

public class RepositoryBaseImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements RepositoryBase<T, ID> {

private MessageLocale messageLocale;
private EntityManager entityManager;

//if you are using IntelliJ, it can show you an error saying "Could not autowire. No beans of JpaEntityInformation". It is just a bug in IntelliJ
public RepositoryBaseImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager, MessageLocale messageLocale) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
this.messageLocale = messageLocale;
}

//... My custom methods here

现在您的 messageLocal 已注入(inject) BaseRepositoryImpl 中,您可以在自定义方法中使用它,而无需在参数中传递它

希望这对某人有帮助

关于java - 如何在 NoRepositoryBean 的实现中 Autowiring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com