Results 1 to 5 of 5

Thread: P-namespace problem,need help!

  1. #1

    Default P-namespace problem,need help!

    When reading spring reference, some words puzzled me:

    Please note that the p-namespace is not quite as flexible as the standard XML format - for
    example particular, the 'special' format used to declare property references will clash with
    properties that end in 'Ref', whereas the standard XML format would have no problem there.

    I do not know what's this mean, can some one explain for me?

    thanks in advance!

  2. #2
    Join Date
    Apr 2006
    Location
    South Carolina
    Posts
    122

    Default

    Perhaps the following code will clarify. This code is based on the example already introduced in the Spring 2.5.6 documentation under 3.3.2.6.2. When XML file says:
    Code:
    p:spouse-ref="jane"
    ... it is trying to inject an instance of "com.example.Person" into the field named "jane" in your person. Your person object has a field like so:

    Code:
    public class Person {
      private Person spouse;
    
      // getters and setters.
    }
    Just because the text following the "p:" is "spouse-ref" doesn't mean there is a field called "spouse-ref" in the class. Since "-ref" is special, it sees that and looks for a field named just 'spouse' and not 'spouse-ref'.

    Basically, if you use this, don't give your Java fields names ending in "Ref" such as:

    Code:
    public class Person {
      private Person spouseRef;
    
      // getters and setters.
    }

  3. #3

    Default

    Bron, thanks for your reply, my question is: why could not do like following:

    public class Person {
    private Person spouseRef;

    // getters and setters.
    }

    I tried it and it's OK.

    Is you mean that we SHOULD not do like that, not MUST not do like that?


    Quote Originally Posted by Bron View Post
    Perhaps the following code will clarify. This code is based on the example already introduced in the Spring 2.5.6 documentation under 3.3.2.6.2. When XML file says:
    Code:
    p:spouse-ref="jane"
    ... it is trying to inject an instance of "com.example.Person" into the field named "jane" in your person. Your person object has a field like so:

    Code:
    public class Person {
      private Person spouse;
    
      // getters and setters.
    }
    Just because the text following the "p:" is "spouse-ref" doesn't mean there is a field called "spouse-ref" in the class. Since "-ref" is special, it sees that and looks for a field named just 'spouse' and not 'spouse-ref'.

    Basically, if you use this, don't give your Java fields names ending in "Ref" such as:

    Code:
    public class Person {
      private Person spouseRef;
    
      // getters and setters.
    }

  4. #4
    Join Date
    Apr 2006
    Location
    South Carolina
    Posts
    122

    Default

    Hmm. That is kind of confusing. My guess is that it should be avoided for clarity's sake. I don't know if it would break anything mysterious or not. Certainly, you can't have a variable name like spouse-ref anyway because that is not a valid Java identifier.

  5. #5

    Default

    ok, thank you very much, Bron !

Posting Permissions

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