Announcement Announcement Module
Collapse
No announcement yet.
Are static setter/getters allowed? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Are static setter/getters allowed?

    maybe someone asked that before... I could'nt find any restrictions relating to the usage of static setter/getters. are they allowed in spring?
    Mo

  • #2
    Well, Spring is focused around beans and their instances, but static getters and setters will work just fine.

    Comment


    • #3
      Static properties

      I think the instrospector donīt recognize static getter/setter as a property ... ( at least it donīt display here in jbuilder ) ... I remeber I tried that with spring but i couldn't make it work ... anyway I refactored it and itīs running now

      Comment


      • #4
        .... i don't know sun's bean specification by heart but i can't remember it saying explicitly that using static bean access is forbidden....

        Code:
        public class Person {
          private static String id;
         
          public static String getId() {
            return id;
          }
          public static void setId(String id) {
            Person.id = id;
          }
        }

        edit: it's not that important though....



        org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'defaultPerson' defined in class path resource [Validation.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'id' of bean class [com.moo.example.Person]: Property 'id' is not writable
        org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'id' of bean class [com.moo.example.Person]: Property 'id' is not writable
        at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:630)
        at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:568)
        at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:685)
        at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:712)
        at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:701)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:630)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:467)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:232)
        at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:177)
        at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:159)
        at com.moo.command.PersonTest.testPerson(PersonTest.j ava:41)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at junit.framework.TestCase.runTest(TestCase.java:154 )
        at junit.framework.TestCase.runBare(TestCase.java:127 )
        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:118)
        at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
        at junit.framework.TestSuite.run(TestSuite.java:203)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:421)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:305)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:186)

        Comment


        • #5
          Spring use the Introspector to find property set and get methods, so if you provide your own BeanInfo, I guess static get/set would work alright...

          Comment


          • #6
            Re: Are static setter/getters allowed?

            Originally posted by moo
            maybe someone asked that before... I could'nt find any restrictions relating to the usage of static setter/getters. are they allowed in spring?
            Mo
            Don't do it. How would you mock/stub that class? You can't of course. Use a Singleton bean instead.

            Comment

            Working...
            X