Skip to main content

Posts

Showing posts from March, 2012

Spring - Array configuration using xml bean configuration file

In this example I am going to give example for array configuration in spring application using XML bean configuration file. This is my bean, here I have created the setter and getter method for array variable ("countryArray").

publicclass ArrayConfig {
private String[] countryArray;
public String[] getCountryArray() { returncountryArray;       }
publicvoid setCountryArray(String[] countryArray) { this.countryArray = countryArray;       } }
This is my beans.xml file. The <array> tag is used for configuring array. <?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-3.1.xsd"> <!-- Array Configuration --> <beanid="arrayConfigBean"class="com.arrayconfig.ArrayConfig"> <propertynam…

Spring - ArrayList configuration using xml bean configuration file

In this example I am going to give example for array list configuration in spring application using XML bean configuration file. This is my bean, here I have created the setter and getter method for array list variable ("countryList").

import java.util.ArrayList;
publicclass ArrayListConfig { private ArrayList<String> countryList;
public ArrayList<String> getCountryList() { returncountryList;       }
publicvoid setCountryList(ArrayList<String> countryList) { this.countryList = countryList;       }  }
This is my beans.xml file. The <list> tag is used for configuring list. <?xmlversion="1.0"encoding="UTF-8"?>  <beansxmlns="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-3.1.xsd"> <!-- Array list configuration --> <beanid="co…

Getting key/value pair from JSON object and getting variable name and value from JavaScript object.

Hi, I had faced one issue like this. I have an JSON object but I don't know any key name but I need to get the all the key and corresponding value from JSON object using client side JavaScript. Suddenly I wondered whether it's possible or not, after that I had done lot of workaround and finally got this solution. See the below example.
function getKeyValueFromJSON() {
var jsonObj = {a:10,b:20,c:30,d:50}; for(var key in jsonObj) {       alert("Key: " + key + " value: " + jsonObj[key]);     }  }
 In this example I have created the one json array as string, and converted this string into JSON object using eval() function. Using for-each loop I got all the key value from jsonObj, and finally using that key I got the corresponding value.
 Finally I got the alert like this,

 Key: a value:10
   Key: b value:20
   Key: c value:30
   Key: d value:50

 During this workaround I got one more idea, using this same way I got all the variable name and corresponding value from Java…

Spring bean wiring (Dependency Injection)

In spring application, you have two ways for injecting beans. These are,
 1. Setter based injection.
 2. Constructor based injection.
You can able to use primitive type as well as reference type for injecting.

Example for setter based injection. In this example I am going to inject the room object into home using setter method.
1.  Create a new Room bean like this.
     package com.setterinject;      publicclass Room { private String roomName; public String getRoomName() { returnroomName;        } publicvoid setRoomName(String roomName) { this.roomName = roomName;        } } 2 .Create a new Home bean POJO class like this 
    package com.setterinject;     publicclass Home { private String homeName; private Room room; public String getHomeName() { returnhomeName;       } publicvoid setHomeName(String homeName) { this.homeName = homeName;       }

Spring Containers

The central component of spring application is containers. The spring container manage the life cycle of the bean and it's responsible for wiring of these bean. Wiring nothing but the associating different beans. The spring application provide the two major interfaces namely "BeanFactory" and "ApplicationContext", these two interfaces are act as a containers of spring application.