Convert Boolean bean creation to boolean using Spring

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

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

In the configuration file, I have the following:

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

How do I make this work using primitive boolean?

+5
source share
2 answers

I assume that one way would be to declare a type installer Booleanand let it assign a value to the type field Boolean, i.e.

boolean initializationCache;

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

I have not tested it yet.

+2
source

In Spring 3, you can do this without an intermediate bean with @Value:

@Value("${initialization.cache}")
boolean initializationCache;
+9
source

All Articles