Announcement Announcement Module
Collapse
No announcement yet.
How to use java 1.5 generics features Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use java 1.5 generics features

    Hi,

    I would like to know if is possible to use generics with BeanFactory and how to use it?

    thank's

    Moa.

  • #2
    Moa,

    Most of Spring's code is written to run on Java 1.3 so you won't be able to take advantage of any generics features with most of Spring codebase. Some specific features are written for 1.4 or 1.5 but as yet there is no generics support in the API.

    Rob

    Comment


    • #3
      Hi Robh, thank's for the fast answer.

      Just one last question, do you know when this feature will be implemented?

      bye.

      Comment


      • #4
        No plans as yet to make Spring dependent on Java 1.5 and its probably not something you will see for some time. We are committed to making sure that the widest range of users can take advantage of Spring so we will continue to support Java 1.3 for the forseeable future.

        Rob

        Comment


        • #5
          Well Java6 is out and Spring can't do generics

          Hello robh,

          it about 3 years ago from your post. Does spring now supports generics?

          Greetings

          astrapi

          Comment


          • #6
            Spring always supported generics, in a certain way. What exactly do you want to do?

            Comment


            • #7
              Hi dejanp,

              here is my code:

              package beans;

              import service.AbstractBusinessService;

              public interface AbstractBackingBean<E, BB, SR, SF> {


              public E getDomainObject();


              public BB getBusinessBean();

              public AbstractBusinessService <E, SR, SF> getBusinessService();

              public void setDomainObject(E pDomainObject);

              }

              ______________________________________________

              package generics;

              import java.util.List;

              import service.AbstractBusinessService;

              public class GenericBackingBean<E, BB, SR, SF> {

              private Class<E> entity;

              private Class<BB> bb;

              private Class<SR> sr;

              private Class<SF> sf;

              private AbstractBusinessService<E, SR, SF> abs;

              public GenericBackingBean(Class<E> entity, Class<BB> bb, Class<SR> sr, Class<SF> sf) {
              this.entity = entity;
              this.bb = bb;
              this.sr = sr;
              this.sf = sf;
              }

              public void saveEntity(E entity) {
              abs.saveVersion(entity);
              }

              public List<SR> findBySearchFilter(SF searchBean) {
              return abs.findBySearchFilter(searchBean);
              }

              }

              _____________________________________________

              My Question:
              How do I define the Class in the applicationContext.xml?

              <bean id="myBackingBean" class="generics.GenericBackingBean">
              <constructor-arg>
              <value>domain.Person</value>
              <value>beans.BusinessBean</value>
              <value>beans.SearchResultBean</value>
              <value>beans.SearchBean</value>
              </constructor-arg>
              </bean>

              Comment


              • #8
                Does it work like that?

                Comment


                • #9
                  No i have problem with the "value"-Element in the "constructor-arg"-Element.

                  How do i make this in Spring?

                  Comment


                  • #10
                    Post the stacktrace of the error please.

                    Comment


                    • #11
                      stacktrace

                      public void testMethodCreatingAPerson() {
                      Resource res = new FileSystemResource("applicationContext.xml");
                      XmlBeanFactory beanFactory = new XmlBeanFactory(res);


                      GenericBackingBean bean = (GenericBackingBean) beanFactory.getBean("myBackingBean");

                      System.out.println(bean);

                      }


                      Im trying to get a reference to the bean and get a stacktrace


                      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myBackingBean' defined in file [C:\Dokumente und Einstellungen\ald99886\workspace\JustTestin\applic ationContext.xml]: 4 constructor arguments specified but no matching constructor found in bean 'myBackingBean' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)
                      Related cause: org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'myBackingBean' defined in file [C:\Dokumente und Einstellungen\ald99886\workspace\JustTestin\applic ationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
                      at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:179)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:893)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:803)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:437)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:404)
                      at java.security.AccessController.doPrivileged(Native Method)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:375)
                      at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 63)
                      at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:170)
                      at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:260 )
                      at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:184)
                      at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:163)
                      at generics.GenericBackingBeanTest.testMethodCreating APerson(GenericBackingBeanTest.java:19)
                      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
                      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
                      at java.lang.reflect.Method.invoke(Unknown Source)
                      at junit.framework.TestCase.runTest(TestCase.java:164 )
                      at junit.framework.TestCase.runBare(TestCase.java:130 )
                      at junit.framework.TestResult$1.protect(TestResult.ja va:106)
                      at junit.framework.TestResult.runProtected(TestResult .java:124)
                      at junit.framework.TestResult.run(TestResult.java:109 )
                      at junit.framework.TestCase.run(TestCase.java:120)
                      at junit.framework.TestSuite.runTest(TestSuite.java:2 30)
                      at junit.framework.TestSuite.run(TestSuite.java:225)
                      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:130)
                      at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
                      Related cause:
                      org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'myBackingBean' defined in file [C:\Dokumente und Einstellungen\ald99886\workspace\JustTestin\applic ationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.Class]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?
                      at org.springframework.beans.factory.support.Construc torResolver.createArgumentArray(ConstructorResolve r.java:556)
                      at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:190)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:893)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:803)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:437)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:404)
                      at java.security.AccessController.doPrivileged(Native Method)
                      at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:375)
                      at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 63)
                      at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:170)
                      at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:260 )
                      at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:184)
                      at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:163)
                      at generics.GenericBackingBeanTest.testMethodCreating APerson(GenericBackingBeanTest.java:19)
                      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
                      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
                      at java.lang.reflect.Method.invoke(Unknown Source)
                      at junit.framework.TestCase.runTest(TestCase.java:164 )
                      at junit.framework.TestCase.runBare(TestCase.java:130 )
                      at junit.framework.TestResult$1.protect(TestResult.ja va:106)
                      at junit.framework.TestResult.runProtected(TestResult .java:124)
                      at junit.framework.TestResult.run(TestResult.java:109 )
                      at junit.framework.TestCase.run(TestCase.java:120)
                      at junit.framework.TestSuite.runTest(TestSuite.java:2 30)
                      at junit.framework.TestSuite.run(TestSuite.java:225)
                      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:130)
                      at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
                      at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

                      Comment


                      • #12
                        I guess it should be like:

                        Code:
                        <bean id="myBackingBean" class="generics.GenericBackingBean">
                           <constructor-arg value="domain.Person"/>
                           <constructor-arg value="beans.BusinessBean"/>
                           <constructor-arg value="beans.SearchResultBean"/>
                           <constructor-arg value="beans.SearchBean"/>
                        </bean>

                        Comment


                        • #13
                          No i changed it to:

                          <bean id="myBackingBean" class="generics.GenericBackingBean">
                          <constructor-arg index="0" type="domain.ConditionGroup">
                          <value>domain.ConditionGroup</value>
                          </constructor-arg>
                          <constructor-arg index="1" type="beans.BusinessBean">
                          <value>beans.BusinessBean</value>
                          </constructor-arg>
                          <constructor-arg index="2" type="beans.SearchResultBean">
                          <value>beans.SearchResultBean</value>
                          </constructor-arg>
                          <constructor-arg index="3" type="beans.SearchBean">
                          <value>beans.SearchBean</value>
                          </constructor-arg>
                          </bean>

                          Comment


                          • #14
                            Still the same stacktrace

                            But its still the same stacktrace.

                            Comment


                            • #15
                              Originally posted by astrapi View Post
                              But its still the same stacktrace.
                              So, you defined 'myBackingBean' bean with four ctor arguments and spring is still not able to create it with the reason '4 constructor arguments specified but no matching constructor found in bean 'myBackingBean''? I guess incorrect bean class is defined at spring config or incorrect config (old config) is used or you use incompatible compiled version of that class (e.g. old version that had three-args ctor etc). The most probable case is that old config is still used. Check the exact file path used by application context via debugger.

                              Comment

                              Working...
                              X