我可以使用HashMap来选择在SpringBoot中自动装配哪个接口(DAO)吗?

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

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(&quot;abc&quot;) {
   obj = abcDAO.getOne(id);
} else if(key.equals(&quot;xyz&quot;) {
   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&lt;String, ParentCLass&gt; 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&lt;String, ParentClass&gt; map. 
map.put(&quot;abc&quot;, 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&lt;String, MyDao&gt; 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&lt;String, Repository&gt; daos;

EDIT2: Example

public interface UserRepo extends JpaRepository&lt;User, Long&gt; { ... }

@Service
public class MyService {

    @Autowired
	private Map&lt;String, Repository&gt; daos;

    public List&lt;User&gt; findAll() {
        return daos.get(&quot;userRepo&quot;).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 = &quot;daoImpl1&quot;)
public Dao daoImpl1(){
return new DaoImpl1();

@Bean(name = &quot;daoImpl2&quot;)
public Dao daoImpl2(){
return new DaoImpl2();

And then @Autowire them using @Qualifier with that name

@Autowired
@Qualifier(&quot;daoImpl1&quot;)
private Dao daoImpl1;

@Autowired
@Qualifier(&quot;daoImpl2&quot;)
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;
}

我可以使用HashMap来选择在SpringBoot中自动装配哪个接口(DAO)吗?

英文:

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&lt;Page&gt; getAllPages() {
        List&lt;Page&gt; allPages = entityRepository.findAll(Page.class);
        
        return   allPages;
    }

我可以使用HashMap来选择在SpringBoot中自动装配哪个接口(DAO)吗?

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

发表评论

匿名网友

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

确定