此内容没有您所选择的语言版本。

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
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat