0% found this document useful (0 votes)
12 views

P1 Spring-5.2.5 - Study Guide

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

P1 Spring-5.2.5 - Study Guide

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

-

SPRING 5.2
STUDY GUIDE

Author
Som Prakash Rai
S
o
www.jtcindia.org 1 Spring-5.2 Study Guide
Guide
 The Spring Framework provides a comprehensive programming and configuration model for
modern Java-based enterprise applications

 Spring Framework is a Java platform that provides comprehensive infrastructure support for
developing Java applications.

 Spring handles the infrastructure so you can focus on your application.

 Using Spring Framework,We can develop Large-Scale , Distributed High-Performance


Applications

Module 1: Spring IOC


Module 2: Spring AOP
Module 3: Spring DAO Support.
Module 4: Spring with JDBC
Module 5: Spring with Hibernate
Module 6: Spring Transaction Management
Module 7: Spring Web MVC
Module 8: Spring Rest WebServices.
Module 9: Spring Security
Module 10: Spring with Java Mail

1) Spring Boot
2) Spring Data.
3) Spring Cloud
4) Spring Messaging
5) Spring AMQP
6) Spring Kafka
7) Spring Android.
8) Spring Batch
9) Spring Social.
10) Spring Web Flow.
11) Spring LDAP.
12) Spring Hadoop.
13) Spring Scala.
14) Spring Mobile.
15) Spring Roo.

www.jtcindia.org 2 Spring-5.2 Study Guide


Guide
 When you are developing any component or bean, it may contain some dependencies.

class A{ class Hello{


void m1(){ ... }
} A aobj=null;
B bobj=null;
class B{
void m2(){ ... } void show(){
} aobj.m1();
bobj.m2();
}
}

 Hello Bean needs A object and B object.


 Hello Bean needs A resource and B resource.
 Hello Bean needs A Bean and B Bean

 Hello Bean is depending on two beans called A bean and B bean.


 Hello Bean dependencies called A and B has to be Initialized.
 Hello Bean dependencies called A and B has to be Injected
 Spring container Injects the Hello Bean dependencies called A and B.

Dependency Injection
 Dependency Injection is the process of Injecting bean dependencies automatically.
 Spring Dependency Injection uses 3 ways to inject the dependencies
1) Setter Injection
2) Constructor Injection
3) Field Injection (using Annotations)

Ex:
class Student{
String sid; //1 Setter Injection

String sname; //2 Constructor


Injection @Autowired
Address add; //3 Field Injection.( using Annotations)

public void setSid(String sid){


this.sid=sid; //1 Setter Injection
}
public Student(String sname){
this.sname=sname; //2 Constructor Injection
}
}

www.jtcindia.org 3 Spring-5.2 Study Guide


Guide
Download the Spring Jars
1) Open this url
https://repo.spring.io/release/org/springframework/spring/
2) Click on 5.2.5.RELEASE
3) Click ON spring-5.2.5.RELEASE-dist.zip ... It downloads the ZIP
4) Unzip the spring-5.2.5.RELEASE-dist.zip then you will see the folder called spring-
framework-5.2.5.RELEASE
5) You can see 63 Jars under libs
6) These 63 jars as belongs 3 groups
a. 21- binary jars (We need this only)
b. 21-source jars
c. 21 - java doc jars

7) Copy 21-Binary Jars into required Folder to use in your Eclipse Projects.

1) Open the Eclipse with Some Workspace.


2) Create the Java Project with the Name : Lab1
3) Add the JDK1.8 to Project.
4) Add 21 Spring-5 Jars to Project build path.
5) Create the Package called com.jtcindia.spring
6) Write the following 3 Beans
a. A.java
b. B.java
c. Hello.java
7) Write the Client Program without Spring
a. Lab1A.java
8) Run Lab1A
9) Configuring the Beans in Configuration Class
JTCAppConfig.java
10) Write the Client Program
a. Lab1B.java
11) Run the Lab1B

Lab1: Files required

1. Lab1A.java 2. Lab1B.java
3. A.java 4. B.java
5. Hello.java 6. JTCAppConfig.java

www.jtcindia.org 4 Spring-5.2 Study Guide


Guide
1. Lab1A.java 2. Lab1B.java
package com.jtcindia.spring; package com.jtcindia.spring;
/*
* @Author : Som Prakash import
* @Company : jtcindia org.springframework.context.ApplicationConte
* @Website : www.jtcindia.org xt;
* */ import
public class Lab1A { org.springframework.context.annotation.Annot
public static void main(String[] args) { ationConfigApplicationContext;
/*
//Without Spring * @Author : Som Prakash
//Task1 : Create and Initialize A object * @Company : jtcindia
A ao=new A(); * @Website : www.jtcindia.org
ao.setA(101); * */
ao.setMsg("I am A"); public class Lab1B {
public static void main(String[] args) {
//Task2 : Create and Initialize B object //With Spring
B bo=new B(102,"I am B");
ApplicationContext ctx=new
//Task3 : Create and Initialize Hello object AnnotationConfigApplicationContext(JTCAppCo
Hello h=new Hello(bo); nfig.class);
h.setAobj(ao); System.out.println("--------- Now Spring
Container is Ready----- ");
h.show();
A aobj=(A)ctx.getBean("createA");
} System.out.println(aobj);
}
B bobj=(B)ctx.getBean("BO");
System.out.println(bobj);

Hello h=(Hello)ctx.getBean("myhello");
h.show();

}
}

www.jtcindia.org 5 Spring-5.2 Study Guide


Guide
3. A.java 4. B.java
package com.jtcindia.spring; package com.jtcindia.spring;
/* /*
* @Author : Som Prakash * @Author : Som Prakash
* @Company : jtcindia * @Company : jtcindia
* @Website : www.jtcindia.org * @Website : www.jtcindia.org
* */ * */
public class A { public class B {
int a; //S.I int b; //C.I
String msg; //S.I String str; //C.I
static { static {
System.out.println("A - S.B"); System.out.println("B - S.B");
} }
public A() { public B(int b, String str) {
System.out.println("A - D.C"); System.out.println("B - 2 arg");
} this.b = b;
public void setA(int a) { this.str = str;
System.out.println("A - setA()"); }
this.a = a; public String toString() {
} return ""+b+"\t"+str;
public void setMsg(String msg) { }
System.out.println("A - setMsg()"); }
this.msg = msg;
}
public String toString() {
return ""+a +"\t"+msg;
}
}

5. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
A aobj; //S.I
B bobj; //C.I
static {
System.out.println("Hello - S.B");
}
public Hello(B bobj)
{ System.out.println("Hello(B) -
1arg"); this.bobj = bobj;
}
public void setAobj(A aobj)
{ System.out.println("Hello-
setAobj()");
www.jtcindia.org 6 Spring-5.2 Study Guide
Guide
this.aobj = aobj;
}
public void show() {
System.out.println("Hello-show()");
System.out.println(aobj);
System.out.println(bobj);
}
}
6. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean
public A createA() {
//Task1 : Create and Initialize A object
A ao=new A();
ao.setA(101);
ao.setMsg("I am A");
return ao;
}

@Bean("BO")
public B createB() {
//Task2 : Create and Initialize B object
B bo=new B(102,"I am B");
return bo;
}

@Bean("myhello")
public Hello createHello(A ao,B bo) {
//Task3 : Create and Initialize Hello object
Hello h=new Hello(bo);
h.setAobj(ao);
return h;
}
}

www.jtcindia.org 7 Spring-5.2 Study Guide


Guide
 Bean Instance created by Spring Container can be in one of the following Scopes(Updated
from Spring5).
1) singleton
2) prototype
3) request
4) session
5) application
6) websocket

Scope Description
singleton  When bean scope is singleton then only one instance will be created for
that bean and the same instance will be returned when you call getBean()
method.
 singleton is the default scope in the ApplicationContext container.
 When scope is single-ton then default loading type is aggressive loading.

prototype  When bean scope is prototype then every time a new instance will be
created for that bean when you call getBean() method.
 When scope is prototype then default loading type is lazy loading.

request  Scopes a single bean definition to the lifecycle of a single HTTP request.
 Single Bean instance will be created per HTTP Request
 Only valid in the context of a web-aware Spring ApplicationContext.

session  Scopes a single bean definition to the lifecycle of an HTTP Session.


 Single Bean instance will be created per HTTP Session
 Only valid in the context of a web-aware Spring ApplicationContext.

application  Scopes a single bean definition to the lifecycle of a ServletContext.


 Single Bean instance will be created per ServletContext.
 Only valid in the context of a web-aware Spring ApplicationContext.

websocket  Scopes a single bean definition to the lifecycle of a WebSocket.


 Single Bean instance will be created per WebSocket.
 Only valid in the context of a web-aware Spring ApplicationContext.

 Usage:

@Scope(value="singleton")
@Scope(value=" prototype ")
@Scope("singleton")
@Scope("prototype")

www.jtcindia.org 8 Spring-5.2 Study Guide


Guide
Bean Scope Example with Java Configuration
Lab2: Files required
1. Lab2.java 2. Hello.java
3. Hai.java 4. JTCAppConfig.java

1. Lab2.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab2 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello1=(Hello)ctx.getBean("hello");
Hello hello2=(Hello)ctx.getBean("hello");
System.out.println(hello1==hello2);

Hai hai1=(Hai)ctx.getBean("hai");
Hai hai2=(Hai)ctx.getBean("hai");
System.out.println(hai1==hai2);

Hello hello=(Hello)ctx.getBean("hello");
hello.showHello();

Hai hai=(Hai)ctx.getBean("hai");
hai.showHai();
}
}

2. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

static {

www.jtcindia.org 9 Spring-5.2 Study Guide


Guide
System.out.println("Hello - S.B");
}
public Hello()
{ System.out.println("Hello - D.C");
}
public void showHello() {
System.out.println("Hello-showHello()");
}
}

3. Hai.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {

static
{ System.out.println("Hai -
S.B");
}
public Hai()
{ System.out.println("Hai - D.C");
}
public void showHai() {
System.out.println("Hai-showHai()");
}
}
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("hello")
@Scope("singleton")
public Hello createHello() {
System.out.println("-------createHello()-------- called");

www.jtcindia.org 10 Spring-5.2 Study Guide


Guide
return new Hello();
}
@Bean("hai")
@Scope("prototype")
public Hai createHai() {
System.out.println("-------createHai()--------called");
return new Hai();
}

 Bean configured in the Spring Configuration Class can be loaded in two ways.
1) Aggressive loading or Eager loading
2) Lazy loading.

 Usage:
@Lazy(value="true")
@Lazy(value="true")
@Lazy("true")
@Lazy("false")

1) Aggressive loading or Eager loading


 In the case of aggressive loading, all the Beans will be loaded, instantiated and initialized by
the container at the container start-up.
Ex:
@Bean
@Lazy(false)
public Hello hello() {
return new Hello();
}
2) Lazy loading.

 In the case of lazy loading, all the Beans will be loaded, instantiated and initialized when
you or container try to use them by calling getBean() method.

Ex:
@Bean @Lazy(true)
public Hello hello() {
return new Hello();
}

www.jtcindia.org 11 Spring-5.2 Study Guide


Guide
Bean Loading Types Example with Java Configuration:
Lab3: Files required
1. Lab3.java New One
2. Hello.java Same as Lab2
3. Hai.java Same as Lab2
4. JTCAppConfig.java New One

1. Lab3.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab3 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("hello");
hello.showHello();

Hai hai=(Hai)ctx.getBean("hai");
hai.showHai();

}
}

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

www.jtcindia.org 12 Spring-5.2 Study Guide


Guide
@Configuration
public class JTCAppConfig {

@Bean("hello")
@Scope("singleton")
@Lazy(true)
public Hello createHello() {
System.out.println("-------createHello()-------- called");
return new Hello();
}

@Bean("hai")
@Scope("prototype")
@Lazy(true)
public Hai createHai() {
System.out.println("-------createHai()--------called");
return new Hai();
}
}

www.jtcindia.org 13 Spring-5.2 Study Guide


Guide
Lab4: Files required
5. Lab4.java 6. Hello.java
7. JTCAppConfig.java

1. Lab4.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab4 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
Runtime rt =(Runtime)ctx.getBean("myruntime");
System.out.println(rt.availableProcessors());
} }

2. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello
{ static Hello hello;
static {
hello=new Hello();
}
private Hello() {
}
public static Hello getHello()
{ return hello;
}
public void show() {
System.out.println("Hello-show()");
} }

www.jtcindia.org 14 Spring-5.2 Study Guide


Guide
3. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig
{ @Bean("myhello")
public Hello createHello() {
System.out.println("-------createHello()-------- called");
Hello hello=Hello.getHello();
return hello;
}
@Bean("myruntime")
public Runtime createRT() {
System.out.println("-------createRT()--------called");
Runtime rt=Runtime.getRuntime();
return rt;
}
}

 JTCAppConfig - Spring Configuration class which contains all the Bean Definitions.

 You can write Multiple Configuration classes in One Spring Application.

 There are 3 ways to Handle Multiple Configuration classes


1) Using the constructor of AnnotationConfigApplicationContext
2) Using @Import annotation
3) Using @ImportResource annotation

