英文:
Can I use a HashMap to pick which interface (DAO) to autowire in SpringBoot?
问题
我会尽量详细说明。我有许多DAO,我的服务需要根据我获得的密钥使用其中一个。例如 -
if(key.equals("abc") {
obj = abcDAO.getOne(id);
} else if(key.equals("xyz") {
obj = xyzDAO.getOne(id);
}
该对象属于父类类型,abc、xyz 等都是子类。
我的想法是创建一个 Map<String, ParentCLass>
,只需传递密钥即可获取对象,而不是使用 If-else,以便于进一步的更改。如果它是一个普通类,我会初始化这个映射如下 -
Map<String, ParentClass> map.
map.put("abc", new Abc());
但是由于DAO是接口,并且需要通过 @Autowired 来使用它们,我不知道如何继续。我是一个初学者。感谢任何帮助。
英文:
I will try to be as detailed as possible. I have many DAO and my service needs to use one of them based on the key i get. For instance -
if(key.equals("abc") {
obj = abcDAO.getOne(id);
} else if(key.equals("xyz") {
obj = xyzDAO.getOne(id);
}
The object is of type parent class and abc, xyz.. are all child classes.
My idea is to create a Map<String, ParentCLass>
to get the object just by passing the key instead of If-else so that it will be easy to add for further changes. In case it would've been a normal class, I wouldv'e initialized the map as
Map<String, ParentClass> map.
map.put("abc", new Abc());
But since DAO are interfaces and require to be @Autowired for using them, I don't know how to proceed. I am a beginner. Any help appreciated.
答案1
得分: 4
Spring能够将所有具有相同接口的bean注入到一个Map中,如果该Map的键是String类型(将包含bean名称),值是接口。
public interface MyDao {
}
@Autowired
private Map<String, MyDao> daos;
编辑: 如果你使用Spring Data Repository,已经有一个标记接口:Repository。你可以使用下面的代码将所有DAO注入到一个bean中。
@Autowired
private Map<String, Repository> daos;
编辑2: 示例
public interface UserRepo extends JpaRepository<User, Long> { ... }
@Service
public class MyService {
@Autowired
private Map<String, Repository> daos;
public List<User> findAll() {
return daos.get("userRepo").findAll();
}
}
英文:
Spring is able to inject all beans with the same interface in a map if the map has String as key (will contain the bean names) and the interface as value.
public interface MyDao {
}
@Autowired
private Map<String, MyDao> daos;
EDIT: If you use Spring Data Repository, there is already a tagging Interface: Repository. You can use below code to inject all DAOs in one bean.
@Autowired
private Map<String, Repository> daos;
EDIT2: Example
public interface UserRepo extends JpaRepository<User, Long> { ... }
@Service
public class MyService {
@Autowired
private Map<String, Repository> daos;
public List<User> findAll() {
return daos.get("userRepo").findAll();
}
}
答案2
得分: 0
@Bean注解可以为每个Dao创建具有特定名称的不同bean。
@Bean(name = "daoImpl1")
public Dao daoImpl1(){
return new DaoImpl1();
}
@Bean(name = "daoImpl2")
public Dao daoImpl2(){
return new DaoImpl2();
}
然后可以使用@Qualifier
注解和相应的名称来进行@Autowired
注入。
@Autowired
@Qualifier("daoImpl1")
private Dao daoImpl1;
@Autowired
@Qualifier("daoImpl2")
private Dao daoImpl2;
英文:
You can create different bean for each of your Dao with a specific name
@Bean(name = "daoImpl1")
public Dao daoImpl1(){
return new DaoImpl1();
@Bean(name = "daoImpl2")
public Dao daoImpl2(){
return new DaoImpl2();
And then @Autowire
them using @Qualifier
with that name
@Autowired
@Qualifier("daoImpl1")
private Dao daoImpl1;
@Autowired
@Qualifier("daoImpl2")
private Dao daoImpl2;
答案3
得分: 0
我创建了一些用于执行简单 JPA 操作的方法,
我将所有的仓库存储在 HashMap 中,
你只需要传递子类,就可以获得相应的 JpaRepository。
你可以在 https://github.com/fajaralmu/base_web_app 查看更多信息。
示例:
public List<Page> getAllPages() {
List<Page> allPages = entityRepository.findAll(Page.class);
return allPages;
}
英文:
i made methods to make simple jpa things,
i stored all of repositories in the HashMap
you just have to pass the child class and you get the corresponding JpaRepository
you can see more at https://github.com/fajaralmu/base_web_app
example :
public List<Page> getAllPages() {
List<Page> allPages = entityRepository.findAll(Page.class);
return allPages;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论