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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
Movie.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
public interface Movie {
public String getMovie();
public String getCategory();
}
------------------------------------------------------------------------------------------------------------
Bollywood.java
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
Category.java
------------------------------------------------------------------------------------------------------------
package com.demo.spring;
import org.springframework.stereotype.Component;
public interface Category {
public String getCategory();
}
------------------------------------------------------------------------------------------------------------
Comedy.java
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------
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
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
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
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
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
No comments:
Post a Comment