Tuesday, 28 February 2017

Spring: Inject Map

By using <map> element, you can inject map.

Example
 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <map>
    <entry key="Iceland" value="Reykjavik" />
    <entry key="India" value="New Delhi" />
    <entry key="Indonesia" value="Jakarta" />
    <entry key="Iran" value="Tehran" />
    <entry key="Iraq" value="Baghdad" />
   </map>
  </property>
 </bean>

Following is the complete working application.

Countries.java
package com.sample.pojo;

import java.util.Map;

public class Countries {
 private Map<String, String> countries;

 public Map<String, String> getCountries() {
  return countries;
 }

 public void setCountries(Map<String, String> countries) {
  this.countries = countries;
 }

}

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="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <map>
    <entry key="Iceland" value="Reykjavik" />
    <entry key="India" value="New Delhi" />
    <entry key="Indonesia" value="Jakarta" />
    <entry key="Iran" value="Tehran" />
    <entry key="Iraq" value="Baghdad" />
   </map>
  </property>
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import java.util.Map;

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

import com.sample.pojo.Countries;

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

  Countries countries = context.getBean("demoCountryCapitals", Countries.class);

  Map<String, String> countriesMap = countries.getCountries();

  for (String country : countriesMap.keySet()) {
   System.out.println(country + " : " + countriesMap.get(country));
  }

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

Run HelloWorld.java, you can able to see following output.
Iceland : Reykjavik
India : New Delhi
Indonesia : Jakarta
Iran : Tehran
Iraq : Baghdad










Previous                                                 Next                                                 Home

Spring: Inject set

By using <set> element, you can inject set.

Exmaple
 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <set>
    <value>Iceland</value>
    <value>India</value>
    <value>Sri Lanka</value>
    <value>Russia</value>
   </set>
  </property>
 </bean>

Following is the complete working application.

Countries.java
package com.sample.pojo;

import java.util.Set;

public class Countries {
 private Set<String> countries;

 public Set<String> getCountries() {
  return countries;
 }

 public void setCountries(Set<String> countries) {
  this.countries = countries;
 }

}


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="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <set>
    <value>Iceland</value>
    <value>India</value>
    <value>Sri Lanka</value>
    <value>Russia</value>
   </set>
  </property>
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import java.util.Set;

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

import com.sample.pojo.Countries;

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

  Countries countries = context.getBean("demoCountryCapitals", Countries.class);

  Set<String> countryNames = countries.getCountries();

  for (String country : countryNames) {
   System.out.println(country);
  }

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


Run HelloWorld.java, you can able to see following output.
Iceland
India
Sri Lanka
Russia




Previous                                                 Next                                                 Home

Spring: Inject List

By using <list> element, you can inject a java list.

Example
 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <list>
    <value>Iceland</value>
    <value>India</value>
    <value>Sri Lanka</value>
    <value>Russia</value>
   </list>
  </property>
 </bean>

Following is the complete working application.

Countries.java
package com.sample.pojo;

import java.util.List;

public class Countries {
 private List<String> countries;

 public List<String> getCountries() {
  return countries;
 }

 public void setCountries(List<String> countries) {
  this.countries = countries;
 }

}

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="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countries">
   <list>
    <value>Iceland</value>
    <value>India</value>
    <value>Sri Lanka</value>
    <value>Russia</value>
   </list>
  </property>
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

import java.util.List;

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

import com.sample.pojo.Countries;

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

  Countries countries = context.getBean("demoCountryCapitals", Countries.class);

  List<String> countryNames = countries.getCountries();

  for (String country : countryNames) {
   System.out.println(country);
  }

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

Run HelloWorld.java, you can able to see following output.
Iceland
India
Sri Lanka
Russia



Previous                                                 Next                                                 Home

Monday, 27 February 2017

Spring: Inject Properties instance

By using <props> element, you can inject java.util.Properties instance.
public class Countries {
 private Properties countryCapitals;

 .....
 .....
}


