From Documentation

Jump to: navigation, search

Icon info.png Note: The content of this page has been deprecated/removed in the latest version.

Starting from ZK Spring 4.0.0 this feature has been removed.

Stop.png This article is out of date, please refer to for more up to date information.

we deprecate this feature since ZK Spring 3.1 for its function might be unstable under various application servers, which means GenericSpringComposer, CoreContextListener, and @EventHandler are deprecated. Please refer to a post on the blog. We suggest users to use SelectorComposer or GenericForwardComposer . For more information, please refer's_Reference/MVC/Controller/Composer.



Inject ZK Components in a Spring component bean


This example demonstrates how Spring web application developers can autowire ZK components into Spring component beans. It has a single textbox to enter a name and a button to show a greeting message.


Setup ZK Spring integration library as described in Setting up ZK Spring section earlier. In addition to this you need the following configurations to make autowiring of ZK components in Spring beans work. In your application web.xml file, declare ZK Spring Core listener. Make sure it is declared before standard Spring context listener as it needs to pre-process spring beans to enable ZK component autowiring.


Next enable ZK custom scopes by specifying <zk-config/> in your bean configuration file

<beans xmlns=""
    <context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents"></context:component-scan>

Notice that you need to declare ZK Spring Core namespace schema at the start of your bean configuration file. Also important is to enable context component scan for org.zkoss.spring.beans.zkcomponents package

Note: This feature has a runtime dependency on reflections (v0.9.5-RC2), javassist (v3.14.0.GA) and CGLIB (v2.2)libraries.


Lets take a look at our example ZUML file

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Autowire ZK Components Example" border="normal" height="100px"
    width="400px" apply="${greetingCtrl}">
    <label value="Name:"></label>
    <textbox id="name" />
    <button id="greetBtn" label="Greet!" />

Here we use standard ZK MVC approach to apply a controller to the main window using apply attribute. Value of apply attribute is an EL expression ${greetingCtrl} that resolves to a spring bean instance.


We have a Spring managed component bean that we apply as a controller to the window declared in our ZUML. We also autowire several ZK components in this bean as shown below.

public class GreetingCtrl extends GenericSpringComposer {

    private Textbox name;
    private Button greetBtn;
    public void doAfterCompose(Component comp) throws Exception {
    public void showGreeting(Event evt) throws WrongValueException, InterruptedException {"Hello " + name.getValue() + "!");

As you can see above the @Autowired ZK components corresponds to the ones defined on the ZUML page. For this autowiring to work you need to extend from a new utility class org.zkoss.spring.util.GenericSpringComposer. In addition to the @Autowired ZK components in this Spring managed bean you can also define event handlers for ZK component events using EventHandler annotation. For example in above code we have an event handling method called showGreeting(Event) that handles onClick event of greetBtn ZK Button component.

Version History

Last Update : 2019/1/21

Version Date Content
  3.1   2012/3/9   Deprecate the feature "Inject ZK Components in Spring Beans".
  4.0.0   2019/01   Remove the feature "Inject ZK Components in Spring Beans".

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.