Monday, 26 February 2018

11.0 SPRING MVC

SPRING MODEL VIEW CONTROLLER




















  • Client send a reqeust through web Application.
  • DispatcherServlet which is the first controller receives the request.
  • DispatcherServlet consults the HandlerMapping to identify the particular controller for the given URL.
  • HandlerMapping identifies the controller for the given request and sends to the DispatcherServlet.
  • DispatcherServlet will call the handleRequest(request,response) method on Controller. A Controller is developed by writing a simple java class which implements Controller interface or extends its adapter class.
  • Controller will call the business method according to business requirement.
  • Service class will call the DAO class method for business data.
  • DAO interacts with DB to get data.
  • DAO returns same data to service.
  • Fetched data will be processed according to business requirement and return results to Controller.
  • The Controller returns the Model and View in the form of Object back to the Controller i.e, DispatcherServlet.
  • The front controller i.e, DispatcherServlet then tries to resolve the actual View which may be JSP,velocity or Free Marker by consulting the View Resolver Object.
  • ViewResolver selected view is rendred back to the DispatcherServlet.
  • DispatcherServletconsult the particular view with the model.
  • View executes and returns HTML output to the DispatcherServlet.
  • DispatcherServlet will sends the output to the Browser.
Read More »

10.0 Spring Container Using Java Code


Spring Container Configuration using pure Java class

Instead of configuring Spring container using XML we'll use pure java class to configure.

Three ways to configure Spring container

  1. Full XML Config
  2. XML Component Scan
  3. Java Configuration Class

Java Configuration Class for configuring Spring Container





















Configuring Java class instead of xml


@Configuration

@ComponentScan("com.demo.spring")


---------------------------------------------------------------------------------------------------------------------
Movie.java
---------------------------------------------------------------------------------------------------------------------


package com.demo.spring;

public interface Movie {
public String getMovie();
public String getCategory();
}


---------------------------------------------------------------------------------------------------------------------
Category.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

public interface Category {
public String getCategory();
}

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

import org.springframework.stereotype.Component;

@Component
public class Comedy implements Category {

@Override
public String getCategory() {
return "Life is a comedy";
}

}

---------------------------------------------------------------------------------------------------------------------
Bollywood.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("bollywood")
public class Bollywood implements Movie {
@Autowired
Category cat;
@Override
public String getMovie() {
return "Bollywood movies have thousands of varities";
}

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

}

---------------------------------------------------------------------------------------------------------------------
Comedy.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.stereotype.Component;

@Component
public class Comedy implements Category {

@Override
public String getCategory() {
return "Life is a comedy";
}

}

---------------------------------------------------------------------------------------------------------------------
MyApp.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
context.close();
}
}


---------------------------------------------------------------------------------------------------------------------
MovieConfig.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.demo.spring")
public class MovieConfig {

}


---------------------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------------------
Feb 27, 2018 3:07:54 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:07:54 IST 2018]; root of context hierarchy
Bollywood movies have thousands of varities
Life is a comedy
Feb 27, 2018 3:07:54 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:07:54 IST 2018]; root of context hierarchy



Configuring bean in java config without using  @ComponentScan

---------------------------------------------------------------------------------------------------------------------
Action.java // no @Component annotation
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

public class Action implements Category {

@Override
public String getCategory() {
return "Coding is the only Action i believe in";
}

}

---------------------------------------------------------------------------------------------------------------------
Hollywood.java - // basic constructor injection
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.beans.factory.annotation.Value;

public class Hollywood implements Movie {

private Category cat;
public Hollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Graphics in Hollywood movies is a real-feel kind";
}

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

}

---------------------------------------------------------------------------------------------------------------------
MovieConfig.java -// defining the individual bean without @ComponentScan
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
public class MovieConfig {

//defining bean for action category note: getCategory() is the bean id
@Bean
public Category getCategory(){
return new Action();
}
//define bean for Hollywood and inject dependency // hollywood is the bean id
@Bean
public Movie hollywood(){
return new Hollywood(getCategory());
}
}


---------------------------------------------------------------------------------------------------------------------
HollywoodConfigJavaDemo.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HollywoodConfigJavaDemo {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("hollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());

}
}

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

Feb 27, 2018 3:14:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:14:45 IST 2018]; root of context hierarchy
Graphics in Hollywood movies is a real-feel kind
Coding is the only Action i believe in
Feb 27, 2018 3:14:45 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:14:45 IST 2018]; root of context hierarchy


Reading variables form a property file

literal injection using @PropertySource("classpath:bikesh.properties") &

@Value

---------------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.beans.factory.annotation.Value;

public class Hollywood implements Movie {

private Category cat;
@Value("${foo.name}")
private String name;
public String getName() {
return name;
}
public String getEmail() {
return email;
}

@Value("${foo.email}")
private String email;
public Hollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Graphics in Hollywood movies is a real-feel kind";
}

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

}

---------------------------------------------------------------------------------------------------------------------
MovieConfig.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:bikesh.properties")
public class MovieConfig {

//defining bean for action category
@Bean
public Category getCategory(){
return new Action();
}
//define bean for Hollywood and inject dependency
@Bean
public Movie hollywood(){
return new Hollywood(getCategory());
}
}


---------------------------------------------------------------------------------------------------------------------
Hollywood.java
---------------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HollywoodConfigJavaDemo {
public static void main(String args[])
{
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(MovieConfig.class);
Movie obj=context.getBean("hollywood",Movie.class);
System.out.println(obj.getMovie());
System.out.println(obj.getCategory());
Hollywood obj1=context.getBean("hollywood",Hollywood.class);
System.out.println(obj1.getName());
System.out.println(obj1.getEmail());
context.close();
}
}

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

Feb 27, 2018 3:15:26 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:15:26 IST 2018]; root of context hierarchy
Graphics in Hollywood movies is a real-feel kind
Coding is the only Action i believe in
Bikesh Kumar
bikesh.kumar@tiwari.com
Feb 27, 2018 3:15:27 AM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1963006a: startup date [Tue Feb 27 03:15:26 IST 2018]; root of context hierarchy



Read More »

9.0 Practice Project using Spring Annotations

PROJECT TO WORK FOR PRACTICE (DOWNLOAD)





















1. IoC Section


@Component annotation















------------------------------------------------------------------------------------------------------------
Movie.java
------------------------------------------------------------------------------------------------------------

package com.demo.spring;

public interface Movie {
public String getMovie();
}


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

import org.springframework.stereotype.Component;

@Component("bollywood")
public class Bollywood implements Movie {

@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}

}


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

import org.springframework.stereotype.Component;

@Component("hollywood")
public class Hollywood implements Movie {

@Override
public String getMovie() {
return "Hollywood is famous for its creativity.";
}

}


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

import org.springframework.stereotype.Component;

@Component("tollywood")
public class Tollywood implements Movie {

@Override
public String getMovie() {
return "Tollywood is famous for storyline";
}

}


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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println("Bollywood : "+obj.getMovie());
Movie obj1=context.getBean("hollywood",Movie.class);
System.out.println("Hollywood : "+obj1.getMovie());
Movie obj2=context.getBean("tollywood",Movie.class);
System.out.println("Tollywood : "+obj2.getMovie());
context.close();

}

}

------------------------------------------------------------------------------------------------------------
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:component-scan base-package="com.demo.spring"></context:component-scan>
</beans>

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

Feb 26, 2018 5:21:22 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:21:22 IST 2018]; root of context hierarchy
Feb 26, 2018 5:21:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Bollywood : Bollywood id famous for acting and songs
Hollywood : Hollywood is famous for its creativity.
Tollywood : Tollywood is famous for storyline
Feb 26, 2018 5:21:22 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:21:22 IST 2018]; root of context hierarchy


@Autowired annotation

Constructor injection


------------------------------------------------------------------------------------------------------------
Movie.java
------------------------------------------------------------------------------------------------------------

package com.demo.spring;

public interface Movie {
public String getMovie();
public String getCategory();
}


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("bollywood")
public class Bollywood implements Movie {
private Category cat;
//constructor-injection
@Autowired
public Bollywood(Category getcat){
cat=getcat;
}
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}

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

}


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("hollywood")
public class Hollywood implements Movie {
private Category cat;
//constructor injection
@Autowired
public Hollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Hollywood is famous for its creativity.";
}

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

}


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("tollywood")
public class Tollywood implements Movie {
private Category cat;
//constructor injection
@Autowired
public Tollywood(Category thecat){
cat=thecat;
}
@Override
public String getMovie() {
return "Tollywood is famous for storyline";
}

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

}

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

