Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Boolean bean creation to boolean using Spring

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?

like image 873
AHungerArtist Avatar asked Nov 22 '10 17:11

AHungerArtist


People also ask

How do I convert a string to a Boolean in Java?

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.

What is @BEAN annotation in spring?

@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.

What is beanpostprocessor in Spring Spring Boot?

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 .

Which method returns a bean of the customer in Spring Boot?

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.


2 Answers

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

@Value("${initialization.cache}")
boolean initializationCache;
like image 79
axtavt Avatar answered Nov 11 '22 22:11

axtavt


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.

like image 44
Grzegorz Oledzki Avatar answered Nov 11 '22 22:11

Grzegorz Oledzki