Setting null values in spring

Posted on 2011-05-04
Last Modified: 2012-08-13
I was reading folowing lines and I did not understand clearly.

Setting null values:
 what if in order to satisfy a requirement you need to explicitly set a property to null.
 This is really
just another kind of wiring, only in this case we are wiring null instead of a value
or bean.
To set a property to null, you simply use the <null/> element.
For example, to
set a foo property to null, you’d use this:
<property name="foo"><null/><property>
Why would you ever need to do this? If you do not explicitly wire a property in
Spring, you may assume that the property is left null. But that’s not always true.
For one thing, the bean itself may set the property to some default value. Or, if
you are using autowiring, the property may be implicitly wired. In either case, you
may need to use <null/> to explicitly set the property to null.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
Question by:gudii9
    LVL 92

    Expert Comment

    <null/> allows you to explicitly set a property to null

    you need this when a bean initialise a property but you want it to be null

    class MyBean {
       private String xyz = "abc"
    LVL 40

    Accepted Solution

    Setting it in spring is no different than in regular code, you set it to null if you can't assume the property is null or don't know the value of  it. You may not know what the property value is.
    LVL 7

    Author Comment

    >ll if you can't assume the property is null or don't know the value of  it.
    How it helps in future by setting to null now. please advise
    LVL 92

    Assisted Solution

    you simply might want it to be null
    without <null/> there would be no way to set a property to null


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This video teaches viewers about errors in exception handling.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now