Thursday, 23 March 2017

Spring: Inject bean as constructor argument using c name space

In my previous post, I explained how to inject arguments to a constructor using c-namespace. In this post, I am going to explain how to inject a bean using c-namespace.

Example       
 <bean id="osho" class="com.sample.pojo.Author">
  <constructor-arg name="firstName" value="Chandra Mohan" />
  <constructor-arg name="lastName" value="Jain" />
  <constructor-arg name="dateOfBirth" value="11 December 1931" />
  <constructor-arg name="country" value="India" />
 </bean>

 <bean id="bookOfSecrets" name="book1" class="com.sample.pojo.Book">
  <constructor-arg name="author" ref="osho" />
  <constructor-arg name="title" value="The Book Of Secrets" />
  <constructor-arg name="noOfPages" value="1152" />
  <constructor-arg name="price" value="452" />
 </bean>

Notify above snippet, I am injecting author bean into the bean with id book1. Same snippet can be re-written using c-namespace like below.

 <bean id="osho" name="osho" class="com.sample.pojo.Author"
  c:firstName="Chandra Mohan" c:lastName="Jain" c:dateOfBirth="11 December 1931"
  c:country="india" />

 <bean id="bookOfSecrets" name="book1" class="com.sample.pojo.Book"
  c:title="Vedanta An Art of Dying" c:noOfPages="164" c:price="90"
  c:author-ref="osho" />

Notify above snippet constructor argument ‘author’ of the instance book1 is defined as c:author-ref="osho". In this case author is the argument name, whereas the -ref part indicates that this is not a straight value but rather a reference to another bean.
  
Following is the complete working application.


Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public Author(String firstName, String lastName, String dateOfBirth, String country) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.dateOfBirth = dateOfBirth;
  this.country = country;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}


Book.java

package com.sample.pojo;

public class Book {
 private String title;
 private int noOfPages;
 private float price;
 private Author author;
 
 public Book(String title, int noOfPages, float price, Author author){
  this.title = title;
  this.noOfPages = noOfPages;
  this.price = price;
  this.author = author;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public int getNoOfPages() {
  return noOfPages;
 }

 public void setNoOfPages(int noOfPages) {
  this.noOfPages = noOfPages;
 }

 public float getPrice() {
  return price;
 }

 public void setPrice(float price) {
  this.price = price;
 }

 public Author getAuthor() {
  return author;
 }

 public void setAuthor(Author author) {
  this.author = author;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Book [title=").append(title).append(", noOfPages=").append(noOfPages).append(", price=")
    .append(price).append(", author=").append(author).append("]");
  return builder.toString();
 }

}

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

 <bean id="osho" name="osho" class="com.sample.pojo.Author"
  c:firstName="Chandra Mohan" c:lastName="Jain" c:dateOfBirth="11 December 1931"
  c:country="india" />

 <bean id="bookOfSecrets" name="book1" class="com.sample.pojo.Book"
  c:title="Vedanta An Art of Dying" c:noOfPages="164" c:price="90"
  c:author-ref="osho" />

</beans>

HelloWorld.java

package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Book;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Book book = context.getBean("book1", Book.class);

  System.out.println(book);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run ‘HelloWorld.java’, you can able to see following output.

Book [title=Vedanta An Art of Dying, noOfPages=164, price=90.0, author=Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=india]]




Previous                                                 Next                                                 Home

Tuesday, 21 March 2017

Spring: Working with c-namespace

In my previous posts, I explained how to work with p-name space. Spring also provides c-namespace, where you can configure constructor arguments inline, rather than by using constructor-arg element.

Example
<bean id="531" name="kiran" class="com.sample.pojo.Employee">
 <constructor-arg name="lastName" value="Darsi" />
 <constructor-arg name="id" value="531" />
 <constructor-arg name="firstName" value="Kiran Kumar" />
</bean>

Above snippet can be written using c-namespace like below.

<bean id="531" name="kiran" class="com.sample.pojo.Employee" c:lastName="foo@bar.com" c:id="531" c:firstName="Kiran Kumar" />

Following is the complete working application.

Employee.java
package com.sample.pojo;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
    .append(lastName).append("]");
  return builder.toString();
 }

}


myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="531" name="kiran" class="com.sample.pojo.Employee"
  c:lastName="foo@bar.com" c:id="531" c:firstName="Kiran Kumar" />
  
</beans>

HelloWorld.java

package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Employee;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Employee employee = context.getBean("kiran", Employee.class);

  System.out.println(employee);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.

Employee [id=531, firstName=Kiran Kumar, lastName=foo@bar.com]




Previous                                                 Next                                                 Home

Friday, 17 March 2017

Spring: Inject bean using p-namespace

In my previous post, I explained how to inject properties to a bean using p-namespace. In this post, I am going to explain how to inject bean as property to other bean using p-namespace.

Exmaple
 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />
  
 <bean name="book1" class="com.sample.pojo.Book" 
  p:title="Vedanta An Art of Dying"
  p:noOfPages = "164"
  p:price = "90"
  p:author-ref="osho" />

Notify above snippet property ‘author’ of the instance book1 is defined as p:author-ref="osho". In this case author is the property name, whereas the -ref part indicates that this is not a straight value but rather a reference to another bean.

Following is the complete working application.

Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

Book.java
package com.sample.pojo;

public class Book {
 private String title;
 private int noOfPages;
 private float price;
 private Author author;

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public int getNoOfPages() {
  return noOfPages;
 }

 public void setNoOfPages(int noOfPages) {
  this.noOfPages = noOfPages;
 }

 public float getPrice() {
  return price;
 }

 public void setPrice(float price) {
  this.price = price;
 }

 public Author getAuthor() {
  return author;
 }

 public void setAuthor(Author author) {
  this.author = author;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Book [title=").append(title).append(", noOfPages=").append(noOfPages).append(", price=")
    .append(price).append(", author=").append(author).append("]");
  return builder.toString();
 }

}

myConfiguration.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />

 <bean name="book1" class="com.sample.pojo.Book" p:title="Vedanta An Art of Dying"
  p:noOfPages="164" p:price="90" p:author-ref="osho" />

</beans>

HelloWorld.java

package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Book;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Book book = context.getBean("book1", Book.class);

  System.out.println(book);

  ((ClassPathXmlApplicationContext) context).close();
 }
}



Previous                                                 Next                                                 Home

Wednesday, 15 March 2017

Spring: p-namespace

Spring provides p-namespace, to define properties of a bean in convenient way.

For example,
 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="country" value="India" />
  <property name="dateOfBirth" value="11 December 1931" />
 </bean>

Above bean definition can be written like below using p-namespace.
 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />

Following is the complete working application.


Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

 <bean name="osho" class="com.sample.pojo.Author" p:firstName="Chandra Mohan"
  p:lastName="Jain" p:country="India" p:dateOfBirth="11 December 1931" />

</beans>

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Author;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Author author = context.getBean("osho", Author.class);

  System.out.println(author);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java application, you can able to see following output.
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]


Previous                                                 Next                                                 Home

Spring: Assign null values to properties

By using <null/> element, you can assign null values to the properties.

Example
 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="country" value="India" />

  <property name="dateOfBirth">
   <null />
  </property>
 </bean>

Notice above snippet, it passes null to the property dateOfBirth.

Following is the complete working application.

Author.java
package com.sample.pojo;

public class Author {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

}

myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="country" value="India" />

  <property name="dateOfBirth">
   <null />
  </property>
 </bean>


</beans>

HelloWorld.java
package com.sample.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Author;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Author author = context.getBean("osho", Author.class);

  System.out.println(author);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=null, country=India]






Previous                                                 Next                                                 Home

Spring: Collections merging

Spring API provides support to merge collections. You can define a bean such that, the collection of elements of child bean can be inherited form parent bean.

Example
 <bean id="parent" abstract="true" class="com.sample.pojo.Organization">
  <property name="orgEmails">
   <props>
    <prop key="administrator">administrator@example.com</prop>
    <prop key="support">support@example.com</prop>
   </props>
  </property>
 </bean>

 <bean id="child" parent="parent">
  <property name="orgEmails">
   <!-- the merge is specified on the child collection definition -->
   <props merge="true">
    <prop key="sales">sales@example.com</prop>
    <prop key="support">support@example.co.in</prop>
   </props>
  </property>
 </bean>

Notice the use of the merge=true attribute on the <props/> element of the orgEmails property of the child bean definition. When the child bean is resolved and instantiated by the container, the resulting instance has an orgEmails Properties collection that contains the result of the merging of the child’s orgEmails collection with the parent’s orgEmails collection.

The child Properties collection’s value set inherits all property elements from the parent <props/>, and the child’s value for the support value overrides the value in the parent collection.

Following is the complete working application.

Organization.java
package com.sample.pojo;

import java.util.Properties;

public class Organization {
 private Properties orgEmails;

 public Properties getOrgEmails() {
  return orgEmails;
 }

 public void setOrgEmails(Properties orgEmails) {
  this.orgEmails = orgEmails;
 }

}

myConfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="parent" abstract="true" class="com.sample.pojo.Organization">
  <property name="orgEmails">
   <props>
    <prop key="administrator">administrator@example.com</prop>
    <prop key="support">support@example.com</prop>
   </props>
  </property>
 </bean>

 <bean id="child" parent="parent">
  <property name="orgEmails">
   <!-- the merge is specified on the child collection definition -->
   <props merge="true">
    <prop key="sales">sales@example.com</prop>
    <prop key="support">support@example.co.in</prop>
   </props>
  </property>
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.pojo.Organization;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Organization organization = context.getBean("child", Organization.class);

  Properties props = organization.getOrgEmails();
  
  for(Object property: props.keySet()){
   System.out.println(property + " : " + props.get(property));
  }

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.
support : support@example.co.in
administrator : administrator@example.com
sales : sales@example.com

In the same way you can apply merging to all supported collecitons like List, Map, Set.

Can I merge Collections of different types?

No, you can’t merge collections of different types. For example, if you try to merger Map collection to List, you will get an exception.



Previous                                                 Next                                                 Home