Monday 26 February 2018

8.0 Practice Project Using XML Configuration

PROJECT TO WORK FOR PRACTICE (Download)






1. IoC Section














---------------------------------------------------------------------------------------------------------------
Movie.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public interface Movie {
public String getMovie();
}


---------------------------------------------------------------------------------------------------------------
Bollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Bollywood implements Movie {

@Override
public String getMovie() {
return "Bollywood is famous for Acting and movie songs";
}

}


---------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Hollywood implements Movie {

@Override
public String getMovie() {
return "Hollywood Movies are creative";
}

}


---------------------------------------------------------------------------------------------------------------
Tollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Tollywood implements Movie {

@Override
public String getMovie() {
return "Tollywood movies have a good story line";
}

}


---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <bean id="getMovie" class="com.spring.demo.Hollywood">
 </bean>

</beans>



---------------------------------------------------------------------------------------------------------------
Entertainment.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {
public static void main(String args[]){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=(Movie) context.getBean("getMovie",Movie.class);
System.out.println(obj.getMovie());
context.close();
}

}

---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 1:11:11 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 13:11:11 IST 2018]; root of context hierarchy
Feb 26, 2018 1:11:11 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Hollywood Movies are creative
Feb 26, 2018 1:11:11 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 13:11:11 IST 2018]; root of context hierarchy


2. Inject the dependency 
















Inject the dependency  using Constructor injection


---------------------------------------------------------------------------------------------------------------
Category.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public interface Category {
public String getCategory();
}


---------------------------------------------------------------------------------------------------------------
Comedy.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Comedy implements Category {

@Override
public String getCategory() {
return "Watch Comedy movies and laugh out loud";
}

}


---------------------------------------------------------------------------------------------------------------
Entertainment.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {
public static void main(String args[]){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=(Movie) context.getBean("getMovie",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
context.close();
}

}


---------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Hollywood implements Movie {
private Category mycategory;
// constructor injection
public Hollywood(Category theCategory){
mycategory=theCategory;
}
@Override
public String getMovie() {
return "Hollywood Movies are creative";
}

@Override
public String getCategory() {
return mycategory.getCategory();
}

}

---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <bean id="getCategory" class="com.spring.demo.Comedy">
 </bean>
 <bean id="getMovie" class="com.spring.demo.Hollywood">
 <!-- constructor injection -->
 <constructor-arg ref="getCategory"></constructor-arg>
 </bean>

</beans>



---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 2:11:31 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 14:11:31 IST 2018]; root of context hierarchy
Feb 26, 2018 2:11:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Hollywood Movies are creative
Watch Comedy movies and laugh out loud
Feb 26, 2018 2:11:31 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 14:11:31 IST 2018]; root of context hierarchy



Inject the dependency  using Setter injection


---------------------------------------------------------------------------------------------------------------
Action.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Action implements Category {

@Override
public String getCategory() {
return "Watch action movies to refresh your mind and to learn some moves.";
}

}

---------------------------------------------------------------------------------------------------------------
Bollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Bollywood implements Movie {
private Category mycategory;

public Bollywood(){
System.out.println("Bollywood = > in no-arg constructor");
}
//setter injection
public void setMycategory(Category mycategory) {
System.out.println("Bollywwod = > in setMyCategory setter");
this.mycategory = mycategory;
}

@Override
public String getMovie() {
return "Bollywood is famous for Acting and movie songs";
}

@Override
public String getCategory() {
return mycategory.getCategory();
}

}

---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <bean id="getCategory" class="com.spring.demo.Comedy">
 </bean>
  <bean id="getCategory1" class="com.spring.demo.Action">
 </bean>
 <bean id="getMovie" class="com.spring.demo.Hollywood">
 <!-- constructor injection -->
 <constructor-arg ref="getCategory"></constructor-arg>
 </bean>

<bean id="getMovie1" class="com.spring.demo.Bollywood">
<!-- setter injection -->
<property name="mycategory" ref="getCategory1"></property>
</bean>

</beans>



---------------------------------------------------------------------------------------------------------------
Entertainment.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {
public static void main(String args[]){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=(Movie) context.getBean("getMovie",Movie.class);
Movie obj1=(Movie) context.getBean("getMovie1",Movie.class);
System.out.println("getMovie Bean : " + obj.getMovie());
System.out.println("Constructor injection : " + obj.getCategory());
System.out.println("getMovie1 Bean" + obj1.getMovie());
System.out.println("Setter injection : " +obj1.getCategory());
context.close();
}

}

---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 3:54:40 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 15:54:40 IST 2018]; root of context hierarchy
Feb 26, 2018 3:54:40 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Bollywood = > in no-arg constructor
Bollywwod = > in setMyCategory setter
getMovie Bean : Hollywood Movies are creative
Constructor injection : Watch Comedy movies and laugh out loud
getMovie1 BeanBollywood is famous for Acting and movie songs
Setter injection : Watch action movies to refresh your mind and to learn some moves.
Feb 26, 2018 3:54:40 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 15:54:40 IST 2018]; root of context hierarchy


Inject the dependency  using Literals injection


---------------------------------------------------------------------------------------------------------------
Entertainment.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {
public static void main(String args[]){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=(Movie) context.getBean("getMovie",Movie.class);
Movie obj1=(Movie) context.getBean("getMovie1",Movie.class);
System.out.println("getMovie Bean : " + obj.getMovie());
System.out.println("Constructor injection : " + obj.getCategory());
System.out.println("getMovie1 Bean" + obj1.getMovie());
System.out.println("Setter injection : " +obj1.getCategory());
Bollywood obj3=context.getBean("getMovie1",Bollywood.class);
System.out.println("getMovie1 Bean : " +obj3.getEmail());
System.out.println("Literals injection : " +obj3.getName());
context.close();
}

}


---------------------------------------------------------------------------------------------------------------
Bollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Bollywood implements Movie {
private Category mycategory;
//literal injection
private String name;
private String email;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Bollywood(){
System.out.println("Bollywood = > in no-arg constructor");
}
//setter injection
public void setMycategory(Category mycategory) {
System.out.println("Bollywwod = > in setMyCategory setter");
this.mycategory = mycategory;
}

@Override
public String getMovie() {
return "Bollywood is famous for Acting and movie songs";
}

@Override
public String getCategory() {
return mycategory.getCategory();
}

}


---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <bean id="getCategory" class="com.spring.demo.Comedy">
 </bean>
  <bean id="getCategory1" class="com.spring.demo.Action">
 </bean>
 <bean id="getMovie" class="com.spring.demo.Hollywood">
 <!-- constructor injection -->
 <constructor-arg ref="getCategory"></constructor-arg>
 </bean>

<bean id="getMovie1" class="com.spring.demo.Bollywood">
<!-- setter injection -->
<property name="mycategory" ref="getCategory1"></property>

<!-- literal injection -->
<property name="name" value="Bikesh Kumar"></property>
<property name="email" value="bikesh.kumar@tiwari.com"></property>
</bean>

</beans>




---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 4:09:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:09:22 IST 2018]; root of context hierarchy
Feb 26, 2018 4:09:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Bollywood = > in no-arg constructor
Bollywwod = > in setMyCategory setter
getMovie Bean : Hollywood Movies are creative
Constructor injection : Watch Comedy movies and laugh out loud
getMovie1 BeanBollywood is famous for Acting and movie songs
Setter injection : Watch action movies to refresh your mind and to learn some moves.
getMovie1 Bean : bikesh.kumar@tiwari.com
Literals injection : Bikesh Kumar
Feb 26, 2018 4:09:22 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:09:22 IST 2018]; root of context hierarchy


Inject the dependency  using Literals injection from a property 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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder location="classpath:bikesh.properties" />
 <bean id="getCategory" class="com.spring.demo.Comedy">
 </bean>
  <bean id="getCategory1" class="com.spring.demo.Action">
 </bean>
 <bean id="getMovie" class="com.spring.demo.Hollywood">
 <!-- constructor injection -->
 <constructor-arg ref="getCategory"></constructor-arg>
 </bean>

<bean id="getMovie1" class="com.spring.demo.Bollywood">
<!-- setter injection -->
<property name="mycategory" ref="getCategory1"></property>

<!-- literal injection -->
<property name="name" value="${foo.name}"></property>
<property name="email" value="${foo.email}"></property>
</bean>

</beans>



---------------------------------------------------------------------------------------------------------------
bikesh.properties
---------------------------------------------------------------------------------------------------------------
foo.name=Bikesh Kumar
foo.email=bikesh.kumar@tiwari.com




Scope of a BEAN


DEFAULT scope is SINGLETON

---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">



<bean id="getMovie" class="com.spring.demo.Bollywood" scope="singleton">
</bean>

</beans>



---------------------------------------------------------------------------------------------------------------
ScopeEntertainmentTesting.java
---------------------------------------------------------------------------------------------------------------

package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeEntertainmentTesting {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("scope-applicationContext.xml");
Movie obj=context.getBean("getMovie",Movie.class);
Movie obj1=context.getBean("getMovie",Movie.class);
boolean result=(obj==obj1);
System.out.println(result);
System.out.println(obj);
System.out.println(obj1);

}

}


---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 4:25:42 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:25:42 IST 2018]; root of context hierarchy
Feb 26, 2018 4:25:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [scope-applicationContext.xml]
Bollywood = > in no-arg constructor
true
com.spring.demo.Bollywood@18eed359
com.spring.demo.Bollywood@18eed359


PROTOTYPE SCOPE OF A BEAN

---------------------------------------------------------------------------------------------------------------
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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">



<bean id="getMovie" class="com.spring.demo.Bollywood" scope="prototype">
</bean>

</beans>



---------------------------------------------------------------------------------------------------------------
ScopeEntertainmentTesting.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeEntertainmentTesting {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("scope-applicationContext.xml");
Movie obj=context.getBean("getMovie",Movie.class);
Movie obj1=context.getBean("getMovie",Movie.class);
boolean result=(obj==obj1);
System.out.println(result);
System.out.println(obj);
System.out.println(obj1);

}

}

---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------
Feb 26, 2018 4:26:08 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:26:08 IST 2018]; root of context hierarchy
Feb 26, 2018 4:26:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [scope-applicationContext.xml]
Bollywood = > in no-arg constructor
Bollywood = > in no-arg constructor
false
com.spring.demo.Bollywood@4e9ba398
com.spring.demo.Bollywood@6d7b4f4c



init-method & destroy-method

---------------------------------------------------------------------------------------------------------------
Tollywood.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

public class Tollywood implements Movie {

private Category mycategory;

@Override
public String getMovie() {
return "Tollywood movies have a good story line";
}

@Override
public String getCategory() {
return mycategory.getCategory();
}
public void doStuff(){
System.out.println("in doStuff : init-method");
}
public void doMoreStuff(){
System.out.println("in doMoreStuff : destroy-method");
}

}


---------------------------------------------------------------------------------------------------------------
ScopeEntertainmentTesting.java
---------------------------------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeEntertainmentTesting {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("scope-applicationContext.xml");
Movie obj=context.getBean("getMovie",Movie.class);
System.out.println(obj.getMovie());
context.close();

}

}

---------------------------------------------------------------------------------------------------------------
scope-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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">



<bean id="getMovie" class="com.spring.demo.Tollywood"  
init-method="doStuff" 
destroy-method="doMoreStuff"
>
</bean>

</beans>


---------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------

Feb 26, 2018 4:36:08 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:36:08 IST 2018]; root of context hierarchy
Feb 26, 2018 4:36:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [scope-applicationContext.xml]
in doStuff : init-method
Tollywood movies have a good story line
Feb 26, 2018 4:36:09 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 16:36:08 IST 2018]; root of context hierarchy
in doMoreStuff : destroy-method


No comments:

Post a Comment