import org.springframework.stereotype.Component;


public interface Category {
public String getCategory();
}


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

import org.springframework.stereotype.Component;

@Component
public class Comedy implements Category {

@Override
public String getCategory() {
return "Commedy is for fun";
}

}

------------------------------------------------------------------------------------------------------------
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:component-scan base-package="com.demo.spring"></context:component-scan>
</beans>



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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println("Bollywood : "+obj.getMovie());
System.out.println("Bollywood category : "+obj.getCategory());
Movie obj1=context.getBean("hollywood",Movie.class);
System.out.println("Hollywood : "+obj1.getMovie());
System.out.println("Hollywood category : "+obj1.getCategory());
Movie obj2=context.getBean("tollywood",Movie.class);
System.out.println("Tollywood : "+obj2.getMovie());
System.out.println("Tollywood category : "+obj2.getCategory());
context.close();

}

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 5:38:04 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:38:04 IST 2018]; root of context hierarchy
Feb 26, 2018 5:38:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Bollywood : Bollywood id famous for acting and songs
Bollywood category : Commedy is for fun
Hollywood : Hollywood is famous for its creativity.
Hollywood category : Commedy is for fun
Tollywood : Tollywood is famous for storyline
Tollywood category : Commedy is for fun
Feb 26, 2018 5:38:04 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:38:04 IST 2018]; root of context hierarchy


@Autowired annotation

Setter injection


------------------------------------------------------------------------------------------------------------
Movie.java
------------------------------------------------------------------------------------------------------------


package com.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("bollywood")
public class Bollywood implements Movie {
private Category cat;
/*
//constructor-injection
@Autowired
public Bollywood(Category getcat){
cat=getcat;
}
*/

@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
public void setCat(Category cat) {
this.cat = cat;
}

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

}

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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println("Bollywood : "+obj.getMovie());
System.out.println("Bollywood category (setter): "+obj.getCategory());
Movie obj1=context.getBean("hollywood",Movie.class);
System.out.println("Hollywood : "+obj1.getMovie());
System.out.println("Hollywood category : "+obj1.getCategory());
Movie obj2=context.getBean("tollywood",Movie.class);
System.out.println("Tollywood : "+obj2.getMovie());
System.out.println("Tollywood category : "+obj2.getCategory());
context.close();

}

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 5:44:59 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:44:59 IST 2018]; root of context hierarchy
Feb 26, 2018 5:44:59 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Bollywood : Bollywood id famous for acting and songs
Bollywood category (setter): Commedy is for fun
Hollywood : Hollywood is famous for its creativity.
Hollywood category : Commedy is for fun
Tollywood : Tollywood is famous for storyline
Tollywood category : Commedy is for fun
Feb 26, 2018 5:45:00 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:44:59 IST 2018]; root of context hierarchy


@Autowired annotation

method injection


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("hollywood")
public class Hollywood implements Movie {
private Category cat;
//constructor injection
/* @Autowired
public Hollywood(Category thecat){
cat=thecat;
}*/
@Autowired
public void doSomeCrazyStuff()
{
System.out.println("Hollywood : this is a random method");
}
@Override
public String getMovie() {
return "Hollywood is famous for its creativity.";
}

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

}



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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Entertainment {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Movie obj=context.getBean("bollywood",Movie.class);
System.out.println("Bollywood : "+obj.getMovie());
System.out.println("Bollywood category (setter): "+obj.getCategory());
Hollywood obj1=context.getBean("hollywood",Hollywood.class);
System.out.println("Hollywood : "+obj1.getMovie());
obj1.doSomeCrazyStuff();
Movie obj2=context.getBean("tollywood",Movie.class);
System.out.println("Tollywood : "+obj2.getMovie());
System.out.println("Tollywood category : "+obj2.getCategory());
context.close();

}

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 5:51:16 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:51:16 IST 2018]; root of context hierarchy
Feb 26, 2018 5:51:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Feb 26, 2018 5:51:17 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor lambda$buildAutowiringMetadata$2
WARNING: Autowired annotation should only be used on methods with parameters: public void com.demo.spring.Hollywood.doSomeCrazyStuff()
Hollywood : this is a random method
Bollywood : Bollywood id famous for acting and songs
Bollywood category (setter): Commedy is for fun
Hollywood : Hollywood is famous for its creativity.
Hollywood : this is a random method
Tollywood : Tollywood is famous for storyline
Tollywood category : Commedy is for fun
Feb 26, 2018 5:51:17 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:51:16 IST 2018]; root of context hierarchy



