Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ZK custom scopes: "desktop", etc. #7

Closed
gushakov opened this issue Oct 9, 2018 · 6 comments
Closed

Support for ZK custom scopes: "desktop", etc. #7

gushakov opened this issue Oct 9, 2018 · 6 comments

Comments

@gushakov
Copy link

gushakov commented Oct 9, 2018

Hello,

Thank you for creating this very useful starter.

In our team we've noticed that, apparently, custom scopes ("desktop", etc.) are not yet supported.

Example:

Declare a converter bean.

@Scope("desktop")
@Component
public class CustomConverter implements Converter<String, String, org.zkoss.zk.ui.Component> {
   // implementation omitted 
}

Register it with a view-model.

<label value="@load('hello world') @converter(vm.customConverter)"/>

Try to use it, there is an exception.

Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'desktop'
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:347) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.zkoss.spring.init.CoreVariableResolver.resolveVariable(CoreVariableResolver.java:61) ~[zkspring-core-3.2.0.jar:3.0]
	at org.zkoss.zkplus.spring.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:100) ~[zkplus-8.5.2.1-Eval.jar:8.5.2.1]
	at org.zkoss.xel.util.Evaluators.resolveVariable(Evaluators.java:200) ~[zcommon-8.5.2.1-Eval.jar:8.5.2.1]
	at org.zkoss.zk.ui.impl.PageImpl.getXelVariable(PageImpl.java:648) ~[zk-8.5.2.1-Eval.jar:8.5.2.1]

Workaround

The way we are working around this is to do what the old <zk-config/> XML configuration was doing.

  1. For this we specify the dependency for zkspring-core-3.2.0 in our POM.
  2. Then use a custom application initializer
public class ZkConfigRegisteringApplicationContextInitializer implements ApplicationContextInitializer<AnnotationConfigServletWebServerApplicationContext> {

    @Override
    public void initialize(AnnotationConfigServletWebServerApplicationContext applicationContext) {
        applicationContext.addBeanFactoryPostProcessor(beanFactory -> {
            // we just need an instance of XmlBeanDefinitionReader configured with this bean factory
            // since the zk-config element is not actually used by ZkConfigDefinitionParser
            final XmlReaderContext xmlReaderContext = new XmlReaderContext(null, null, null, null,
                    new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory), null);
            final ParserContext pc = new ParserContext(xmlReaderContext, null);
            new ZkConfigDefinitionParser().parse(null, pc);
            // custom beans from zk-config are registered at this point
        });
    }
}
  1. And finally use the initializer when running our Boot application
@SpringBootApplication
public class ZkBootApplication {

    public static void main(String[] args) {

        new SpringApplicationBuilder(ZkBootApplication.class)
                .initializers(new ZkConfigRegisteringApplicationContextInitializer())
                .run(args);

    }

}

This way we are able to use the beans with the custom scopes.

Question

This does not seem like a long-term solution since it depends on the old dependency zkspring-core. Would you please consider adding support for custom scoped beans.

Thank you,

George

@cor3000
Copy link
Contributor

cor3000 commented Nov 14, 2018

sorry about the delay ... we were busy with the ZK 8.6.0 release, I'll get back to you soon

@cor3000
Copy link
Contributor

cor3000 commented Nov 14, 2018

I tested the "old-dependency" zkspring-core:3.2.0 and it still works as before. No workaround was needed on my side. I agree a Java configuration approach would be preferable and we'll consider it when upgrading the dependencies for spring 4/5.

For now spring 4/5 still support XML configuration as before ... (especially for legacy libraries)
The simplest config I came up with was the following:

As you did: add the dependency and exclude the obsolete versions (this should be improved on our side in the next release)

	<dependency>
		<groupId>org.zkoss.zk</groupId>
		<artifactId>zkspring-core</artifactId>
		<version>3.2.0</version>
		<exclusions>
			<exclusion>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
			</exclusion>
			<exclusion>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
			</exclusion>
			<exclusion>
				<groupId>org.springframework</groupId>
				<artifactId>spring-web</artifactId>
			</exclusion>
			<exclusion>
				<groupId>org.reflections</groupId>
				<artifactId>reflections</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

Then create a small XML config file - XML does not bite ;)

e.g. /resources/zkscopes-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:zksp="http://www.zkoss.org/2008/zkspring/core"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd">
	<!-- Enables ZK custom scopes for Spring Beans -->
	<zksp:zk-config/>
</beans>

And then import it in the application.

@SpringBootApplication
@Controller
@ImportResource("classpath:zkscopes-config.xml")
public class DemoApplication {
...

When starting spring boot it picks up the config file.

2018-11-14 12:35:24.153 INFO 13272 --- [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [zkscopes-config.xml]

After that I was able to annotate and use beans with @Scope("desktop")

I agree this is quite ugly but still works without any change from the documented usage.

@cor3000
Copy link
Contributor

cor3000 commented Nov 14, 2018

Also I think a java config options should be provided from zkspring-core before adding a config property to the zkspringboot autoconfig project. I created the FR
ZKSPRING-57 to allow simpler configuration.

@cor3000
Copy link
Contributor

cor3000 commented Dec 5, 2018

@gushakov which custom zk scopes are you still using... I assume only desktop and maybe application

The others (idspace, page, component, execution) seem obsolete to me
(even application is somewhat redundant as that is already provided by spring anyway)

I am planning to clean up trying to remove deprecated and obsolete configuration classes

@cor3000
Copy link
Contributor

cor3000 commented Dec 21, 2018

I added a simple version to add the most common ZK scopes webapp/desktop/execution

currently just in a branch 'development'

https://github.com/zkoss/zkspringboot/blob/development/zkspringboot-autoconfig/src/main/java/org/zkoss/zkspringboot/scopes/ZkScopeConfigurer.java

@SpringBootApplication
@Import(ZkScopeConfigurer.class)
public class DemoApplication {

If you needed you can also implement your own CustomScopeConfigurer adding the other scope classes from zkspring-core. Often that's not necessary so I just provided them here.

Would does this work?

@cor3000
Copy link
Contributor

cor3000 commented Feb 19, 2019

zkspring-core 4.0.0 has been released (compatible with spring 4/5) providing a java config option
https://github.com/zkoss/zkspring/releases (maven)

@Import(ZkScopesConfigurer.class)

see:
https://github.com/zkoss/zkspring/blob/master/zkspringessentials/zkspringcoresec/src/main/java/org/zkoss/zkspringessentials/config/ApplicationConfig.java

@cor3000 cor3000 closed this as completed Feb 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants