Ce contenu n'est pas disponible dans la langue sélectionnée.

7.2. Callbacks


The descriptor shown in Example 7.3, “Callbacks to Collect and Filter Beans” allows you to collect all beans of a certain type, and even limit the number of matching beans.
In conjunction with the descriptor in Example 7.3, “Callbacks to Collect and Filter Beans”, the Java code shown in Example 7.4, “A Parser to Collect all Editors” shows a Parser which collects all Editors.

Example 7.3. Callbacks to Collect and Filter Beans

<bean name="checker" class="org.jboss.demos.ioc.callback.Checker">
  <constructor>
    <parameter>
      <value-factory bean="parser" method="parse">
	<parameter>
	  <array elementClass="java.lang.Object">
	    <value>http://www.jboss.org</value>
	    <value>SI</value>
	    <value>3.14</value>
	    <value>42</value>
	  </array>
	</parameter>
      </value-factory>
    </parameter>
  </constructor>
</bean>
<bean name="editorA" class="org.jboss.demos.ioc.callback.DoubleEditor"/>
<bean name="editorB" class="org.jboss.demos.ioc.callback.LocaleEditor"/>
<bean name="parser" class="org.jboss.demos.ioc.callback.Parser">
  <incallback method="addEditor" cardinality="4..n"/>
  <uncallback method="removeEditor"/>
</bean>
<bean name="editorC" class="org.jboss.demos.ioc.callback.LongEditor"/>
<bean name="editorD" class="org.jboss.demos.ioc.callback.URLEditor"/>
			
			
			
			

Copy to Clipboard Toggle word wrap

Example 7.4. A Parser to Collect all Editors

public class Parser {
    private Set<Editor> editors = new HashSet<Editor>();
    ...
	public void addEditor(Editor editor)
    {
	editors.add(editor);
    }
    public void removeEditor(Editor editor)
    {
	editors.remove(editor);
    }
}
			
			
			
			

Copy to Clipboard Toggle word wrap
Notice that incallback and uncallback use the method name for matching.
<incallback method="addEditor" cardinality="4..n"/>
<uncallback method="removeEditor"/>
		
		
		
		

Copy to Clipboard Toggle word wrap
A bottom limit controls how many editors actually cause the bean to progress from a Configured state: cardinality=4..n/>
Eventually, Checker gets created and checks the parser. This is illustrated in Example 7.5, “The Checker for the Parser”.

Example 7.5. The Checker for the Parser

public void create() throws Throwable {
    Set<String> strings = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    for (Object element : elements)
	strings.add(element.toString());
    if (expected.equals(strings) == false)
	throw new IllegalArgumentException("Illegal expected set: " + expected + "!=" + strings);
}
			
			
			
			

Copy to Clipboard Toggle word wrap
Retour au début
Red Hat logoGithubredditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance. Découvrez nos récentes mises à jour.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez le Blog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

Theme

© 2025 Red Hat