Following snippet inject countryCapitals to Countries instance.
 <bean id="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countryCapitals">
   <props>
    <prop key="Iceland">Reykjavik</prop>
    <prop key="India">New Delhi</prop>
    <prop key="Indonesia">Jakarta</prop>
    <prop key="Iran">Tehran</prop>
    <prop key="Iraq">Baghdad</prop>
    <prop key="Ireland">Dublin</prop>
    <prop key="Israel">Jerusalem</prop>
    <prop key="Italy">Rome</prop>
   </props>
  </property>
 </bean>

Following is the complete working application.


Countries.java
package com.sample.pojo;

import java.util.Properties;

public class Countries {
 private Properties countryCapitals;

 public Properties getCountryCapitals() {
  return countryCapitals;
 }

 public void setCountryCapitals(Properties countryCapitals) {
  this.countryCapitals = countryCapitals;
 }

}

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="demoCountryCapitals" name="demoCountryCapitals" class="com.sample.pojo.Countries">
  <property name="countryCapitals">
   <props>
    <prop key="Iceland">Reykjavik</prop>
    <prop key="India">New Delhi</prop>
    <prop key="Indonesia">Jakarta</prop>
    <prop key="Iran">Tehran</prop>
    <prop key="Iraq">Baghdad</prop>
    <prop key="Ireland">Dublin</prop>
    <prop key="Israel">Jerusalem</prop>
    <prop key="Italy">Rome</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.Countries;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });
  
  Countries countries = context.getBean("demoCountryCapitals", Countries.class);
  
  Properties props = countries.getCountryCapitals();
  
  for(Object country: props.keySet()){
   System.out.println(country + " " + props.getProperty((String) country));
  }
  
  ((ClassPathXmlApplicationContext) context).close();
 }
}

Run HelloWorld.java, you can able to see following output.
Iceland Reykjavik
Iran Tehran
Indonesia Jakarta
Israel Jerusalem
Italy Rome
India New Delhi
Ireland Dublin
Iraq Baghdad



Previous                                                 Next                                                 Home

Spring: Defining collections

Spring provides <list/>, <set/>, <map/>, and <props/> elements to define java collections List, Set, Map, and Properties, respectively. My subsequent posts, explain how to define each collection.


Previous                                                 Next                                                 Home

Sunday, 26 February 2017

Spring: Inner beans

By defining <bean> element inside the <property/> or <constructor-arg/> elements defines an inner bean.
 <bean id="book1" class="com.sample.pojo.Book">
  <property name="title" value="Vedanta An Art of Dying" />
  <property name="noOfPages" value="164" />
  <property name="price" value="90" />

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

Observe above snippet, bean ‘osho’ is injected as inner 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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="book1" class="com.sample.pojo.Book">
  <property name="title" value="Vedanta An Art of Dying" />
  <property name="noOfPages" value="164" />
  <property name="price" value="90" />

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

</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 application, 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

Friday, 24 February 2017

Spring: Colloborators: Dependency injection using ref element

In my previous post, I explained how to inject a bean as the value of the property of other bean using ‘ref’ attribute to <property> and <constructor-arg> tags. In this post, I am going to explain how to inject a bean into other bean using ‘ref’ element.

Ex:
<ref bean="someBean"/>

You can specify the bean by specifying any one of the attributes bean, local, or parent attributes.

Ex:
<ref bean="someBean"/>
<ref local="someBean"/>
<ref parent="someBean"/>

Specifying the bean using local attribute
If you are going to specify the bean dependency using ‘local’ attribute, then the dependent bean and its dependency must be in same xml file. The value of the bean attribute may be the same as the id attribute of the target bean, or as one of the values in the name attribute of the target bean. The local attribute on the ref element is no longer supported in the 4.0 beans xsd, So I am not going to explain about this.

Specifying the bean using bean attribute
It is same as local attribute, but it can able to inject the bean that is present in other xml file also.

Following is the complete working example.

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();
 }

}

authors.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="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

</beans>

books.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="book1" class="com.sample.pojo.Book">
  <property name="title" value="Vedanta An Art of Dying" />
  <property name="noOfPages" value="164" />
  <property name="price" value="90" />
  <property name="author">
   <ref bean="osho" />
  </property>
 </bean>
</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[] { "books.xml", "authors.xml" });
  
  Book book = context.getBean("book1", Book.class);
  
  System.out.println(book);

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

Run HelloWorld application, 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