Inject ZK Components in Spring Beans"
m |
m (correct highlight (via JWB)) |
||
(29 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{ZKSpringEssentialsPageHeader}} | {{ZKSpringEssentialsPageHeader}} | ||
+ | |||
+ | {{Deprecated Content}} | ||
+ | |||
+ | Starting from ZK Spring 4.0.0 this feature has been removed. | ||
+ | |||
+ | {{Deprecated|url= http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction}} | ||
+ | |||
+ | we deprecate this feature since ZK Spring 3.1 for its function might be unstable under various application servers, which means <code> GenericSpringComposer</code>, <code> CoreContextListener</code>, and <code> @EventHandler </code> are deprecated. Please refer to [http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction a post on the blog]. We suggest users to use <code>SelectorComposer</code> or <code> GenericForwardComposer </code>. For more information, please refer http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer. | ||
===Purpose=== | ===Purpose=== | ||
+ | Inject ZK Components in a Spring component bean | ||
===Example=== | ===Example=== | ||
+ | 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. | ||
+ | |||
===Configuration=== | ===Configuration=== | ||
+ | Setup ZK Spring integration library as described in [[ZK_Spring_Essentials/Getting_Started_with_ZK_Spring/Setting_Up_ZK_Spring|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. | ||
+ | <source lang="xml" start="8" highlight="9"> | ||
+ | <listener> | ||
+ | <listener-class>org.zkoss.spring.web.context.CoreContextListener</listener-class> | ||
+ | </listener> | ||
+ | </source> | ||
+ | Next enable ZK custom scopes by specifying <code><zk-config/></code> in your bean configuration file | ||
+ | <source lang="xml" highlight="5,9,12,13"> | ||
+ | <beans xmlns="http://www.springframework.org/schema/beans" | ||
+ | xmlns:security="http://www.springframework.org/schema/security" | ||
+ | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
+ | xmlns:context="http://www.springframework.org/schema/context" | ||
+ | xmlns:zksp="http://www.zkoss.org/2008/zkspring/core" | ||
+ | xmlns:aop="http://www.springframework.org/schema/aop" | ||
+ | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
+ | http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd | ||
+ | http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd | ||
+ | http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd | ||
+ | http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> | ||
+ | <context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents"></context:component-scan> | ||
+ | <zksp:zk-config/> | ||
+ | ... | ||
+ | </source> | ||
+ | |||
+ | 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 <code>org.zkoss.spring.beans.zkcomponents</code> package | ||
+ | |||
+ | Note: This feature has a runtime dependency on [http://code.google.com/p/reflections/ reflections] (v0.9.5-RC2), [http://www.csg.is.titech.ac.jp/~chiba/javassist/ javassist] (v3.14.0.GA) and [http://cglib.sourceforge.net/ CGLIB] (v2.2)libraries. | ||
+ | |||
+ | ===ZUML=== | ||
+ | Lets take a look at our example ZUML file | ||
+ | <source lang="xml"> | ||
+ | <?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!" /> | ||
+ | </window> | ||
+ | </source> | ||
+ | Here we use standard [http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC ZK MVC approach] to apply a controller to the main window using apply attribute. Value of apply attribute is an EL expression <code>${greetingCtrl}</code> that resolves to a spring bean instance. | ||
+ | |||
+ | ===Java=== | ||
+ | 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. | ||
+ | <source lang="java" start="5" highlight="7,9,10,12,13,19"> | ||
+ | @org.springframework.stereotype.Component("greetingCtrl") | ||
+ | @Scope("desktop") | ||
+ | public class GreetingCtrl extends GenericSpringComposer { | ||
+ | |||
+ | @Autowired | ||
+ | private Textbox name; | ||
+ | |||
+ | @Autowired | ||
+ | private Button greetBtn; | ||
+ | |||
+ | public void doAfterCompose(Component comp) throws Exception { | ||
+ | super.doAfterCompose(comp); | ||
+ | } | ||
+ | |||
+ | @EventHandler("greetBtn.onClick") | ||
+ | public void showGreeting(Event evt) throws WrongValueException, InterruptedException { | ||
+ | Messagebox.show("Hello " + name.getValue() + "!"); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | 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 <code>org.zkoss.spring.util.GenericSpringComposer</code>. | ||
+ | In addition to the @Autowired ZK components in this Spring managed bean you can also define event handlers for ZK component events using <javadoc directory="zkspring-core">org.zkoss.spring.context.annotation.EventHandler</javadoc> annotation. For example in above code we have an event handling method called <code>showGreeting(Event)</code> that handles <code>onClick</code> event of <code>greetBtn</code> ZK Button component. | ||
=Version History= | =Version History= | ||
Line 10: | Line 90: | ||
! Version !! Date !! Content | ! 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". | ||
|} | |} | ||
{{ZKSpringEssentialsPageFooter}} | {{ZKSpringEssentialsPageFooter}} |
Latest revision as of 12:57, 19 January 2022
Starting from ZK Spring 4.0.0 this feature has been removed.
This article is out of date, please refer to http://blog.zkoss.org/index.php/2012/03/08/zk-spring-3-1-release-introduction 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 http://books.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Controller/Composer.
Purpose
Inject ZK Components in a Spring component bean
Example
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.
Configuration
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.
<listener>
<listener-class>org.zkoss.spring.web.context.CoreContextListener</listener-class>
</listener>
Next enable ZK custom scopes by specifying <zk-config/>
in your bean configuration file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="org.zkoss.zkspringessentials.controller,org.zkoss.spring.beans.zkcomponents"></context:component-scan>
<zksp:zk-config/>
...
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.
ZUML
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!" />
</window>
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.
Java
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.
@org.springframework.stereotype.Component("greetingCtrl")
@Scope("desktop")
public class GreetingCtrl extends GenericSpringComposer {
@Autowired
private Textbox name;
@Autowired
private Button greetBtn;
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
}
@EventHandler("greetBtn.onClick")
public void showGreeting(Event evt) throws WrongValueException, InterruptedException {
Messagebox.show("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
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". |