Results 1 to 6 of 6

Thread: Are static setter/getters allowed?

  1. #1
    Join Date
    Sep 2004
    Posts
    21

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

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

  3. #3
    Join Date
    Oct 2004
    Posts
    1

    Default 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

  4. #4
    Join Date
    Sep 2004
    Posts
    21

    Default

    .... 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)

  5. #5

    Default

    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...

  6. #6

    Default Re: Are static setter/getters allowed?

    Quote 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.

Similar Threads

  1. Replies: 5
    Last Post: Jan 23rd, 2008, 04:30 PM
  2. static initialization block
    By sleyzerzon in forum Container
    Replies: 1
    Last Post: Sep 25th, 2005, 01:28 PM
  3. Replies: 3
    Last Post: Jun 22nd, 2005, 11:01 AM
  4. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM
  5. Replies: 7
    Last Post: Nov 3rd, 2004, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •