Rajinder Menu

Spring Constructor Dependency Injection Example using Eclipse



This tutorial shows how to inject dependencies to Spring beans using Constructor Dependency Injection method.

This example is using following technologies:

Note: The Spring 3.0 requires at least Java 1.5. 

Step 1: Create a new Project in eclipse.

Create a new project in eclipse by going to File > New > Java Project and name it as SpringDIConstructor as shown in following figure.



Project structure in eclipse is shown below:




There are four files used in this program:

1)      Car.java
2)      Engine.java
3)      MainApp.java
4)      ApplicationContext.java

Create these files in eclipse. If you want to see in detail how a basic Spring project is created in eclipse, you can refer here.

Step2: Add the source code.

Add the following source code in eclipse.

Car.java

package examples.spring.di;

public class Car {

    private String make;
    private String model;
    private int year;
    private Engine engine;

/*Dependency Injection by Constructor*/
public Car(String make,String model,int year,Engine engine){
    this.engine=engine;
    this.model=model;
    this.make=make;
    this.year=year;
   
}
   
public void setMake(String make){
    this.make=make;
}

public String getMake(){
    return make;
}

public void setModel(String model){
    this.model=model;
}

public String getModel(){
    return model;
}

public void setYear(int year){
    this.year=year;
}

public int getYear(){
    return year;
}

public void setEngine(Engine engine){
    this.engine=engine;
}

public Engine getEngine(){
    return engine;
}
public void showCarDetails(){
    System.out.println("Car Details:");
    System.out.println("Make="+getMake());
    System.out.println("Model="+getModel());
    System.out.println("Year="+getYear());
    engine.showEngineDetails();
    }

}

Engine.java 

package examples.spring.di;

public class Engine {
    private String engine;
    private String maxPower;
    private String engineLocation;
   
    /*Dependency Injection by Constructor*/
    public Engine(String engine,String maxPower, String engineLocation){
        this.engine=engine;
        this.maxPower=maxPower;
        this.engineLocation=engineLocation;
               
    }
    public void setEngine(String engine){
        this.engine=engine;
    }
   
    public String getEngine(){
        return engine;
    }
   
    public void setMaxPower(String maxPower){
        this.maxPower=maxPower;
    }
   
    public String getMaxPower(){
        return maxPower;
    }
    public void setEngineLocation(String engineLocation){
        this.engineLocation=engineLocation;
    }
   
    public String getEngineLocation(){
        return engineLocation;
    }
   
    public void showEngineDetails(){
        System.out.println("****Engine Details****");
        System.out.println("Engine="+getEngine());
        System.out.println("Maximum Power="+getMaxPower());
        System.out.println("Engine Location="+getEngineLocation());
    }
}


MainApp.java

package examples.spring.di;

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

public class MainApp {
    public static void main(String args[]){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
        Car car=(Car)ctx.getBean("myCar");
        car.showCarDetails();
    }

}

ApplicationContext.java
<?xml version="1.0" encoding="UTF-8"?>

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

<bean id="myCar" class="examples.spring.di.Car">

<!-- Injecting primitive values as Dependencies-->
  <constructor-arg name="make" value="Maruti Suzuki"/>
  <constructor-arg name="model" value="Wagon R"/>
  <constructor-arg name="year" value="2012"/>

<!-- Injecting reference to other bean as Dependency-->
  <constructor-arg name="engine" ref="myEngine"/>
</bean>

<bean id="myEngine" class="examples.spring.di.Engine">

<!-- Injecting primitive values as Dependencies-->
    <constructor-arg name="engine" value="3664 cc"/>
    <constructor-arg name="maxPower" value="304HP"/>
    <constructor-arg name="engineLocation" value="front"/>
</bean>

</beans>

Step3: Add jars

You will need to add following jars to your application.

1) org.springframework.beans-3.0.6.RELEASE.jar
2) org.springframework.core-3.0.6.RELEASE.jar
3) org.springframework.context-3.0.6.RELEASE.jar
4) org.springframework.asm-3.0.6.RELEASE.jar
5) org.springframework.expression-3.0.6.RELEASE.jar
6) commons-logging-1.1.1.jar 

You can download commons-logging-1.1.1.jar from here. Spring related jars can be found in dist folder of your downloaded Spring 3.0.6 distribution folder.

To add above jars right click on your project SpringDIConstructor,  go to Build Path > Configure Build Path > Add External JARs... and add them one by one.

Step 4: Running the program.
 Right Click on the project SpringDIConstructor and go to Run As > Java Application.

You will see the output  in the Console Pane at bottom.


Hope this would help you!




I would like to know your comments and if you liked the article then please share it on social networking buttons.


What is ApplicationContext? What are its implementations?



ApplicationContext, like BeanFactory, is also used to represent Spring Container. It is built upon BeanFactory interface.

BeanFactory provides basic functionality while ApplicationContext provides advance features to our spring applications which make them enterprise level applications, like i18n, event publishing, JNDI access, EJB integration, Remoting etc.


ApplicationContext is always preferred over BeanFactory and is suitable for J2EE Applications. ApplicationContext includes all functionality of the BeanFactory.

ApplicationContext and Singleton beans:

While using BeanFactory, beans get instantiated when they get requested first time, like in getBean("bean_id") method, not when object of BeanFactory itself gets created. This is known as lazy-instantiation.

But while using ApplicationContext, singleton beans does not get created lazily. By default, ApplicationContext immediately instantiates the singleton beans and wire/set its properties as it's object itself gets created. So ApplicationContext loads singleton beans eagerly (pre-instantiated).

Example:


ApplicationContext acObj=new ClassPathXmlApplicationContext("beanconfig.xml");

MyBean beanObj=(MyBean)acObj.getBean("mybean");

beanObj.someMethod();

In above example we have created an ApplicationContext object acObj using one if its implementations ClassPathXmlApplicationContext which loads the configuration from source beanconfig.xml under classpath.

If we had used BeanFactory here, mybean would get instantiated when the method getBean(mybean) would  be called.

But here with ApplicationContext, instantiation of bean with id mybean does not get delayed until getBean() method is called. If scope of the bean mybean  is declared in configuration file as singleton, it will be immediately instantiated when we create ApplicationContext object acObj. So when getBean() would be called, mybean would already have got loaded and its dependencies set.

We can change this default behavior so that ApplicationContext does not load singleton beans eagerly by using  lazy-init attribute as:

<bean id="mybean" class="x.y.z..MyBean" lazy-init="true"/>

A lazy-initialized bean tells the Spring to create a bean instance when it is first requested, rather than at the time of creation of ApplicationContext object.

Implementations of ApplicationContext:

There are many implementations of ApplicationContext interface. Important ones are:

1)  ClassPathXmlApplicationContext:

It loads bean definitions from XML files located in the classpath.
           
Example1:


ApplicationContext context = new ClassPathXmlApplicationContext("myconfig.xml");



Example2:  Loading configuration from multiple files under classpath.



ApplicationContext context = new ClassPathXmlApplicationContext(
newString[]{"servicesconfig.xml","daoconfig.xml"});


2) FileSystemXmlApplicationContext:

            It loads bean definitions from XML files in the file system.
           
Example:        
           

ApplicationContext context = new FileSystemXmlApplicationContext("c:/myconfig.xml");

3)XmlWebApplicationContext:

XmlWebApplicationContext is used to represent Spring Container for web applications.By defalut Spring creates object of XmlWebApplicationContext class to represent application context/spring container for web applications.

It loads bean definitions from an XML file contained within a web application. By default it loads the configuration from file "/WEB-INF/applicationContext.xml".

If we want to load bean definitions from more than one xml files we can specify their locations in  contextConfigLocation  parameter of ContextLoaderListener or DispatcherServlet in web.xml.Read more about this here.

XmlWebApplicationContext is an implementation of WebApplicationContext interface which in turn extends ApplicationContext interface.




4)AnnotationConfigApplicationContext:

AnnotationConfigApplicationContext class is used when we are using Java-based configuration for the bean definitions instead of Xml files.

In above ApplicationContext implementations (ClassPathXmlApplicationContext, FileSystemXmlApplicationContext) we have supplied bean configuration from xml configuration files. AnnotationConfigApplicationContext class is used to create Spring container which takes bean definitions from java classes annotated with @Configuration, instead of xml files.

It is introduced in Spring 3.0.

Example:


public static void main(String[]args){

/* Creating Spring IoC Container Without XML configuration file*/

ApplicationContext context= new AnnotationConfigApplicationContext(MyConfig.class);

MyBean beanObj = context.getBean(MyBean.class);

beanObj.someMethod();
}

In above code, AnnotationConfigApplicationContext is accepting MyConfig class as input. Here we are obtaining bean definitions from a java class named MyConfig annotated with @Configuration, instead of a Xml file. MyConfig class is described as:


@Configuration
public class MyConfig{
            @Bean
            public MyBean myBeanId(){
            return new MyBean();
            }
}

By giving @Configuration annotation we are treating Myconfig class as  <beans></beans>  tag of xml file.

By giving @Bean annotation we are treating myBean() method as  <bean id="..." class="..."/>


Name of myBeanId() method will be treated as bean id.

Both @Configuration and @Bean are also introduced in Spring 3.0.

5)AnnotationConfigWebApplicationContext:

Like XmlWebApplicationContext is web counterpart for the ClassPathXmlApplicationContext and FileSystemXmlApplicationContext and is used to create application context for web  applications, similarly, AnnotationConfigWebApplicationContext is web counterpart for AnnotationConfigApplicationContext.

AnnotationConfigWebApplicationContext is used to create application context for web applications by using java clases as input for bean definitions instead of xml files.

By default Spring use XmlWebApplicationContext (an implementation of WebApplicationContext) for creating spring container in web applications. But we can change this default value to AnnotationConfigWebApplicationContext by changing the value of contextClass parameter of ContextLoaderListener or DispatcherServlet in web.xml as shown below:

For ContextLoaderListener:
<web-app>
<context-param>
<param-name>contextClass</param-name> 
<param-value>   
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value> 
</context-param> 
<context-param>
<param-name>contextConfigLocation</param-name> 
<!--MyConfig must be annotated with @Configuration-->
<param-value> MyConfig</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener> 
</web-app> 
For  DispatcherServlet:
<web-app>
<servlet>
<servlet-name>mydispatcher</servlet-name>
<servlet-class > org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param> 
<param-name>contextClass</param-name> 
<param-value>            
 org.springframework.web.context.support.AnnotationConfigWebApplicationContext     
</param-value>
</init-param> 
<init-param>   
<param-name>contextConfigLocation</param-name>    
<!--MyConfig must be class annotated with @Configuration-->
<param-value> MyConfig </param-value>
</init-param>
</servlet>
<servlet-mapping> 
<servlet-name>mydispatcher</servlet-name>    
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
I would like to know your comments and if you liked the article then please share it on social networking buttons.

What is Bean Factory and XmlBeanFactory?


BeanFactory is represented by org.springframework.beans.factory.BeanFactory interface.It is the main and the basic  way to access the Spring container.Other ways to access the spring container such as ApplicationContext,ListableBeanFactory, ConfigurableBeanFactory etc. are built upon this BeanFactory interface (extends/implements BeanFactory interface).

BeanFactory interface provides basic functionality for the Spring Container like:

1)It provides DI / IOC mechanism for the Spring.

2)It is built upon Factory Design Pattern.

3)It loads the beans definitions and their property descriptions from some configuration source (for example, from XML configuration  file) .

4)Instantiates the beans when they are requested like beanfactory_obj.getBean("beanId").

5)Wire dependencies and properties for the beans according to their configuration defined in configuration source while instantiating the beans.

6)Manage the bean life cycle by bean lifecycle interfaces and calling initialization and destruction methods.

(Note that BeanFactory does not create the objects of beans immediately when it loads the configuration for beans from configuration source.Only bean definitions and their property descriptions are loaded. Beans themselves are instantiated  and their properties are set only when they are requested such as by getBean() method.)

BeanFactory Implementations:

The most important BeanFactory implementation is –org.springframework.beans.factory.xml.XmlBeanFactory.It reads bean definitions from an XML file.

Constructor for XmlBeanFactory:

XmlBeanFactory(Resource resource)

Example1:


BeanFactory bfObj = new XmlBeanFactory(new FileSystemResource ("c:/beansconfig.xml"));

MyBean beanObj= (MyBean) bfObj.getBean("mybean");


In above code BeanFactory object bfObj is representing a Spring IOC container.

XmlBeanFactory constructor takes an implementation of Resource interface as an argument.
Here we have used FileSystemResource which is one of the implementations of Resource interface.

The Resource interface has many implementaions. Two mainly used are:

1)org.springframework.core.io.FileSystemResource:
              Loads the resource from underlying file system.

2)org.springframework.core.io.ClassPathResource:
              Loads the resource from classpath(shown below).

Purpose of FileSystemResource is to provide the xml file with the given name from underlying file system to XmlBeanFactory.

Now,  bfObj represents a Spring Container which has loaded the bean definitions from the beansconfig.xml file.

At this point only beans definitions got loaded but bean themselves are not instantiated yet.

At the second line,

MyBean beanObj = (MyBean) bfObj.getBean("mybean");

We are requesting from spring container a bean with id "mybean". BeanFactory will read bean definition of a bean with id "mybean" from beansconfig.xml file, instantiates it and return a reference to that. Thus BeanFactory loads the beans lazily.

Example2:


ClassPathResource resorce = new ClassPathResource ("beansconfig.xml");

BeanFactory factory = new XmlBeanFactory(resource);


Example3:


ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});

// an ApplicationContext is also  a BeanFactory.
BeanFactory factory = (BeanFactory) appContext;


Example4:

BeanFactory factory = new XmlBeanFactory(new FileInputStream("beansconfig.xml"));???

Note: BeanFactory is not recomended for use in latest Spring versions. It is there only for backward compatability. ApplicationContext is preferred over this because ApplicationContext provides more advance level features which makes an application enterprise level application.



I would like to know your comments and if you liked the article then please share it on social networking buttons.


WebApplicationContext and XmlWebApplicationContext(applicationContexts used in a Web Applications)


In non-web applications we use ApplicationContext interface to provide beans configuration to our applications from xml files.For example:


ApplicationContext context = new FileSystemXmlApplicationContext("c:/foo.xml");


OR


ApplicationContext context = new ClassPathXmlApplicationContext("foo.xml");


Here we are using two implementations of ApplicationContext interface namely FileSystemXmlApplicationContext and ClassPathXmlApplicationContext for loading configuration from xml files.


But in web applications we use WebApplicationContext interface instead of ApplicationContext interface to provide bean configuration to our web application and to represent the spring container.


WebApplicationContext extends ApplicationContext and has some extra features necessary for web applications.


In web applications, application contexts are provided at two levels(Context Hierarchy):

1)Root application context
2)Application context specific to each servlet defined in web.xml

There is a single root context per application, while each servlet in the application (including a dispatcher servlet in the MVC framework) has its own child context.

Both contexts are represented by WebApplicationContext interface.

Each servlet specific application context inherits the configuration defined in root application context.Thus root application context is shared among all servlet specific application contexts.

 
1)Root application context:

Root application context generally contains configuration information from more than one configuration files representing different layers of application like db layer,service layer etc. Beans specific to each layer should be defined in separate configuration file.For example data access beans should reside in application-context-db.xml, service layer beans should be in application-context-service.xml

It is loaded by using ContextLoaderListener class (org.springframework.web.context.ContextLoaderListener) declared in web.xml file as.

web.xml:


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context-db.xml
/WEB-INF/applicationContext.xml
/WEB-INF/application-context-service.xml
</param-value>
</context-param>

<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

ContextLoaderListener class has two context-param level parameters in web.xml:
1) contextClass  and
2) contextConfigLocation  (shown in above code)
  
contextConfigLocation:
ContextLoaderListener reads the configuration files as defined in contextConfigLocation parameter value.

In above code root application context contains configuration information from three files namely-application-context-db.xml, applicationContext.xml and application-context-service.xml.

If contextConfigLocation parameter is not defined in web.xml then by default ContextLoaderListener will read configuration from  /WEB-INF/applicationContext.xml.

contextClass:
contextClass parameter is used to specify the class which can be used to represent  application context/spring container for our web application. Any class that implements WebApplicationContext interface can be used as value for this parameter. 

If this parameter is not defined in web.xml as in above code, then by default org.springframework.web.context.support.XmlWebApplicationContext class (an implementation of WebApplicationContext interface) is used.

We can change value of this parameter as: 

<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>

AnnotationConfigWebApplicationContext is also an implementation of WebApplicationContext interface.

ContextLoaderlistener delegates this information to org.springframework.web.context.ContextLoader class in order to create root application context.


Once the context files specified in contextConfigLocation  parameter get loaded, Spring creates a WebApplicationContext object (by default XmlWebApplicationContext object) based on the bean definitions and stores it in the ServletContext of the webapplication.

We can obtain this root WebApplicationContext object from ServletContext by using org.springframework.web.context.support.WebApplicationContextUtils class as:


WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);


Now we can use this WebApplicationContext object to obtain beans in our web application as:

MyBean beanObject= (MyBean) ctx.getBean("mybeanId");

getBean() here is inhertited by WebApplicationContext by BeanFactory interface.

2)Servlet Specific Application Context:

Each servlet defined in web.xml has its own  application context  which inherits all configurations defined in root application context. For servlet-specfic contexts,by default configurations are looked up in a file named as /WEB-INF/[servlet-name]-servlet.xml.

Thus two different servlets named servlet1 and servlet2 defined in web.xml will have their separate non-shared configurations (local to their servlet contexts) loaded by default from the files /WEB-INF/servlet1-servlet.xml and /WEB-INF/servlet2-servlet.xml.

For example, suppose we have defined a dispatcherservlet in web.xml as: 

web.xml:



<web-app>
<servlet>
<servlet-name>mydispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mydispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>



Then spring will look for a file named as /WEB-INF/mydispatcher-servlet.xml for bean configurations.

In case of DispactherServlet, there are also two context-param level parameters:
1) contextClass  and
2) contextConfigLocation

They are defined for the same puropse as in ContextLoaderListener described above.




I would like to know your comments and if you liked the article then please share it on social networking buttons.


String Pool in Java



String pool is a pool of string literals which is maintained by java.lang.String class. This pool contains distinct String literals with no two String literals having same value.

Thus a string literal having value “myfriend” can’t exist twice in String pool. There would be only one String literal in a String pool whose value can be “myfriend”.

The main purpose of the presence of String pool is to save memory and increase performance by preventing the creation of  new String objects for those String literals in your code that are already present in the String pool. Thus String pool contains String literals (aka references to String objects) which get shared at many places in your code.


Note that any String literal in your code represents a reference to a String object having same value as the corresponding String literal has.

When we say String pool is pool of String literals it actually means  it is a pool of references, not the pool of  String objects themselves. String objects themselves are on the heap.

How String pool works?
What happens is that whenever a String literal is encountered in your program, then instead of creating a new String object for that literal, JVM first check whether there is any String literal with the same value already exists in String pool. If there is a string literal with the same value already present in the pool, then instead of creating new String object for that encountered String literal in your code, String literal from the pool is used. If there does not exists any string literal with the same value in the pool then a new String object is created, its reference is stored into the string pool and returned.

Consider this program:

public class PoolDemo{
    public static void main(String[] args){

       String firstString = "Hello"; 
       String secondString = "Hello";
       System.out.println(“Hello”);
    }
}

In the above program there are three occurrences of string literal “Hello”. Each occurrence of “Hello” represents an object of String class.

Now suppose String pool does not have any string literal with value “Hello” in it.

Had there not been the string pool, then there would be three different objects of String class containing same value occupying their own memory, like:



But with string pool all three occurrences of “Hello” will point to the same single String object, not the three distinct objects.

When first time “Hello” is referenced in String firstString = "Hello";, JVM will check whether there is already any String literal in the pool with value “Hello”. As we have assumed our string pool does not contain literal “Hello”, a new String object will be created with value “Hello” and its reference is stored into the pool. The reference to this newly created object is returned and assigned to firstString.

When you reference “Hello” second time in String secondString = "Hello"; then again JVM will check the string pool. As now there is already a string literal with same value (“Hello”) present there (just added before), so no new String object is created but the reference to already present object is shared and assigned to secondString .

Same thing happens when you reference the “Hello” third time in System.out.print(“Hello”);, no new object created but reference to “Hello” in String pool is shared.

Now all three occurrences of “Hello” points to single “Hello” object in String pool.



As instead of three different objects now there is only one String object with value “Hello”, it saves memory.

Whether firstString and secondString in above program points to same String object or not can be checked as:

public class PoolDemo{
 public static void main(String[] args){

 String firstString = "Hello";
 String secondString = "Hello";

 //Following line will print true     
 System.out.println(firstString==secondString);
 }
}

firstString == secondString giving result as true means that both firstString  and secondString refer to same String object.

Not every String object goes to String pool…

 String objects can be created in two ways:

1)   String first=”Hello”;

2)   String other=new String(“Hello”);

String objects created by “new”never goes to string pool. Therefore first is pointing to a String object whose reference is in the String pool while other is pointing to String object whose reference does not lie in the String pool.


We can check this by:

public class PoolDemo{
    public static void main(String[] args){

       String first = "Hello"; 
       String other = new String("Hello");

       //Following line will print false
       System.out.println(first==other);
    }
}

first==other comes out false this means that first and other do not point to the same object.

Though strings created “new” do not go to pool, you can store their references in pool by using intern() method of String class.
Interned String means String literal that resides in String pool.

String created as String s=”Hello”; are automatically interned.

While strings created by “new” needs to be explicitly interned if you want them to get stored in string pool

public class PoolDemo{

    public static void main(String[] args){

        String anObject = new String("abc");
        String otherObject =anObject.intern();
 
       //Following line will print false
       System.out.println( anObject == otherObject);
     }
}

anObject does  not represent a String object whose reference is in the String pool. While otherObject represents a String object whose reference is in String pool with same value “abc”. 

Both are different objects as you can see from the result of anObject == otherObject which print false.



Now take this example.

For example:

public class PoolDemo{

    public static void main(String[] args){

       String first = "Hello"; 
       String second = new String("Hello");
       String third=second.intern();

       //Following line will print true
       System.out.println(first==third);
    }

}

second does not represent a String object of pool while first represent a String object in pool. When we called second.intern() what happened is that JVM will try to create a new String object having value “Hello” to store in String pool. But there already presents a String literal there with the same value created by
 String first = "Hello";. So instead of creating new String object it will return the reference of that object from String pool and store it in third. Thus both first and third now points to same object as shown below:




I would like to know your comments and if you liked the article then please share it on social networking buttons.



You may like:
 String Pool is possible due to String immutabilty





String Pool is possible due to String Immutability



String pool in java became possible due to the immutability of the String. The purpose of String pool is to reduce the creation of new String objects in code and this is achieved by sharing the String literals in the String pool at different places in the code. If String were to be mutable, change to a particular String literal at one place would be seen at all other places where this String literal is being used. Thus change at one place would spoil the other parts of code.

If you have not gone through the String pool you may refer String Pool.

Consider this example:

public class StringImmutabilty{
    public static void main(String s[]){
        
        String myName="Humpty Dumpty";
        String yourName="Humpty Dumpty";

        System.out.println("My name is :"+myName);
        System.out.println("Your name is also :"+yourName);
        
        System.out.println("I AM GOING TO CHANGE MY NAME :");

        //Now myName changes to “Dumpty Dumpty”
        myName=myName.replace("Humpty","Dumpty");

        System.out.println("AFTER CHANGE");
        System.out.println("My name is :"+myName);
        System.out.println("Your name is :"+yourName);

    }

}

Output:

My name is :Humpty Dumpty
Your name is also :Humpty Dumpty
I AM GOING TO CHANGE MY NAME :
AFTER CHANGE
My name is :Dumpty Dumpty
Your name is :Humpty Dumpty

In the above code myName and yourName both have same value as String literals so both point to same String object whose reference lies in String pool as shown in following figure:


Now myName has been changed to “Dumpty Dumpty” by

myName=myName.replace("Humpty","Dumpty");


If String were to be mutable, it would also change the value of yourName which is not desirable as we do not want to change yourName.

Since String is immutable it would not change the value of yourName, instead it will create a new String object with value “Dumpty Dumpty” and put its reference in myName as shown in following figure:



Thus due to the String immutability changes at one place to the shared String literal in String pool do not effect other places where there String literal is also being used.



I would like to know your comments and if you liked the article then please share it on social networking buttons.


Spring Setter Dependency Injection Example using Eclipse




This example shows how to inject dependencies to Spring beans using Setter Based Dependency Injection method.

This example is using following technologies:

Note: The Spring 3.0 requires at least Java 1.5. 

Step 1: Create a new Project in eclipse.

Create a new project in eclipse by going to File > New > Java Project and name it as SpringDISetter as shown in following figure.




Project structure in eclipse is shown below:



There are four files used in this program:

1)      Car.java
2)      Engine.java
3)      MainApp.java
4)      ApplicationContext.java

Create these files in eclipse. If you want to see in detail how a basic Spring project is created in eclipse, you can refer here.

Step2: Add the source code.

Add the following source code in eclipse.

Car.java
package examples.spring.di;

public class Car {

    private String make;
    private String model;
    private int year;
    private Engine engine;
  
    /*Dependency Injection by Setter methods*/
    public void setMake(String make){
        this.make=make;
    }
  
    public String getMake(){
        return make;
    }
  
    public void setModel(String model){
        this.model=model;
    }
  
    public String getModel(){
        return model;
    }
  
    public void setYear(int year){
        this.year=year;
    }
  
    public int getYear(){
        return year;
    }
  
    public void setEngine(Engine engine){
        this.engine=engine;
    }
  
    public Engine getEngine(){
        return engine;
    }

    public void showCarDetails(){
        System.out.println("Car Details:");
        System.out.println("Make="+getMake());
        System.out.println("Model="+getModel());
        System.out.println("Year="+getYear());
        engine.showEngineDetails();
      
    }

}

Engine.java 

package examples.spring.di;

public class Engine {
    private String engine;
    private String maxPower;
    private String engineLocation;
   
    /*Dependency Injection by Setter methods*/
    public void setEngine(String engine){
        this.engine=engine;
    }
   
    public String getEngine(){
        return engine;
    }
   
    public void setMaxPower(String maxPower){
        this.maxPower=maxPower;
    }
   
    public String getMaxPower(){
        return maxPower;
    }
    public void setEngineLocation(String engineLocation){
        this.engineLocation=engineLocation;
    }
   
    public String getEngineLocation(){
        return engineLocation;
    }
   
    public void showEngineDetails(){
        System.out.println("****Engine Details****");
        System.out.println("Engine="+getEngine());
        System.out.println("Maximum Power="+getMaxPower());
        System.out.println("Engine Location="+getEngineLocation());
    }

}

MainApp.java

package examples.spring.di;

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

public class MainApp {
    public static void main(String args[]){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
        Car car=(Car)ctx.getBean("myCar");
        car.showCarDetails();
      
        }

}

ApplicationContext.java
<?xml version="1.0" encoding="UTF-8"?>

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

<bean id="myCar" class="examples.spring.di.Car">

    <!-- Injecting primitive values as Dependencies-->
    <property name="make" value="Maruti Suzuki"/>
    <property name="model" value="Wagon R"/>
    <property name="year" value="2012"/>

    <!-- Injecting reference to other bean as Dependency-->
    <property name="engine" ref="myEngine"/>

</bean>

<bean id="myEngine" class="examples.spring.di.Engine">
<!-- Injecting primitive values as Dependencies-->
    <property name="engine" value="3664 cc"/>
    <property name="maxPower" value="304HP"/>
    <property name="engineLocation" value="front"/>
</bean>

</beans>


Step3: Add jars

You will need to add following jars to your application.

1) org.springframework.beans-3.0.6.RELEASE.jar
2) org.springframework.core-3.0.6.RELEASE.jar
3) org.springframework.context-3.0.6.RELEASE.jar
4) org.springframework.asm-3.0.6.RELEASE.jar
5) org.springframework.expression-3.0.6.RELEASE.jar
6) commons-logging-1.1.1.jar 

You can download commons-logging-1.1.1.jar from here. Spring related jars can be found in dist folder of your downloaded Spring 3.0.6 distribution folder.

To add above jars right click on your project SpringDISetter,  go to Build Path > Configure Build Path > Add External JARs... and add them one by one.

Step 4: Running the program.
 Right Click on the project SpringDISetter and go to Run As > Java Application.

You will see the output  in the Console Pane at bottom.

Hope this would be useful to you!





I would like to know your comments and if you liked the article then please share it on social networking buttons.