Click
  1. Click
  2. CLK-375

spring bean name generator for click pages

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.5 M1
    • Fix Version/s: None
    • Component/s: extras
    • Labels:
      None
    • Environment:
      java 1.4, spring framework 2.5

      Description

      Currently, SpringClickServlet is incompatible with @Component annotation of spring framework. For example, it is not possible to write:

      package com.example;

      // ...

      public @Component
      class HomePage extends Page

      { @Autowired private ExampleService exampleService; }

      The reason is SpringClickServlet is looking for bean 'com.example.HomePage', where as spring generates bean name 'homePage'. The solution is to write new bean name generator for click pages. Here is how to use it:

      <context:component-scan
      name-generator="net.sf.click.extras.spring.PageBeanNameGenerator"
      base-package="com.clickweb.quickstart" />
      <context:annotation-config />

      The code for PageBeanNameGenerator.java (jdk1.4, spring framework 2.5):

      package net.sf.click.extras.spring;

      import net.sf.click.Page;

      import org.springframework.beans.factory.config.BeanDefinition;
      import org.springframework.beans.factory.support.BeanDefinitionRegistry;
      import org.springframework.beans.factory.support.BeanNameGenerator;
      import org.springframework.context.annotation.AnnotationBeanNameGenerator;
      import org.springframework.util.ClassUtils;

      public class PageBeanNameGenerator extends AnnotationBeanNameGenerator
      implements BeanNameGenerator {

      public String generateBeanName(BeanDefinition definition,
      BeanDefinitionRegistry registry) {
      try {
      final Class clazz = ClassUtils.forName(definition
      .getBeanClassName());
      if (ClassUtils.isAssignable(Page.class, clazz))

      { return definition.getBeanClassName(); }

      } catch (Exception e)

      { // do nothing... use default bean name generator }

      return super.generateBeanName(definition, registry);
      }

      }

        Activity

        Hide
        Alexei Sokolov added a comment -

        spring configuration example:

        <context:component-scan
        name-generator="net.sf.click.extras.spring.PageBeanNameGenerator"
        scope-resolver="net.sf.click.extras.spring.PageScopeResolver"
        base-package="com.clickweb">
        <context:include-filter type="assignable" expression="net.sf.click.Page" />
        </context:component-scan>

        Show
        Alexei Sokolov added a comment - spring configuration example: <context:component-scan name-generator="net.sf.click.extras.spring.PageBeanNameGenerator" scope-resolver="net.sf.click.extras.spring.PageScopeResolver" base-package="com.clickweb"> <context:include-filter type="assignable" expression="net.sf.click.Page" /> </context:component-scan>
        Hide
        Alexei Sokolov added a comment -

        Malcolm,

        I take my ok on your solution back, because it does not work when multiple pages in different packages use the same name. So fully-qualified page class name is a must for page bean name.

        Alex

        Show
        Alexei Sokolov added a comment - Malcolm, I take my ok on your solution back, because it does not work when multiple pages in different packages use the same name. So fully-qualified page class name is a must for page bean name. Alex
        Hide
        Alexei Sokolov added a comment -

        Yes, that is ok solution. However, I think custom bean name generator is a better solution, because it avoids duplicate lookups.

        Alex

        Show
        Alexei Sokolov added a comment - Yes, that is ok solution. However, I think custom bean name generator is a better solution, because it avoids duplicate lookups. Alex
        Hide
        Malcolm Edgar added a comment -

        Hi Alex,

        I presume the problem is with Spring instantiated pages.

        Would it help if the Click application attempted to lookup a page "homePage" if the "HomePage" lookup failed?

        regards Malcolm Edgar

        Show
        Malcolm Edgar added a comment - Hi Alex, I presume the problem is with Spring instantiated pages. Would it help if the Click application attempted to lookup a page "homePage" if the "HomePage" lookup failed? regards Malcolm Edgar

          People

          • Assignee:
            Malcolm Edgar
            Reporter:
            Alexei Sokolov
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development