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;
Related videos on Youtube
Comments
-
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 over 13 yearsThat's basically it, autoboxing will unbox
Boolean
toboolean
. -
AHungerArtist over 13 yearsBut 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 over 13 yearsWhat happens if you just change
Boolean
toboolean
in your Java code?
-
-
AHungerArtist over 13 yearsIs there a way without going to Spring 3?
-
AHungerArtist over 13 yearsThanks. I upvoted since this would have been a good answer if I was on 3.0+.