A bean is a simple java class which has some properties (or fields) along with their getter and setter methods.

Let’s say following is the bean class with a couple of properties :

package com.codippa;

 public class User {

   private String name;
  
   private String country;
 
   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }
 
   public String getCountry() {
      return country;
   }
 
   public void setCountry(String country) {
      this.country = country;
   }
}

The above bean has two properties, name and country and there are many ways to populate these properties with desired values.
Going ahead we will be discussing those ways in detail.

1. From XML
Let’s say, the above class is declared as a bean in Spring XML configuration file. The declaration of the bean would look like :

<bean id=”user” class=”com.codippa.User”></bean>

In order to populate its values, we need to provide a property tag with name and value attributes where name attribute will be the name of property.
There will be a <value> tag with the value which we want to assign to this property. This <value> tag will be a child tag of the above <property> tag.
Hence the bean tag will look like :

<bean id="user" class="com.codippa.User">
  <property name="name">
    <value>codippa</value>
  </property>
  <property name="country">
    <value>India</value>
  </property>
</bean>

Another variant of the above declaration is its short hand declaration where in a value attribute of the <property> holds the value of the property as :

<bean id="user" class="com.codippa.User">
   <property name="name" value="codippa"/>
   <property name="country" value="India"/>
</bean>

There may be a scenario where the value of bean properties should not be given in XML configuration but have to be fetched from an external properties file such as :

app.user.name=codippa
app.user.country=India

In such cases, load the properties file using spring and instead of giving the values in XML configuration, we provide the name of the property specified in properties file.
This is done using Spring’s ${ }placeholder.
Hence above bean declaration will be modified to :

<bean id="user" class="com.codippa.User">
   <property name="name" value="${app.user.name}"/>
   <property name="country" value="${app.user.country}"/>
</bean>

Let’s tweak in

  1. Bean properties referred in the property element should have valid setter methods otherwise following error will be received.

    Bean property ‘name’ is not writable or has an invalid setter method.

  2. If the property name being referred in the valueattribute does not exist in properties file then
    Could not resolve placeholder ‘app.user.country’ in string value “${app.user.country}” error is thrown
2. Using p-namespace of Spring

Spring’s p-namespace is an alternate way to provide bean property values from XML configuration.
Using this method reduces the amount of configuration and requires lesser number of tags as compared to the above method.

A bean with values injected using p-namespace would look like :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

   <bean id="user" class="com.codippa.User" p:name="codippa" p:country="India"></bean>

</beans>

See how precise the bean definition became. Don’t forget to add p-namespacedeclaration xmlns:p=”http://www.springframework.org/schema/p” to the root of XML configuration.

Let’s tweak in

  1. Again the properties given in the bean declaration should have valid setter methods otherwise an error will be thrown.
  2. Removing the p-namespace declaration from root element of XML configuration will result in an error during application startup as :

    The prefix “p” for attribute “p:name” associated with an element type “bean” is not bound.

  3. If you are using p-namespaceyou cannot define property names that end with Ref.
    For Example, p:-user-ref="codippa".
    This is because the properties ending with -ref are considered to be referring to some other bean.
    Hence, in the above example, Spring will try to search for a bean named codippa and assign it to the property user and will raise an error No bean named ‘codippa’ is defined.

3. @Value annotation
This method involves applying @Value annotation over bean properties whose values are to be injected.
The string provided along with the annotation may either be the value of the bean field or it may refer to a property name from a properties file loaded earlier in Spring context.

A bean with field values supplied using @Valueannotation is shown below.

Note that the name field is directly provided with a value while the country field has value of a property residing inside properties file :

package com.codippa;

  public class User {
     // value supplied here only
     @Value("codippa")
     private String name;
     // value fetched from properties file
     @Value("${app.user.country}")
     private String country;

    //other methods
 }

Let’s tweak in

  1. If the property name being referred by the @Value annotation does not exist in properties file then Could not resolve placeholder ‘app.user.country’ in string value “${app.user.country}” error is thrown
  2. There is NO need of having setter method of the property whose value is injected using this method unlike other methods where an error is thrown in such condition.
  3. It is necessary to provide a value along with this annotation otherwise a compiler error will arise.
    That is, you can not simply  write @Value. You will have to write something like @Value("injectedValue")
Hope the article was useful.

Leave a Reply