@Autowired annotation

field injection

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("tollywood")
public class Tollywood implements Movie {
@Autowired
private Category cat;

/* //constructor injection
@Autowired
public Tollywood(Category thecat){
cat=thecat;
}*/
@Override
public String getMovie() {
return "Tollywood is famous for storyline";
}

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

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
Feb 26, 2018 5:55:17 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:55:16 IST 2018]; root of context hierarchy
Feb 26, 2018 5:55:17 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Feb 26, 2018 5:55:17 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor lambda$buildAutowiringMetadata$2
WARNING: Autowired annotation should only be used on methods with parameters: public void com.demo.spring.Hollywood.doSomeCrazyStuff()
Hollywood : this is a random method
Bollywood : Bollywood id famous for acting and songs
Bollywood category (setter): Commedy is for fun
Hollywood : Hollywood is famous for its creativity.
Hollywood : this is a random method
Tollywood : Tollywood is famous for storyline
Tollywood category : Commedy is for fun
Feb 26, 2018 5:55:17 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:55:16 IST 2018]; root of context hierarchy



@Autowired is working because we have only one bean dependency qualifier as of now . Lets create one more 

------------------------------------------------------------------------------------------------------------
Action.java
------------------------------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.stereotype.Component;

@Component
public class Action implements Category {

@Override
public String getCategory() {
return "Action movies are breathtaking";
}

}

now we have two Comedy and Action. If you execute your program now then


------------------------------------------------------------------------------------------------------------
EXCEPTION
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 5:58:31 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 17:58:31 IST 2018]; root of context hierarchy
Feb 26, 2018 5:58:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Feb 26, 2018 5:58:32 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bollywood': Unsatisfied dependency expressed through method 'setCat' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.demo.spring.Category' available: expected single matching bean but found 2: action,comedy
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bollywood': Unsatisfied dependency expressed through method 'setCat' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.demo.spring.Category' available: expected single matching bean but found 2: action,comedy

to resolve this we will use @Qualifier(bean id) 
note : bean id is the classname in smallletters

@Qualifier annotation


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("bollywood")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}

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

}


@Scope annotation(singleton/prototype)


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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeEntertainmentTesting {

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

}

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("bollywood")
@Scope("singleton")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}

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

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 6:44:50 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:44:50 IST 2018]; root of context hierarchy
Feb 26, 2018 6:44:50 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
true
com.demo.spring.Bollywood@4de5031f
com.demo.spring.Bollywood@4de5031f
Feb 26, 2018 6:44:51 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:44:50 IST 2018]; root of context hierarchy


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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("bollywood")
@Scope("prototype")
public class Bollywood implements Movie {
private Category cat;
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}

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

}

------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 6:45:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:45:25 IST 2018]; root of context hierarchy
Feb 26, 2018 6:45:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
false
com.demo.spring.Bollywood@4de5031f
com.demo.spring.Bollywood@67e2d983
Feb 26, 2018 6:45:25 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Mon Feb 26 18:45:25 IST 2018]; root of context hierarchy




@postConstruct and @preDestroy annotations

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

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeEntertainmentTesting {

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

}

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

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

@Component("bollywood")
//@Scope("prototype")
public class Bollywood implements Movie {
private Category cat;
public Bollywood(){}
@PostConstruct 
public void doStart(){
System.out.println("startup");
}

@PreDestroy
public void doFinish(){
System.out.println("Finish");
}
@Override
public String getMovie() {
return "Bollywood id famous for acting and songs";
}
//Setter injection
@Autowired
@Qualifier("comedy")
public void setCat(Category cat) {
this.cat = cat;
}

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

}


------------------------------------------------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------------------------------------------------
Feb 26, 2018 7:32:21 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Feb 26 19:32:21 IST 2018]; root of context hierarchy
Feb 26, 2018 7:32:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
startup
Bollywood id famous for acting and songs
Feb 26, 2018 7:32:21 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7dc5e7b4: startup date [Mon Feb 26 19:32:21 IST 2018]; root of context hierarchy
Finish



Read More »