Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-2771

Please support "isPrimary" for @SpringBean

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • None
    • 1.4.8, 1.5-M1
    • wicket-spring
    • None

    Description

      Our application must be adaptable for customer needs. We therefore need to be able to inject customer specific services. Since all our services are autowired, we could solve this by using the "primary" attribute.

      Our propsal is to patch SpringBeanLocator:

      private final String getBeanNameOfClass(ApplicationContext ctx, Class< ? > clazz) {
      // get the list of all possible matching beans
      List<String> names = new ArrayList<String>(Arrays.asList(BeanFactoryUtils .beanNamesForTypeIncludingAncestors(ctx, clazz)));
      Iterator<String> it = names.iterator();

      // filter out beans that are not condidates for autowiring
      while (it.hasNext()) {
      final String possibility = it.next();
      if (BeanFactoryUtils.isFactoryDereference(possibility) || possibility.startsWith("scopedTarget."))

      { it.remove(); }

      }
      if (names.isEmpty())

      { throw new IllegalStateException("bean of type [" + clazz.getName() + "] not found"); }

      else if (names.size() > 1) {
      ///////////////////////// add this ///////////////////////////////////////////////
      for (String name : names){
      BeanDefinition beanDef = ((AbstractApplicationContext)ctx).getBeanFactory().getBeanDefinition(name);
      if(((AbstractBeanDefinition)beanDef).isPrimary())

      { return name; }

      }
      //////////////////////////////////////////////////////////////////////////////////////

      // found no match

      StringBuilder msg = new StringBuilder();
      msg.append("more then one bean of type [");
      msg.append(clazz.getName());
      msg.append("] found, you have to specify the name of the bean ");
      msg.append("(@SpringBean(name=\"foo\")) in order to resolve this conflict. ");
      msg.append("Matched beans: ");
      msg.append(Strings.join(",", names.toArray(new String[0])));
      throw new IllegalStateException(msg.toString());
      }
      else

      { return names.get(0); }

      }

      Attachments

        Activity

          People

            jdonnerstag Juegen Donnerstag
            kbaumgar Kathrin Baumgartner
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: