Using Spring Variable Resolver"

From Documentation
Line 21: Line 21:
 
         public String getMessage() {
 
         public String getMessage() {
 
                 return message;
 
                 return message;
        }
 
        public void setMessage(String message) {
 
                this.message = message;
 
 
         }
 
         }
 
}
 
}
 
</source>
 
</source>
Since Spring 3.0 you have different ways of declaring your beans. The first and the most traditional way is to declare this bean in your applicationContext.xml Spring configuration file as below
+
Spring offers different ways of declaring your beans. The classical way is to declare this bean in your applicationContext.xml Spring configuration file as below.
 +
 
 
<source lang="xml">
 
<source lang="xml">
 
<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
 
<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
         <constructor-arg value="Hello from a simple bean"></constructor-arg>
+
         <constructor-arg value="Hello from a simple bean"/>
 
</bean>
 
</bean>
 
</source>
 
</source>
  
or you can let Spring scans your beans (normally used for auto-wiring dependencies) automatically by providing a base package. The declaration of the Spring context component scan element is shown below:
+
The alternative Java Config to achieve the same (choose for yourself: Spring and ZK will treat both beans equivalently during runtime):
 +
 
 +
<source lang="java">
 +
    @Bean
 +
    public SimpleBean simpleBean() {
 +
        return new SimpleBean("Hello from a simple bean");
 +
    }
 +
</source>
 +
 
 +
Or you can let Spring scan your beans (normally used for auto-wiring dependencies) automatically by providing a base package. The declaration of the Spring context component scan element is shown below:
  
 
<source lang="xml" high="8,9">
 
<source lang="xml" high="8,9">
 
<beans xmlns="http://www.springframework.org/schema/beans"
 
<beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:context="http://www.springframework.org/schema/context"
+
      xmlns:context="http://www.springframework.org/schema/context"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
+
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans.xsd
+
          http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context
+
          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context.xsd">
+
          http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan  base-package="org.zkoss.zkspringessentials.beans">
+
    <context:component-scan  base-package="org.zkoss.zkspringessentials.beans"/>
          </context:component-scan>
 
 
</beans>
 
</beans>
 
</source>
 
</source>
Line 59: Line 65:
 
</zscript>
 
</zscript>
 
<window title="Bean in ZScript" width="640px" border="normal" >
 
<window title="Bean in ZScript" width="640px" border="normal" >
     <label value="${msg}"></label>
+
     <label value="${msg}"/>
 
</window>
 
</window>
 
</zk>
 
</zk>
Line 70: Line 76:
 
<zk>
 
<zk>
 
<window title="Bean in EL Expression" width="640px" border="normal" >
 
<window title="Bean in EL Expression" width="640px" border="normal" >
     <label value="${simpleBean.message}"></label>
+
     <label value="${simpleBean.message}"/>
 
</window>
 
</window>
 
</zk>
 
</zk>
Line 83: Line 89:
 
<window title="Bean in Databinding Annotation" width="640px" border="normal"  
 
<window title="Bean in Databinding Annotation" width="640px" border="normal"  
 
     viewModel="...">
 
     viewModel="...">
     <label value="@load(simpleBean.message)"></label>
+
     <label value="@load(simpleBean.message)"/>
 
</window>
 
</window>
 
</zk>
 
</zk>

Revision as of 02:43, 22 January 2019

Using Spring Variable Resolver



Purpose

Access Spring managed bean within ZK framework

DelegatingVariableResolver

You can access any spring managed beans by its id within ZK, for example, on the ZUML page by declaring variable-resolver for DelegatingVariableResolver at the top of your ZUML page.

Lets define a simple bean first

package org.zkoss.zkspringessentials.beans;

public class SimpleBean {

        private String message;
        
        public SimpleBean() {
        }
        public SimpleBean(String msg) {
                this.message = msg;
        }
        public String getMessage() {
                return message;
        }
}

Spring offers different ways of declaring your beans. The classical way is to declare this bean in your applicationContext.xml Spring configuration file as below.

<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
        <constructor-arg value="Hello from a simple bean"/>
</bean>

The alternative Java Config to achieve the same (choose for yourself: Spring and ZK will treat both beans equivalently during runtime):

    @Bean
    public SimpleBean simpleBean() {
        return new SimpleBean("Hello from a simple bean");
    }

Or you can let Spring scan your beans (normally used for auto-wiring dependencies) automatically by providing a base package. The declaration of the Spring context component scan element is shown below:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan  base-package="org.zkoss.zkspringessentials.beans"/>
</beans>

Now using the DelegatingVariableResolver you can acess this bean in ZSCRIPT, EL expressions and ZK data binding annotations

Access Spring beans in ZSCRIPT

You can access SimpleBean by its bean id in ZSCRIPT as shown below

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<zscript>
        String msg = simpleBean.message;
</zscript>
<window title="Bean in ZScript" width="640px" border="normal" >
    <label value="${msg}"/>
</window>
</zk>

Access Spring beans in EL expressions

Similarly you can also access Spring managed beans in any EL expressions as shown below

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<window title="Bean in EL Expression" width="640px" border="normal" >
    <label value="${simpleBean.message}"/>
</window>
</zk>

Access Spring beans in ZK Databinding annotations

The same DelegatingVariableResolver also resolves Spring beans in ZK Databinding expressions:

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<window title="Bean in Databinding Annotation" width="640px" border="normal" 
    viewModel="...">
    <label value="@load(simpleBean.message)"/>
</window>
</zk>

SpringUtil

You can also access Spring beans on a ZUML page without DelegatingVariableResolver by using a utility class SpringUtil method SpringUtil.getBean(). Here is an example code to demonstrate this

<zk>
<zscript>
        import org.zkoss.zkplus.spring.SpringUtil;
        import org.zkoss.zkspringessentials.beans.*;
        SimpleBean simple = SpringUtil.getBean("simpleBean");
        String msg = simple.getMessage();
</zscript>
<window title="Example for SpringUtil#getBean" width="640px" border="normal" >
    <label value="${msg}"/>
</window>
</zk>

Version History

Last Update : 2019/01/22


Version Date Content
     



Last Update : 2019/01/22

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