Converting Boolean bean creation to boolean using Spring

11,302

Solution 1

I guess one way to go would be to declare a setter of Boolean type and let it assign the value to a field of boolean type, i.e.

boolean initializationCache;

@Resource(name = "initializationCache")
public void setInitializationCache(Boolean b) {
  this.initializationCache = b;
}

I haven't tested it though.

Solution 2

In Spring 3 you can do it without intermediate bean using @Value:

@Value("${initialization.cache}")
boolean initializationCache;
Share:
11,302

Related videos on Youtube

AHungerArtist
Author by

AHungerArtist

Rubber duckies.

Updated on June 04, 2022

Comments

  • AHungerArtist
    AHungerArtist almost 2 years

    So, I have something like this in one of my java files:

    @Resource(name = "initializationCache")
    Boolean initializationCache;
    

    In a config file, I have this:

    <bean id="initializationCache" class="java.lang.Boolean">
        <constructor-arg value="${initialization.cache}" />
    </bean>
    

    How would I go about making this work using a primitive boolean?

    • Buhake Sindi
      Buhake Sindi over 13 years
      That's basically it, autoboxing will unbox Boolean to boolean.
    • AHungerArtist
      AHungerArtist over 13 years
      But I would prefer to have the initializationCache to default to false if for some reason the config isn't pulled in right. As it is now, it will be null, I believe.
    • Grzegorz Oledzki
      Grzegorz Oledzki over 13 years
      What happens if you just change Boolean to boolean in your Java code?
  • AHungerArtist
    AHungerArtist over 13 years
    Is there a way without going to Spring 3?
  • AHungerArtist
    AHungerArtist over 13 years
    Thanks. I upvoted since this would have been a good answer if I was on 3.0+.

Related