Showing posts with label spring.. Show all posts
Showing posts with label spring.. Show all posts

Spring Bean Configuration Examples

  • XML file that can be developed either based on DTD rules or Schema rules. The XMLfile of certain software technology based applications either based on DTD or Schema rules supplied by that technology.
  • Contains Spring Bean configuration to make Spring Containers to recognize Spring.
  • Contains configuration for setter injections and constructor injections.
  • Any filename.xml can be taken as Spring Bean configuration file name and also contains     some misecllanious configurations related to Spring Beans.

Setting the Bean Properties :

public class ExampleBean {

      private String s;
      private int i;

      public void setStringProperty (String s) { this.s = s; }
      public void setIntegerProperty (int i) { this.i = i; }
  }



  <bean id="exampleBean" class="ExampleBean">
       <property name="stringProperty"><value>Hi!</value></property>
       <property name="integerProperty"><value>1</value></property>
  </bean>


Example:
<property name="intProperty"><value>7</value></property>

<property name="doubleProperty"><value>0.25</value></property>

<property name="booleanProperty"><value>true</value></property>
 

<property name="colorProperty"><value>0,255,0</value></property>

java.awt.Color is initialized with RGB values.

Bean Constructor Configuration:

public class ExampleBean {
      private AnotherBean beanOne;
      private YetAnotherBean beanTwo;
      private int i;
      public ExampleBean (AnotherBean b1, YetAnotherBean b2, int i) {
          this.beanOne = b1;
          this.beanTwo = b2;
          this.i = i;
      }
  }

  <bean id="exampleBean" class="ExampleBean">
      <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
      <constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
      <constructor-arg><value>1</value></constructor-arg>
  </bean>

  <bean id="anotherExampleBean" class="AnotherBean"/>
  <bean id="yetAnotherBean" class="YetAnotherBean"/>



Using Java.util.Properties Example:

<property name="propertiesProperty">
       <value>
           foo=1
           bar=2
           baz=3
       </value>
  </property>

  <property name="propertiesProperty">
       <props>
           <prop key="foo">1</prop>
           <prop key="bar">2</prop>
           <prop key="baz">3</prop>
       </props>
  </property>


List Example:

<property name="listProperty">
      <list>
           <value>a list element</value>
           <ref bean="otherBean"/>
           <ref bean="anotherBean"/>
      </list>
  </property>


Set Example:
 
  <property name="setProperty">
      <set>
           <value>a set element</value>
           <ref bean="otherBean"/>
           <ref bean="anotherBean"/>
      </set>
  </property> 


Map Example:

<property name="mapProperty">
       <map>
           <entry key="JavaStuff">
                <value>Spring Example</value>
           </entry>
           <entry key="yup a ref">
                <ref bean="otherBean"/>
           </entry>
       </map>
  </property> 


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email

How Spring does Inversion of Control ?

  • Write a configuration file in which you name concrete "beans" for the interfaces between your layers.
  • "Wire" the application together by stating which beans are dependent on each other.
  • Instantiate a Spring object called an ApplicationContext. This is a type of bean factory that will instantiate all your other beans and handle dependency injection.

What else has Spring got?

Spring provides either implementations or fully-factored API wrappers over these technologies:
  • JDBC and DAOs
  • ORM: Hibernate, iBatis, TopLink and others
  • Declarative Transaction Support (without a full J2EE app server)
  • Aspect-Oriented Programming
  • Remote calls and Web Services (Axis)
  • EJBs


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email

Spring Basic Bean Creation Example -Tutorial

Bean Creation :
  • Direct instantiation
    • <bean id=“beanId” class=“className”>
  • BeanFactory instantiation
    • Same syntax but class is subclass of BeanFactory
    • getObject() called to obtain Bean
  • Static Factory
    • <bean id=“beanId” class=“className" factory-method=" staticCreationMethod“>
  • Instance Factory Method
    • <bean id=“beanId” factory-bean=“existingBeanId" factory-method=“nonStaticCreationMethod">
  • Beans may be singletons or “prototypes”
    • Attribute singleton=“false” causes instantiation with each getBean() lookup
    • Singleton is default
  • XmlBeanFactory pre-instantiates singletons
    • May be overridden on per-instance basis by lazy-init=“true”
  • Beans may also be marked abstract, allowing reuse of attribute values through inheritance

Basic Bean Creation :

Lets start with a simple program about IoC. 
Spring IoC Bean class :


package com.javastuff.spring;

public class SpringExample {
 public String message = " Welcome to Javastuff ";

 /**
  * @return the message
  */
 public String getMessage() {
  return message;
 }

 /**
  * @param message
  *            the message to set
  */
 public void setMessage(String message) {
  this.message = message;
 }
}

 
Spring IoC Container configuration file :
ApplicationContext.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:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 <bean id="springExample" class="com.javastuff.spring.SpringExample" />

</beans>

Test Class :
package com.javastuff.Test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javastuff.spring.SpringExample;

public class TestClass {
 public static void main(String[] args) {
  BeanFactory factory = new ClassPathXmlApplicationContext(
    "ApplicationContext.xml");
  SpringExample se = (SpringExample) factory.getBean("springExample");
  System.out.println(" Message  : " + se.getMessage());
 }
}


Output :
Message  :  Welcome to Javastuff 


    Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

    Make sure to activate your subscription by clicking on the activation link sent to your email


    Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

    Make sure to activate your subscription by clicking on the activation link sent to your email