1) Using the constructor of AnnotationConfigApplicationContext (Refer Lab5)

 Specify the Multiple Configuration classes as Parameters for Constructor of


AnnotationConfigApplicationContext

ApplicationContext ctx= new AnnotationConfigApplicationContext


( JTCAppConfig1.class, JTCAppConfig2.class, JTCAppConfig3.class);

www.jtcindia.org 15 Spring-5.2 Study Guide


Guide
2) Using @Import annotation (Refer Lab6 )

 Specify the One Configuration class as Parameters for Constructor of


AnnotationConfigApplicationContext

ApplicationContext ctx=new AnnotationConfigApplicationContext (JTCAppConfig1.class);

 Import Remaining Configuration classes using @Import Annotation

@Import({JTCAppConfig1.class,JTCAppConfig3.class})
public class JTCAppConfig2{

}...

3) Using @ImportResource annotation (Refer Lab7)


 This is Required only when some of your beans are configured in XML file.
 Consider the Bean Configurations as follows.

jtc1.xml - Configure A bean here


jtc2.xml - Configure B bean here
JTCAppConfig -- Hello,Hai

Note : place jtc1.xml and jtc2.xml in src folder.

 Specify the One Configuration class as Parameters for Constructor of


AnnotationConfigApplicationContext

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);

 Import Remaining Configuration XML files using @ ImportResource Annotation

@ImportResource({"jtc1.xml","jtc2.xml"})
public class JTCAppConfig {

Lab5: Files required


1. Lab5.java 2. A.java
3. B.java 4. Hai.java
5. Hello.java 6. JTCAppConfig1.java
7. JTCAppConfig2.java 8. JTCAppConfig3.java

1. Lab5.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

www.jtcindia.org 16 Spring-5.2 Study Guide


Guide
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab5 {
public static void main(String[] args)
{ ApplicationContext ctx=new
AnnotationConfigApplicationContext(JTCAppConfig1.class,JTCAppConfig2.class,JTCAppConfig3.class
);
System.out.println("---------Now Spring Container is Ready----- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
Hai hai=(Hai) ctx.getBean("myhai");
hai.show();
}
}

2. A.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class A
{ int a; //S.I
String msg; //S.I
static {
System.out.println("A - S.B");
}
public A() {
System.out.println("A - D.C");
}
public void setA(int a) {
System.out.println("A - setA()");
this.a = a;
}
public void setMsg(String msg)
{ System.out.println("A - setMsg()");
this.msg = msg;
}
public String toString()
{ return ""+a
+"\t"+msg;
}
}

www.jtcindia.org 17 Spring-5.2 Study Guide


Guide
3. B.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class B
{ int b; //C.I
String str; //C.I
static {
System.out.println("B - S.B");
}
public B(int b, String str)
{ System.out.println("B - 2 arg");
this.b = b;
this.str = str;
}
public String toString() {
return ""+b+"\t"+str;
}
}

4. Hai.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
A aobj; //S.I
B bobj; //S.I
static {
System.out.println("Hai - S.B");
}
public Hai() {
System.out.println("Hai() - D.C");
}
public void setAobj(A aobj)
{ System.out.println("Hai- setAobj()
");
this.aobj = aobj;
}
public void setBobj(B bobj)
{ System.out.println("Hai- setBobj()
"); this.bobj = bobj;
}

www.jtcindia.org 18 Spring-5.2 Study Guide


Guide
public void show()
{ System.out.println("Hai-
show()");
System.out.println(aobj);
System.out.println(bobj);
}
}
5. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
A aobj; //C.I
B bobj; //C.I
static {
System.out.println("Hello - S.B");
}
public Hello() {
System.out.println("Hello() - D.C");
}
public Hello(A aobj,B bobj)
{ System.out.println("Hello(A,B) - 2arg");
this.aobj=aobj;
this.bobj = bobj;
}
public void show()
{ System.out.println("Hello-
show()"); System.out.println(aobj);
System.out.println(bobj);
}
}
6. JTCAppConfig1.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig1 {

www.jtcindia.org 19 Spring-5.2 Study Guide


Guide
@Bean("ao")
public A createA()
{ A ao=new A();
ao.setA(101);
ao.setMsg("I am A");
return ao;
}
@Bean("bo")
public B createB() {
B bo=new B(102,"I am B");
return bo;
}
}

7. JTCAppConfig2.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig2 {

@Bean("myhello")
public Hello createHello(A ao,B bo)
{ Hello h=new Hello(ao,bo);
return h;
}
}

8. JTCAppConfig3.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : Jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig3 {

www.jtcindia.org 20 Spring-5.2 Study Guide


Guide
@Bean("myhai")
public Hai createHai(A ao,B bo)
{ Hai hai=new Hai();
hai.setAobj(ao);
hai.setBobj(bo);
return hai;
}
}

Lab6: Files required


1. Lab6.java New One
2. A.java Same as Lab5
3. B.java Same as Lab5
4. Hai.java Same as Lab5
5. Hello.java Same as Lab5
6. JTCAppConfig1.java New One
7. JTCAppConfig2.java Same as Lab5
8. JTCAppConfig3.java Same as Lab5

1. Lab6.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab6 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig1.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();

Hai hai=(Hai) ctx.getBean("myhai");


hai.show();
}
}

www.jtcindia.org 21 Spring-5.2 Study Guide


Guide
6. JTCAppConfig1.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
@Import({JTCAppConfig2.class,JTCAppConfig3.class})
public class JTCAppConfig1 {

@Bean("ao")
public A createA()
{ A ao=new A();
ao.setA(101);
ao.setMsg("I am A");
return ao;
}
@Bean("bo")
public B createB() {
B bo=new B(102,"I am B");
return bo;
}
}

Lab7: Files required


1. Lab7.java New One
2. A.java Same as Lab5
3. B.java Same as Lab5
4. Hai.java Same as Lab5
5. Hello.java Same as Lab5
6. JTCAppConfig.java New One
7. jtc1.xml New One
8. jtc2.xml New One

1. Lab7.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia

www.jtcindia.org 22 Spring-5.2 Study Guide


Guide
* @Website : www.jtcindia.org
* */
public class Lab7 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();

Hai hai=(Hai) ctx.getBean("myhai");


hai.show();
}
}

6. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
@ImportResource({"jtc1.xml","jtc2.xml"})
public class JTCAppConfig {

@Bean("myhello")
public Hello createHello(A ao,B bo)
{ Hello h=new Hello(ao,bo);
return h;
}

@Bean("myhai")
public Hai createHai(A ao,B bo)
{ Hai hai=new Hai();
hai.setAobj(ao);
hai.setBobj(bo);
return hai;
}

www.jtcindia.org 23 Spring-5.2 Study Guide


Guide
7. jtc1.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="aobj" class="com.jtcindia.spring.A">


<property name="a" value="99"/>
<property name="msg" value="I am A"/>
</bean>

</beans>

8. jtc2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="bobj" class="com.jtcindia.spring.B">


<constructor-arg value="88"/>
<constructor-arg value="I am B"/>
</bean>

</beans>

You can Inject the following types of Bean Properties.


1. Simple Types(Primitives,Wrappers,Strings,Dates).
2. List type
3. Set type
4. Map type
5. java.util.Properties type
6. other Beans
7. Collection of Other Beans

All of these types of Bean Properties can be Injected with Setter Injection or Constructor Injection. Lab8:

Injecting Various Types of Properties


Lab9: Injecting Two List Types

www.jtcindia.org 24 Spring-5.2 Study Guide


Guide
Lab8: Files required
1. Lab8.java 2. Address.java
3. Account.java 4. Customer.java
5. JTCAppConfig.java

1. Lab8.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab8 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Customer cust=(Customer) ctx.getBean("mycust");


System.out.println(cust);
System.out.println(cust.getEmails());
System.out.println(cust.getPhones());
System.out.println(cust.getRefs());
System.out.println(cust.getAddress());

for(Account acc:cust.getAccounts())
{ System.out.println(acc);
}
}
}

2. Address.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Address
{ private String
street; private String
city; private String
state; public
Address() {}

www.jtcindia.org 25 Spring-5.2 Study Guide


Guide
public void setStreet(String street)
{ this.street = street;
}
public void setCity(String city)
{ this.city = city;
}
public void setState(String state)
{ this.state = state;
}
@Override
public String toString() {
return street + ", " + city + ", " + state ;
}
}

3. Account.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Account
{ private int accno;
private String atype;
private double bal;
public Account() {}
public Account(int accno, String atype, double bal)
{ super();
this.accno = accno;
this.atype = atype;
this.bal = bal;
}
@Override
public String toString() {
return accno + ", " + atype + ", " + bal ;
}
}

4. Customer.java
package com.jtcindia.spring;

import java.util.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

www.jtcindia.org 26 Spring-5.2 Study Guide


Guide
public class Customer {
private int cid;//1
private String cname;//1
private String email;//1
private long phone;//1
private List<String> emails;//2
private Set<Integer> phones;//3
private Map<String,Integer> refs;//4
private Properties myprops;//5
private Address address;//6
private List<Account> accounts;//7
public Customer() {}
public Customer(int cid, String cname, String email, long phone)
{ super();
this.cid = cid;
this.cname = cname;
this.email = email;
this.phone = phone;
}
//Setters and Getters
@Override
public String toString() {
return cid + ", " + cname + ", " + email + ", " + phone;
}
}

5. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig
{ @Bean(name="myemails")
public List<String> getEmails(){
System.out.println("JTCAppConfig -getEmails() ");
List<String> ems=new ArrayList<>();
ems.add("sp@jtc");
ems.add("rai@jtc");
ems.add("som@jtc");
return ems;
}

www.jtcindia.org 27 Spring-5.2 Study Guide


Guide
@Bean(name="myphones")
public Set<Integer> getPhones(){
System.out.println("JTCAppConfig -getPhones() ");
Set<Integer> phs=new TreeSet<>();
phs.add(111);
phs.add(222);
phs.add(333);
return phs;
}

@Bean(name="myrefs")
public Map<String,Integer>
getRefs(){ System.out.println("JTCAppConfig -
getRefs() "); Map<String,Integer> refs=new
TreeMap<>(); refs.put("A",11);
refs.put("B",22);
refs.put("C",33);
refs.put("D",44);
return refs;
}

@Bean(name="myprops")
public Properties getProps(){
System.out.println("JTCAppConfig -getProps() ");
Properties props=new Properties();
props.put("A",11);
props.put("B",22);
props.put("C",33);
props.put("D",44);
return props;
}

@Bean(name="myadd")
public Address getAdd() {
System.out.println("JTCAppConfig -getAdd() ");
Address add=new Address();
add.setStreet("BTM Layout");
add.setCity("Noida");
add.setState("noida
"); return add;
}

@Bean(name="myaccs")
public List<Account>
getAccounts(){ System.out.println("JTCAppConfig -
getAccounts() ");
List<Account> myaccs=new ArrayList<>();
myaccs.add(new Account(101,"SA",15000));

www.jtcindia.org 28 Spring-5.2 Study Guide


Guide
myaccs.add(new Account(102,"CA",25000));
myaccs.add(new Account(103,"DA",35000));
return myaccs;
}
@Bean(name="mycust")
public Customer createCustomer(List<String> myemails,Set<Integer> myphones,
Map<String,Integer> myrefs,Properties myprops,Address myadd,
List<Account> myaccs) {

System.out.println("JTCAppConfig -createCustomer() ");


Customer cust=new Customer(101,"SomPrakash","[email protected]",12345);
cust.setEmails(myemails);
cust.setPhones(myphones);
cust.setRefs(myrefs);
cust.setMyprops(myprops);
cust.setAddress(myadd);
cust.setAccounts(myaccs);
return cust;
}
}

Lab9: Files required


1. Lab9.java 2. Customer.java
3. JTCAppConfig.java

1. Lab9.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab9 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Customer cust=(Customer) ctx.getBean("mycust");


System.out.println(cust);
System.out.println(cust.getEmails());
System.out.println(cust.getPhones());
}
}

www.jtcindia.org 29 Spring-5.2 Study Guide


Guide
2. Customer.java
package com.jtcindia.spring;

import java.util.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Customer {
private int cid;
private String cname;
private String email;
private long phone;
private List<String> emails;
private List<String> phones;
public Customer() {}
public Customer(int cid, String cname, String email, long phone, List<String> emails,
List<String> phones) {
super();
this.cid = cid;
this.cname = cname;
this.email = email;
this.phone = phone;
this.emails = emails;
this.phones = phones;
}

//Setters and Getters


@Override
public String toString() {
return cid + ", " + cname + ", " + email + ", " + phone;
}
}

3. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

www.jtcindia.org 30 Spring-5.2 Study Guide


Guide
@Bean(name="myemails")
public List<String> getEmails(){
List<String> ems=new ArrayList<>();
ems.add("som@jtc");
ems.add("rai@jtc");
ems.add("sp@jtc");
return ems;
}
@Bean(name="myphones")
public List<String> getPhones(){
List<String> phs=new ArrayList<>();
phs.add("111");
phs.add("222");
phs.add("333");
return phs;
}
@Bean(name="mycust")
public Customer createCustomer(List<String> myemails,List<String> myphones)
{ Customer cust=new
Customer(101,"SomPrakash","[email protected]",12345,myemails,myphones); return cust;
}
}

 Wiring is the Process of Inejecting Bean Dependencies.

 Wiring Can be done in two ways.


1) Explicit Wiring
2) Implicit Wiring (or) AutoWiring

1) Explicit Wiring:
 In the case of Explicit Wiring, Bean Dependencies has to be specified by you explicitly.

Ex:
class Hello
{ Hai hai;
….
}

@Bean("myhello")
public Hello createHello(Hai hai)
{ Hello h=new Hello();
h.setHai(hai);
return h;
}
 In the above Example, We are explicitly specifying the Hello dependency called Hai

www.jtcindia.org 31 Spring-5.2 Study Guide


Guide
Case 1: What happens when 0 Matching Beans found.(Refer Lab10)

 Exception will be thrown


 NoSuchBeanDefinitionException: No qualifying bean of type
'com.jtcindia.spring.Hai' available: expected at least 1 bean

Note: In the case of Collections Only , Container creates the Empty Object and passes that as
parameter .(Ref. Lab9)

Case 2: What happens when exactly 1 Matching bean found.(Refer Lab11)

 Identified single bean will be injected with Setter method.

Case 3: What happens when two or more Matching beans found.


 If Any bean name is matching with local variable name then that will be injected with
setter method.(Refer Lab12)

 If Any bean name is not matching with local variable name then Exception will be
thrown.(Ref.Lab13)
 NoUniqueBeanDefinitionException: No qualifying bean of type.........expected single
matching bean but found 2: myhai1,myhai2

Lab10: Files required


1. Lab10.java 2. Hai.java
3. Hello.java 4. JTCAppConfig.java

1. Lab10.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab10 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}

www.jtcindia.org 32 Spring-5.2 Study Guide


Guide
2. Hai.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
static {
System.out.println("Hai - S.B");
}
public Hai() {
System.out.println("Hai() - D.C");
}

public String toString() {


return "Hai Guys, I am Hai Bean";
}
}

3. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
Hai hai; //1. Dependency

static {
System.out.println("Hello - S.B");
}
public Hello() {
System.out.println("Hello() - D.C");
}

public void setHai(Hai hai) { //2. Setter Injection


System.out.println("Hello - setHai()");
this.hai = hai;
}

public void show() {


System.out.println("Hello-show()");
System.out.println(hai);
}
}

www.jtcindia.org 33 Spring-5.2 Study Guide


Guide
4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig
{ @Bean("myhello")
public Hello createHello(Hai hai) { //1. Dependency
Hello h=new Hello();
h.setHai(hai); //2. Setter Injection
return h;
}
}

Lab11: Files required


1. Lab11.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai")
public Hai createHai1() {
Hai hai=new Hai();
return hai;
}

www.jtcindia.org 34 Spring-5.2 Study Guide


Guide
@Bean("myhello")
public Hello createHello(Hai hai) { //1. Dependency
Hello h=new Hello();
h.setHai(hai); //2. Setter Injection
return h;
}
}

Lab12: Files required


1. Lab12.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai1")
public Hai createHai1() {
Hai hai=new Hai("I am First Bean");
return hai;
}

@Bean("myhai2")
public Hai createHai2() {
Hai hai=new Hai("I am Second Bean");
return hai;
}

@Bean("myhello")
public Hello createHello(Hai myhai2) { //1. Dependency
Hello h=new Hello();
h.setHai(hai); //2. Setter Injection
return h;
}
}

www.jtcindia.org 35 Spring-5.2 Study Guide


Guide
Lab13: Files required
1. Lab13.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai1")
public Hai createHai1() {
Hai hai=new Hai("I am First Bean");
return hai;
}

@Bean("myhai2")
public Hai createHai2() {
Hai hai=new Hai("I am Second Bean");
return hai;
}

@Bean("myhello")
public Hello createHello(Hai hai) { //1. Dependency
Hello h=new Hello();
h.setHai(hai); //2. Setter Injection
return h;
}
}

www.jtcindia.org 36 Spring-5.2 Study Guide


Guide
AutoWiring :
 In the case of AutoWiring, No need to specify the Bean Dependencies explicitly.
 Spring Container is responsbile for
 Detecting Bean Dependencies
 Injecting Bean Dependencies

 If Container is Detecting and Injecting Bean Dependencies automatically then it is called as


AutoWiring.
 There are two ways to perform AutoWiring with autowire attribute of @Bean Annotation
1) By Name Autowire Process
2) By Type Autowire Process

 Autowire enum has two constants called BY_NAME and BY_TYPE

@Bean(name="myhello",autowire = Autowire.BY_NAME)
@Bean(name="myhello",autowire = Autowire.BY_BYTE)

1) By Name Autowire Process


 Container detects the Bean by Name.
 Container checks whether any bean is found whose name is matching property name.

Case 1: What happens when 0 beans found.(Refer Lab14)


 *Bean dependency remains Un-Injected.

Case 2: What happens when exactly 1 bean found.(Refer Lab15)


 Identified single bean will be injected with Setter method.

2) By Type Autowire Process


 Container detects the Bean by Data Type.
 Container checks whether any bean is found whose data Type is matching property data type.

Case 1: What happens when 0 beans found. (Ref.Lab16)

*Bean dependency remains Un-Injected.

Case 2: What happens when exactly 1 bean found.(Refer Lab17)


Identified single bean will be Injected with setter method.

Case 3: What happens when two or more beans found.(Refer Lab 18)
 Exception will be thrown
 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type 'com.jtcindia.spring.Hai' available: expected single matching
bean but found 3: myhai1,myhai2,hai

www.jtcindia.org 37 Spring-5.2 Study Guide


Guide
Lab14: Files required
1. Lab14.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai")
public Hai createHai1() {
Hai hai=new Hai("I am Hai Bean");
return hai;
}

@Bean(name="myhello",autowire = Autowire.BY_NAME)
public Hello createHello() {
Hello h=new Hello();
return h;
}
}

Lab15: Files required


1. Lab15.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*

www.jtcindia.org 38 Spring-5.2 Study Guide


Guide
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("hai")
public Hai createHai1() {
Hai hai=new Hai("I am Hai Bean");
return hai;
}

@Bean(name="myhello",autowire = Autowire.BY_NAME)
public Hello createHello() {
Hello h=new Hello();
return h;
}
}

Lab16: Files required


1. Lab16.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhello",autowire = Autowire.BY_TYPE)
public Hello createHello() {
Hello h=new Hello();
return h;
}
}

www.jtcindia.org 39 Spring-5.2 Study Guide


Guide
Lab17: Files required
1. Lab17.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai")
public Hai createHai1() {
Hai hai=new Hai("I am Hai Bean");
return hai;
}
@Bean(name="myhello",autowire = Autowire.BY_TYPE)
public Hello createHello() {
Hello h=new Hello();
return h;
}
}

Lab18: Files required


1. Lab18.java Same as Lab10
2. Hai.java Same as Lab10
3. Hello.java Same as Lab10
4. JTCAppConfig.java Updated Here

4. JTCAppConfig.java
package com.jtcindia.spring;

import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash

www.jtcindia.org 40 Spring-5.2 Study Guide


Guide
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean("myhai1")
public Hai createHai1() {
Hai hai=new Hai("I am First Hai Bean");
return hai;
}

@Bean("myhai2")
public Hai createHai2() {
Hai hai=new Hai("I am Second Hai Bean");
return hai;
}

@Bean("hai")
public Hai createHai3() {
Hai hai=new Hai("I am Third Hai Bean");
return hai;
}

@Bean(name="myhello",autowire = Autowire.BY_TYPE)
public Hello createHello() {
Hello h=new Hello();
return h;
}
}

Lab19: Files required


1. Lab19.java 2. A.java
3. B.java 4. CustomerDAO.java
5. CustomerDAOImpl.java 6. Hello.java
7. JTCAppConfig.java

1. Lab19.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia

www.jtcindia.org 41 Spring-5.2 Study Guide


Guide
* @Website : www.jtcindia.org
* */
public class Lab19 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);
System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}

2. A.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class A {
public String toString() {
return "I am Bean A";
}
}
3. B.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class B extends A{
public String toString() {
return "I am Bean B";
}
}
4. CustomerDAO.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public interface CustomerDAO
{ public void addCustomer();
}

www.jtcindia.org 42 Spring-5.2 Study Guide


Guide
5. CustomerDAOImpl.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class CustomerDAOImpl implements CustomerDAO
{ @Override
public void addCustomer() {
System.out.println("----AddCustomer---");
}
}

6. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
A aobj; //1
CustomerDAO customerDAO; //2

public void setAobj(A aobj)


{ this.aobj = aobj;
}
public void setCustomerDAO(CustomerDAO customerDAO)
{ this.customerDAO = customerDAO;
}
public void show() {
System.out.println(aobj);
customerDAO.addCustomer();
}
}

7. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org

www.jtcindia.org 43 Spring-5.2 Study Guide


Guide
* */
@Configuration
public class JTCAppConfig {
/*
@Bean("ao")
public A createA() {
return new A();
}
*/
@Bean(name="bo")
public B createB() {
return new B();
}
@Bean(name="cdao")
public CustomerDAO getCustDAO()
{ return new CustomerDAOImpl();
}
@Bean(name="myhello",autowire = Autowire.BY_TYPE)
public Hello createHello() { //Hello Bean
return new Hello();
}
}

 Spring Supports Cyclic DI with Setter Injection.


 Spring does not support Cyclic DI with Constructor Injection.

Lab20: Files required


1. Lab20.java 2. Hai.java
3. Hello.java 4. JTCAppConfig.java

1. Lab20.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab20 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);
System.out.println("---------Now Spring Container is Ready---- ");
}
}

www.jtcindia.org 44 Spring-5.2 Study Guide


Guide
2. Hai.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {

Hello hello; //Dependency

public Hai() {
System.out.println("Hai-D.C");
}
public void setHello(Hello hello) { //Setter Injection
System.out.println("Hai - setHello()");
this.hello = hello;
}
}

3. Hello.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

Hai hai; //Dependency

public Hello() {
System.out.println("Hello-D.C");
}
public void setHai(Hai hai) { //Setter Injection
System.out.println("Hello-setHai()");
this.hai = hai;
}
}

www.jtcindia.org 45 Spring-5.2 Study Guide


Guide
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai",autowire = Autowire.BY_TYPE)
public Hai createHai() {
Hai hai=new Hai();
return hai;
}

@Bean(name="myhello",autowire = Autowire.BY_TYPE)
public Hello createHello() {
Hello hello=new Hello();
return hello;
}
}

www.jtcindia.org 46 Spring-5.2 Study Guide


Guide
Lab21: Files required
1. Lab21.java Same as Lab19
2. A.java Same as Lab19
3. B.java Same as Lab19
4. CustomerDAO.java Same as Lab19
5. CustomerDAOImpl.java Same as Lab19
6. Hello.java Same as Lab19
7. JTCAppConfig.java Updated in Lab21

7. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="aobj")
public A createA() {
return new A();
}

@Bean(name="mybo")
public B createB() {
return new B();
}

@Bean(name="customerDAO")
public CustomerDAO getCustDAO()
{ return new CustomerDAOImpl();
}

@Bean(name="myhello",autowire = Autowire.BY_NAME)
public Hello createHello() { //AutoWiring
return new Hello();
}
}

www.jtcindia.org 47 Spring-5.2 Study Guide


Guide
 Field Injection can be implemented with Annotation based AutoWiring
 Field Injection :
o Injecting the Bean Dependencies directly without any Setter methods or Constructor is
called Field Injection.

Using @Autowired:
 @Autowired can be used in two ways.
1. ByType autowire process
2. ByName autowire process

1) ByType autowiring with @Autowired


 When you use @Autowired, then by default, beans will be detected based on byType autowire
process and inject them directly without any Setter methods or Constructor.

A) @Autowired(required=true) / @Autowired

Case 1: What happens when 0 Matching Beans found.(Refer Lab22)

 Exception will be thrown


 NoSuchBeanDefinitionException: No qualifying bean of type
'com.jtcindia.spring.Hai' available: which qualifies as autowire candidate.
Dependency annotations: {Autowired(required=true)}

Case 2: What happens when exactly 1 Matching bean found.(Refer Lab23)

 Identified single bean will be Injected Directly without any setter method.

Case 3: What happens when two or more Matching beans found.


 If Any bean name is matching with local variable name then that will be injected with
setter method.(Refer Lab24)

 If Any bean name is not matching with local variable name then Exception will be
thrown.(Ref.Lab25)
 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying
bean of type 'com.jtcindia.spring.Hai' available: expected single matching bean but
found 3: myhai1,myhai2,myhai3

www.jtcindia.org 48 Spring-5.2 Study Guide


Guide
Lab22: Files required
1. Lab22.java 2. Hai.java
3. Hello.java 4. JTCAppConfig.java

1. Lab22.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab22 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);


System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}

2. Hai.java
package com.jtcindia.spring;

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
String msg;

public void setMsg(String msg)


{ this.msg = msg;
}

public String toString()


{ return msg;
}
}

www.jtcindia.org 49 Spring-5.2 Study Guide


Guide
3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}

www.jtcindia.org 50 Spring-5.2 Study Guide


Guide
Lab23: Files required
1. Lab23.java Same as Lab22
2. Hai.java Same as Lab22
3. Hello.java Same as Lab22
4. JTCAppConfig.java Updated in Lab23

4. JTCAppConfig.java
package com.jtcindia.spring;

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

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}

www.jtcindia.org 51 Spring-5 Study Guide


Lab24: Files required
1. Lab24.java Same as Lab22
2. Hai.java Same as Lab22
3. Hello.java Same as Lab22
4. JTCAppConfig.java Updated in Lab24

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhai2")
public Hai createHai2() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 2");
return hai;
}

@Bean(name="hai")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am also Hai Bean");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 52 Spring-5.2 Study Guide


Lab25: Files required
1. Lab25.java Same as Lab22
2. Hai.java Same as Lab22
3. Hello.java Same as Lab22
4. JTCAppConfig.java Updated in Lab25

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhai2")
public Hai createHai2() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 2");
return hai;
}

@Bean(name="myhai3")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 3");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 53 Spring-5.2 Study Guide


B) @Autowired(required=false)

Case 1: What happens when 0 Matching Beans found.(Refer Lab26)

 Bean Property remains Un-Injected.

Case 2: What happens when exactly 1 Matching bean found.(Refer Lab27)

 Identified single bean will be Injected Directly without any setter method.

Case 3: What happens when two or more Matching beans found.


 If Any bean name is matching with local variable name then that will be injected with
setter method.(Refer Lab28)

 If Any bean name is not matching with local variable name then Exception will be
thrown.(Ref.Lab29)
 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying
bean of type 'com.jtcindia.spring.Hai' available: expected single matching bean but
found 3: myhai1,myhai2,myhai3

Lab26: Files required


1. Lab26.java Same as Lab22
2. Hai.java Same as Lab22
3. Hello.java Updated in Lab26
4. JTCAppConfig.java Updated in Lab26

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired(required = false)
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

www.jtcindia.org 54 Spring-5.2 Study Guide


4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}
Lab27: Files required
1. Lab27.java Same as Lab22
2. Hai.java Same as Lab22
3. Hello.java Updated in Lab27
4. JTCAppConfig.java Updated in Lab27

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired(required = false)
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

www.jtcindia.org 55 Spring-5.2 Study Guide


4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig
{ @Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}
@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

Lab28: Files required


5. Lab28.java Same Lab22
6. Hai.java Same Lab22
7. Hello.java Updated in Lab28
8. JTCAppConfig.java Updated in Lab28

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
@Autowired(required = false)
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

www.jtcindia.org 56 Spring-5.2 Study Guide


4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhai2")
public Hai createHai2() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 2");
return hai;
}

@Bean(name="hai")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am also Hai Bean");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 57 Spring-5.2 Study Guide


Lab29: Files required
1. Lab29.java Same Lab22
2. Hai.java Same Lab22
3. Hello.java Updated in Lab29
4. JTCAppConfig.java Updated in Lab29

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired(required = false)
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

www.jtcindia.org 58 Spring-5.2 Study Guide


@Bean(name="myhai2")
public Hai createHai2() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 2");
return hai;
}

@Bean(name="myhai3")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 3");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

2) ByName autowiring with @Autowired


When you want to detect the beans based on byName autowire process then you need to use
@Qualifier Annotation along with @Autowired.

A) @Autowired(required=true) / @Autowired

@Autowired(required=true)
@Qualifier("myhai")

Case 1: What happens when 0 Matching Beans found.(Refer Lab30)

 Exception will be thrown


 NoSuchBeanDefinitionException: No qualifying bean of type
'com.jtcindia.spring.Hai' available: which qualifies as autowire candidate.
Dependency annotations: {Autowired(required=true)}

Case 2: What happens when exactly 1 Matching bean found.(Refer Lab31)

 Identified single bean will be Injected Directly without any setter method.

www.jtcindia.org 59 Spring-5.2 Study Guide


Lab30: Files required
1. Lab30.java Same Lab22
2. Hai.java Same Lab22
3. Hello.java Updated in Lab30
4. JTCAppConfig.java Updated in Lab30

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired
@Qualifier("myhai")
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 60 Spring-5.2 Study Guide


Lab31: Files required
1. Lab31.java Same Lab22
2. Hai.java Same Lab22
3. Hello.java Updated in Lab31
4. JTCAppConfig.java Updated in Lab31

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
@Autowired
@Qualifier("myhai")
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {
@Bean(name="myhai")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}
@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 61 Spring-5.2 Study Guide


B) @Autowired(required=false)

B) @Autowired(required=false)
@Qualifier("myhai")

Case 1: What happens when 0 Matching Beans found.(Refer Lab32)

 Bean Property remains Un-Injected.

Case 2: What happens when exactly 1 Matching bean found.(Refer Lab33)

 Identified single bean will be Injected Directly without any setter method.

Lab32: Files required


5. Lab32.java Same Lab22
6. Hai.java Same Lab22
7. Hello.java Updated in Lab32
8. JTCAppConfig.java Updated in Lab32

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Autowired(required=false)
@Qualifier("myhai")
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

www.jtcindia.org 62 Spring-5.2 Study Guide


Guide
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

Lab33: Files required


5. Lab33.java Same Lab22
6. Hai.java Same Lab22
7. Hello.java Updated in Lab33
8. JTCAppConfig.java Updated in Lab33

3. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
@Autowired(required=false)
@Qualifier("myhai")
Hai hai; //1

public void show() {


System.out.println(hai);
}
}

www.jtcindia.org 63 Spring-5.2 Study Guide


Guide
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {
@Bean(name="myhai")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}
@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

Using @Autowired for Setter Methods


 You can use @Autowired for setter methods, then beans will be injected with Setter methods

@Autowired can be used for setter methods in two ways.


1) @Autowired(required=true) (Refer Lab34)

@Autowired
public void setAobj(A aobj)
{ this.aobj = aobj;
}

2) @Autowired(required=false) (Refer Lab35)

@Autowired(required=false)
public void setAobj(A aobj)
{ this.aobj = aobj;
}

www.jtcindia.org 64 Spring-5.2 Study Guide


Guide
Lab34: Files required
1. Lab34.java 2. A.java
3. B.java 4. Hai.java
5. Hello.java 6. JTCAppConfig.java

1. Lab34.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab34 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);
System.out.println("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}

2. A.java
package com.jtcindia.spring;

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class A {
String msg; //S.I

public void setMsg(String msg)


{ System.out.println("A - setMsg()");
this.msg=msg;
}
public String toString()
{ return msg;
}
}

www.jtcindia.org 65 Spring-5.2 Study Guide


Guide
3. B.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class B
{ String str;
//C.I

public B( String str) {


System.out.println("B -1 arg");
this.str = str;
}
public String toString()
{ return str;
}
}
4. Hai.java
package com.jtcindia.spring;

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
String msg; //Dependency

public void setMsg(String msg)


{ this.msg = msg;
}
public String toString()
{ return msg;
}
}

5. Hello.java
package com.jtcindia.spring;

import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

www.jtcindia.org 66 Spring-5.2 Study Guide


Guide
public class Hello {

@Autowired
private Hai hai; //Field Injection

private A aobj; //Setter Injection


private B bobj; //Setter Injection

@Autowired
public void setAobj(A aobj)
{ System.out.println("Hello-
setAobj()"); this.aobj = aobj;
}

@Autowired
public void setBobj(B bobj)
{ System.out.println("Hello-
setBobj()"); this.bobj = bobj;
}
public void show() {
System.out.println("Hello-show()");
System.out.println(hai);
System.out.println(aobj);
System.out.println(bobj);
}
}
6. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean");
return hai;
}

www.jtcindia.org 67 Spring-5.2 Study Guide


Guide
@Bean(name="myao")
public A createA() {
A ao=new A();
ao.setMsg("I am Bean - A");
return ao;
}

@Bean(name="mybo")
public B createB() {
B bo=new B("I am Bean - B");
return bo;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

Lab35: Files required


1. Lab35.java Same as Lab34
2. A.java Same as Lab34
3. B.java Same as Lab34
4. Hai.java Same as Lab34
5. Hello.java Updated in Lab35
6. JTCAppConfig.java Updated in Lab35

5. Hello.java
package com.jtcindia.spring;
import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
@Autowired
private Hai hai; //Field Injection

private A aobj; //Setter Injection


private B bobj; //Setter Injection

@Autowired (required=false)
public void setAobj(A aobj)
{ System.out.println("Hello-
setAobj()"); this.aobj = aobj;
}

www.jtcindia.org 68 Spring-5.2 Study Guide


Guide
@Autowired (required=false)
public void setBobj(B bobj)
{ System.out.println("Hello-
setBobj()"); this.bobj = bobj;
}
public void show() {
System.out.println("Hello-show()");
System.out.println(hai);
System.out.println(aobj);
System.out.println(bobj);
}
}
6. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 69 Spring-5.2 Study Guide


Guide
 Following annotations provided in javax.annotation package
1) @PostConstruct init()
2) @PreDestroy destroy()
3) @Resource
Note:
 When you want to use JSR-250 Annotations you must add javaee.jar file to project build
path.
1) @PostConstruct:
 You can mark the method with @PostConstruct Annotation.
 Method which is marked with @PostConstruct Annotation
o will be called by the Spring Container after creating Bean Instance
o contains the code for initializing bean instance with the required resources.

2) @PreDestroy:
 You can mark the method with @ PreDestroy Annotation.
 Method which is marked with @ PreDestroy Annotation
o will be called by the Spring Container before destroying Bean Instance
o contains the code for cleaning resources initialized with bean instance.

3) @Resource:
 You can mark the Bean Property with @Resource Annotation.
 When you use @Resource, then beans will be detected either based on byName or byType
process and injects them.
o When name attribute is specified for @Resource then uses byName autowire
process. (Refer Lab36)
o When name attribute is not specified for @Resource then uses byType autowire
process. (Refer Lab37)

Lab36: Files required


1. Lab36.java Same Lab22
2. Hai.java Same Lab22
3. Hello.java Updated in Lab36
4. JTCAppConfig.java Updated in Lab36

3. Hello.java
package com.jtcindia.spring;

import javax.annotation.Resource;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */

www.jtcindia.org 70 Spring-5.2 Study Guide


Guide
public class Hello {

@Resource //ByType(Default)
Hai hai; //Field Injection

public void show() {


System.out.println("Hello-show()");
System.out.println(hai);
}
}
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

Lab37: Files required


1. Lab37.java Same Lab22
2. Hai.java Same Lab22
3. Hello.java Updated in Lab37
4. JTCAppConfig.java Updated in Lab37

3. Hello.java
package com.jtcindia.spring;

import javax.annotation.Resource;
/*

www.jtcindia.org 71 Spring-5.2 Study Guide


Guide
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Resource(name="myhai2") //ByName
Hai hai; //Field Injection

public void show() {


System.out.println("Hello-show()");
System.out.println(hai);
}
}
4. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai1")
public Hai createHai1() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 1");
return hai;
}

@Bean(name="myhai2")
public Hai createHai2() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean 2");
return hai;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 72 Spring-5.2 Study Guide


Guide
 When you use @Inject, then by default, beans will be detected based on by Type
process and inject them directly without any Setter methods or Constructor.

 When you want to detect the beans based on by Name process then you need to @Qualifier
Annotation along with @Inject.

 Note: When you want to use @Inject Annotation then you must add javax.inject.jar file to
project build path.

Lab38: Files required


1. Lab38.java 2. A.java
3. Hai.java 4. Hello.java
5. JTCAppConfig.java

1. Lab38.java
package com.jtcindia.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Lab38 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext(JTCAppConfig.class);
System.out.println("---------Now Spring Container is Ready---- ");
Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}

2. A.java
package com.jtcindia.spring;
public class A {
String str; //Dependency
public void setStr(String str) {
this.str = str;
}
public String toString()
{ return str;
}
}

www.jtcindia.org 73 Spring-5.2 Study Guide


Guide
3. Hai.java
package com.jtcindia.spring;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
String msg; //Dependency

public void setMsg(String msg) {


this.msg = msg;
}

public String toString()


{ return msg;
}
}

4. Hello.java
package com.jtcindia.spring;

import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Qualifier;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {

@Inject
Hai hai; //ByType

@Inject
@Qualifier("myao")
A aobj; //ByName

public void show() {


System.out.println("Hello-show()");
System.out.println(hai);
System.out.println(aobj);
}
}

www.jtcindia.org 74 Spring-5.2 Study Guide


Guide
5. JTCAppConfig.java
package com.jtcindia.spring;

import org.springframework.context.annotation.*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
hai.setMsg("I am Hai Bean");
return hai;
}

@Bean(name="myao")
public A createA() {
A ao=new A();
ao.setStr("I am Bean - A");
return ao;
}

@Bean(name="myhello")
public Hello createHello()
{ return new Hello();
}
}

www.jtcindia.org 75 Spring-5.2 Study Guide


Guide

You might also like