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?
Using Boolean.parseBoolean () method. This is the most common method to convert String to boolean. This method is used to convert a given string to its primitive boolean value. If the given string contains the value true ( ignoring cases), then this method returns true.
@Bean is a method level annotation, its scopes and method injection. 1. Introduction In this tutorial, We'll learn how to Create Bean in Spring and Spring Boot frameworks. We'll look at @Bean annotation along with its scopes and Method Injection examples. @Bean annotation is introduced in Spring framework to avoid XML level configurations.
Spring's BeanPostProcessor gives us hooks into the Spring bean lifecycle to modify its configuration. BeanPostProcessor allows for direct modification of the beans themselves. In this tutorial, we're going to look at a concrete example of these classes integrating Guava's EventBus .
And also this annotation tells that it returns a bean that is to be managed by the spring container and registered with spring application context or BeanFactory. This method returns a bean of the Customer.
In Spring 3 you can do it without intermediate bean using @Value
:
@Value("${initialization.cache}")
boolean initializationCache;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With