简体   繁体   中英

Query all Beans of Application Context

I have a Spring application.xml (I have many XML in Blueprint Context)

<bean name="bean1">
  <property name="list">
     <list>
       <bean class="com.jrey.xx.A"></bean>
       <bean class="com.jrey.xx.C"></bean>
     </list>
  </property>
<bean name="bean2">
  <property name="list">
    <list>
      <bean class="com.jrey.xx.A"></bean>
      <bean class="com.jrey.xx.B"></bean>
   </list>
 </property>

with the getBeanDefinitionNames method I can get the beans bean1,bean2 but I cannot get the list items, I want scan this beans too.

for (String beanName : applicationContext.getBeanDefinitionNames()) {
  //I have a control
}

What is the way to retrieve all beans of application context, that include the beans that not have names and beans items of a list.

You can't. If you want to be able to reference those beans, declare them with a name at the top level of the XML configuration and reference them within the <list> .

<bean name="A1" class="com.jrey.xx.A" />
<bean name="bean2">
  <property name="list">
    <list>
      <ref bean="A1" />
   </list>
  </property>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM