获取豆子列表的方法?

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

How to get a list of beans?

问题

@Configuration
@EnableTransactionManagement
public class JdbcTemplateConfig{

   @Bean("JdbcTemplateOne")
   public NamedParameterJdbcTemplate jdbcTemplateOne(@Qualifier("firstDataSource") final DataSource ds){
       return new NamedParameterJdbcTemplate(ds); 
   }

   @Bean("JdbcTemplateTwo")
   public NamedParameterJdbcTemplate jdbcTemplateTwo(@Qualifier("secondDataSource") final DataSource ds){
       return new NamedParameterJdbcTemplate(ds); 
   }

   @Bean("JdbcTemplateThree")
   public NamedParameterJdbcTemplate jdbcTemplateThree(@Qualifier("thirdDataSource") final DataSource ds){
       return new NamedParameterJdbcTemplate(ds); 
   }
}
@SpringBootTest
public class SomeITCase{

    @Autowired
    @Qualifier("JdbcTemplateOne")
    private NamedParameterJdbcTemplate jdbcTemplate1;

    @Autowired
    @Qualifier("JdbcTemplateTwo")
    private NamedParameterJdbcTemplate jdbcTemplate2;

    @Autowired
    @Qualifier("JdbcTemplateThree")
    private NamedParameterJdbcTemplate jdbcTemplate3;

    @Autowired
    private List<NamedParameterJdbcTemplate> myList;
}
英文:

I have something like :

@Configuration
@EnableTransactionManagement
public class JdbcTemplateConfig{
 
   @Bean(&quot;JdbcTemplateOne&quot;)
   public NamedParameterJdbcTemplate (@Qualifier(firstDataSource final DataSource ds)){
       return new NamedParameterJdbcTemplate(ds); 
   }

   @Bean(&quot;JdbcTemplateTwo&quot;)
   public NamedParameterJdbcTemplate (@Qualifier(secondDataSource final DataSource ds)){
       return new NamedParameterJdbcTemplate(ds); 
   }

   @Bean(&quot;JdbcTemplateThree&quot;)
   public NamedParameterJdbcTemplate (@Qualifier(thirdDataSource final DataSource ds)){
       return new NamedParameterJdbcTemplate(ds); 
   }
}

and now I need a list of above templates. To get a one by one (for example in an IT-Test) I can make something like:

 @SpringBootTest
 public class SomeITCase{

      @Autowired
      @Qualifier(&quot;JdbcTemplateOne&quot;)
      private NamedParameterJdbcTemplate jdbcTemplate1;

      @Autowired
      @Qualifier(&quot;JdbcTemplateTwo&quot;)
      private NamedParameterJdbcTemplate jdbcTemplate2;

      @Autowired
      @Qualifier(&quot;JdbcTemplateThree&quot;)
      private NamedParameterJdbcTemplate jdbcTemplate3;

      ???
      List&lt;NamedParameterJdbcTemplate&gt; myList = ???
}

Question: How to get all templates in a list without to have declaring them one by one?

答案1

得分: 1

获取应用程序上下下文中所有bean的列表,可以执行以下操作:

public void beanNames(ApplicationContext ctx) {
    String[] beanNames = ctx.getBeanDefinitionNames();
    for (String beanName : beanNames) {
        System.out.println(beanName);
    }
}

您还可以查看Spring Boot监控。它提供了像/bean这样的端点,用于获取在Spring中注册的所有bean。

英文:

To get list of all the beans from the Application context, you can do:

public void beanNames(ApplicationContext ctx) {
      String[] beanNames = ctx.getBeanDefinitionNames();
      for (String beanName : beanNames) {
        System.out.println(beanName);
      }
}

You can also look into Spring boot acutator. It provides endpoints like /bean to get all the beans registered with Spring.

答案2

得分: 1

你所寻找的是一个简单的 @Autowired 注解。

假设我们有几个像你上面示例中定义的bean。

现在,我们创建一个如下的测试:

@SpringBootTest
@Slf4j
public class SomeITCase {
        
    @Autowired
    @Qualifier("JdbcTemplateTwo")
    private NamedParameterJdbcTemplate jdbcTemplate2;

    @Autowired
    List<NamedParameterJdbcTemplate> allTemplates;

    @Test
    public void testMyList() {
        assertThat(allTemplates)
                .hasSize(3)
                .contains(jdbcTemplate2);
    
        allTemplates.forEach(template -> {
            log.info(template.toString());
        });
    }
}

执行这个测试应该会非常明显:Spring会将所有匹配类型的bean注入到你的列表中。这个测试确保列表的大小为三,并至少包含一个已知的bean引用。

关于如何开始使用 @Autowired 的更多信息,可以参考 Injecting collections - Injecting Bean references

英文:

What you a re looking for is a straight forward @Autowired annotation.

Let's suppose we have several beans defined like in your above example.

Now, we create a test like below:

@SpringBootTest
@Slf4j
public class SomeITCase {
        
    @Autowired
    @Qualifier(&quot;JdbcTemplateTwo&quot;)
    private NamedParameterJdbcTemplate jdbcTemplate2;

    @Autowired
    List&lt;NamedParameterJdbcTemplate&gt; allTemplates;

    @Test
    public void testMyList() {
        assertThat(allTemplates)
                .hasSize(3)
                .contains(jdbcTemplate2);
    
        allTemplates.forEach(template -&gt; {
            log.info(template.toString());
        });
    }
}

Executing this test should make it really obvious: Spring injects all beans of matching type in your list. The test ensures that the size is three and at least the one known bean reference is contained.

For further information on getting started with @Autowired, maybe take a look at Injecting collections - Injecting Bean references.

答案3

得分: 1

如果您只想要应用程序中所有NamedParameterJdbcTemplate bean的列表,您可以请求Spring自动装配它。它允许您自动装配特定类型的所有bean的列表:

{
...
      @Autowired
      private List<NamedParameterJdbcTemplate> allJdbcTemplates;
...
}
英文:

If you just want a list of all NamedParameterJdbcTemplate beans in your application, you could just ask spring to autowire it for you. It allows you to autowire a list of all beans of a specific type:

{
...
      @Autowired
      private List&lt;NamedParameterJdbcTemplate&gt; allJdbcTemplates;
...
}

huangapple
  • 本文由 发表于 2020年7月23日 16:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63049778.html
匿名

发表评论

匿名网友

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

确定