[go: up one dir, main page]

0% found this document useful (0 votes)
91 views75 pages

Spring 5.2 Study Guide Overview

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views75 pages

Spring 5.2 Study Guide Overview

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

-

SPRING 5.2
STUDY GUIDE

Author
Som Prakash Rai
S
o
[Link] 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.

[Link] 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){


[Link]=sid; //1 Setter Injection
}
public Student(String sname){
[Link]=sname; //2 Constructor Injection
}
}

[Link] 3 Spring-5.2 Study Guide


Guide
Download the Spring Jars
1) Open this url
[Link]
2) Click on [Link]
3) Click ON [Link] ... It downloads the ZIP
4) Unzip the [Link] then you will see the folder called spring-
[Link]
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 [Link]
6) Write the following 3 Beans
a. [Link]
b. [Link]
c. [Link]
7) Write the Client Program without Spring
a. [Link]
8) Run Lab1A
9) Configuring the Beans in Configuration Class
[Link]
10) Write the Client Program
a. [Link]
11) Run the Lab1B

Lab1: Files required

1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]

[Link] 4 Spring-5.2 Study Guide


Guide
1. [Link] 2. [Link]
package [Link]; package [Link];
/*
* @Author : Som Prakash import
* @Company : jtcindia [Link]
* @Website : [Link] xt;
* */ import
public class Lab1A { [Link]
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 : [Link]
[Link](101); * */
[Link]("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); [Link]);
[Link](ao); [Link]("--------- Now Spring
Container is Ready----- ");
[Link]();
A aobj=(A)[Link]("createA");
} [Link](aobj);
}
B bobj=(B)[Link]("BO");
[Link](bobj);

Hello h=(Hello)[Link]("myhello");
[Link]();

}
}

[Link] 5 Spring-5.2 Study Guide


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

5. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
A aobj; //S.I
B bobj; //C.I
static {
[Link]("Hello - S.B");
}
public Hello(B bobj)
{ [Link]("Hello(B) -
1arg"); [Link] = bobj;
}
public void setAobj(A aobj)
{ [Link]("Hello-
setAobj()");
[Link] 6 Spring-5.2 Study Guide
Guide
[Link] = aobj;
}
public void show() {
[Link]("Hello-show()");
[Link](aobj);
[Link](bobj);
}
}
6. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

@Bean
public A createA() {
//Task1 : Create and Initialize A object
A ao=new A();
[Link](101);
[Link]("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);
[Link](ao);
return h;
}
}

[Link] 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")

[Link] 8 Spring-5.2 Study Guide


Guide
Bean Scope Example with Java Configuration
Lab2: Files required
1. [Link] 2. [Link]
3. [Link] 4. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab2 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello1=(Hello)[Link]("hello");
Hello hello2=(Hello)[Link]("hello");
[Link](hello1==hello2);

Hai hai1=(Hai)[Link]("hai");
Hai hai2=(Hai)[Link]("hai");
[Link](hai1==hai2);

Hello hello=(Hello)[Link]("hello");
[Link]();

Hai hai=(Hai)[Link]("hai");
[Link]();
}
}

2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

static {

[Link] 9 Spring-5.2 Study Guide


Guide
[Link]("Hello - S.B");
}
public Hello()
{ [Link]("Hello - D.C");
}
public void showHello() {
[Link]("Hello-showHello()");
}
}

3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {

static
{ [Link]("Hai -
S.B");
}
public Hai()
{ [Link]("Hai - D.C");
}
public void showHai() {
[Link]("Hai-showHai()");
}
}
4. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

@Bean("hello")
@Scope("singleton")
public Hello createHello() {
[Link]("-------createHello()-------- called");

[Link] 10 Spring-5.2 Study Guide


Guide
return new Hello();
}
@Bean("hai")
@Scope("prototype")
public Hai createHai() {
[Link]("-------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();
}

[Link] 11 Spring-5.2 Study Guide


Guide
Bean Loading Types Example with Java Configuration:
Lab3: Files required
1. [Link] New One
2. [Link] Same as Lab2
3. [Link] Same as Lab2
4. [Link] New One

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab3 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("hello");
[Link]();

Hai hai=(Hai)[Link]("hai");
[Link]();

}
}

4. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

[Link] 12 Spring-5.2 Study Guide


Guide
@Configuration
public class JTCAppConfig {

@Bean("hello")
@Scope("singleton")
@Lazy(true)
public Hello createHello() {
[Link]("-------createHello()-------- called");
return new Hello();
}

@Bean("hai")
@Scope("prototype")
@Lazy(true)
public Hai createHai() {
[Link]("-------createHai()--------called");
return new Hai();
}
}

[Link] 13 Spring-5.2 Study Guide


Guide
Lab4: Files required
5. [Link] 6. [Link]
7. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab4 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
Runtime rt =(Runtime)[Link]("myruntime");
[Link]([Link]());
} }

2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello
{ static Hello hello;
static {
hello=new Hello();
}
private Hello() {
}
public static Hello getHello()
{ return hello;
}
public void show() {
[Link]("Hello-show()");
} }

[Link] 14 Spring-5.2 Study Guide


Guide
3. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig
{ @Bean("myhello")
public Hello createHello() {
[Link]("-------createHello()-------- called");
Hello hello=[Link]();
return hello;
}
@Bean("myruntime")
public Runtime createRT() {
[Link]("-------createRT()--------called");
Runtime rt=[Link]();
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


( [Link], [Link], [Link]);

[Link] 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 ([Link]);

 Import Remaining Configuration classes using @Import Annotation

@Import({[Link],[Link]})
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.

[Link] - Configure A bean here


[Link] - Configure B bean here
JTCAppConfig -- Hello,Hai

Note : place [Link] and [Link] in src folder.

 Specify the One Configuration class as Parameters for Constructor of


AnnotationConfigApplicationContext

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);

 Import Remaining Configuration XML files using @ ImportResource Annotation

@ImportResource({"[Link]","[Link]"})
public class JTCAppConfig {

Lab5: Files required


1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link] 8. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];

[Link] 16 Spring-5.2 Study Guide


Guide
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab5 {
public static void main(String[] args)
{ ApplicationContext ctx=new
AnnotationConfigApplicationContext([Link],[Link],[Link]
);
[Link]("---------Now Spring Container is Ready----- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
Hai hai=(Hai) [Link]("myhai");
[Link]();
}
}

2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class A
{ int a; //S.I
String msg; //S.I
static {
[Link]("A - S.B");
}
public A() {
[Link]("A - D.C");
}
public void setA(int a) {
[Link]("A - setA()");
this.a = a;
}
public void setMsg(String msg)
{ [Link]("A - setMsg()");
[Link] = msg;
}
public String toString()
{ return ""+a
+"\t"+msg;
}
}

[Link] 17 Spring-5.2 Study Guide


Guide
3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class B
{ int b; //C.I
String str; //C.I
static {
[Link]("B - S.B");
}
public B(int b, String str)
{ [Link]("B - 2 arg");
this.b = b;
[Link] = str;
}
public String toString() {
return ""+b+"\t"+str;
}
}

4. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {
A aobj; //S.I
B bobj; //S.I
static {
[Link]("Hai - S.B");
}
public Hai() {
[Link]("Hai() - D.C");
}
public void setAobj(A aobj)
{ [Link]("Hai- setAobj()
");
[Link] = aobj;
}
public void setBobj(B bobj)
{ [Link]("Hai- setBobj()
"); [Link] = bobj;
}

[Link] 18 Spring-5.2 Study Guide


Guide
public void show()
{ [Link]("Hai-
show()");
[Link](aobj);
[Link](bobj);
}
}
5. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
A aobj; //C.I
B bobj; //C.I
static {
[Link]("Hello - S.B");
}
public Hello() {
[Link]("Hello() - D.C");
}
public Hello(A aobj,B bobj)
{ [Link]("Hello(A,B) - 2arg");
[Link]=aobj;
[Link] = bobj;
}
public void show()
{ [Link]("Hello-
show()"); [Link](aobj);
[Link](bobj);
}
}
6. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig1 {

[Link] 19 Spring-5.2 Study Guide


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

7. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig2 {

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

8. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : Jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig3 {

[Link] 20 Spring-5.2 Study Guide


Guide
@Bean("myhai")
public Hai createHai(A ao,B bo)
{ Hai hai=new Hai();
[Link](ao);
[Link](bo);
return hai;
}
}

Lab6: Files required


1. [Link] New One
2. [Link] Same as Lab5
3. [Link] Same as Lab5
4. [Link] Same as Lab5
5. [Link] Same as Lab5
6. [Link] New One
7. [Link] Same as Lab5
8. [Link] Same as Lab5

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab6 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();

Hai hai=(Hai) [Link]("myhai");


[Link]();
}
}

[Link] 21 Spring-5.2 Study Guide


Guide
6. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
@Import({[Link],[Link]})
public class JTCAppConfig1 {

@Bean("ao")
public A createA()
{ A ao=new A();
[Link](101);
[Link]("I am A");
return ao;
}
@Bean("bo")
public B createB() {
B bo=new B(102,"I am B");
return bo;
}
}

Lab7: Files required


1. [Link] New One
2. [Link] Same as Lab5
3. [Link] Same as Lab5
4. [Link] Same as Lab5
5. [Link] Same as Lab5
6. [Link] New One
7. [Link] New One
8. [Link] New One

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia

[Link] 22 Spring-5.2 Study Guide


Guide
* @Website : [Link]
* */
public class Lab7 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();

Hai hai=(Hai) [Link]("myhai");


[Link]();
}
}

6. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
@ImportResource({"[Link]","[Link]"})
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();
[Link](ao);
[Link](bo);
return hai;
}

[Link] 23 Spring-5.2 Study Guide


Guide
7. [Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]

<bean id="aobj" class="[Link].A">


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

</beans>

8. [Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]

<bean id="bobj" class="[Link].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. [Link] 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

[Link] 24 Spring-5.2 Study Guide


Guide
Lab8: Files required
1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab8 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Customer cust=(Customer) [Link]("mycust");


[Link](cust);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

for(Account acc:[Link]())
{ [Link](acc);
}
}
}

2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Address
{ private String
street; private String
city; private String
state; public
Address() {}

[Link] 25 Spring-5.2 Study Guide


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

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

4. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

[Link] 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();
[Link] = cid;
[Link] = cname;
[Link] = email;
[Link] = phone;
}
//Setters and Getters
@Override
public String toString() {
return cid + ", " + cname + ", " + email + ", " + phone;
}
}

5. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig
{ @Bean(name="myemails")
public List<String> getEmails(){
[Link]("JTCAppConfig -getEmails() ");
List<String> ems=new ArrayList<>();
[Link]("sp@jtc");
[Link]("rai@jtc");
[Link]("som@jtc");
return ems;
}

[Link] 27 Spring-5.2 Study Guide


Guide
@Bean(name="myphones")
public Set<Integer> getPhones(){
[Link]("JTCAppConfig -getPhones() ");
Set<Integer> phs=new TreeSet<>();
[Link](111);
[Link](222);
[Link](333);
return phs;
}

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

@Bean(name="myprops")
public Properties getProps(){
[Link]("JTCAppConfig -getProps() ");
Properties props=new Properties();
[Link]("A",11);
[Link]("B",22);
[Link]("C",33);
[Link]("D",44);
return props;
}

@Bean(name="myadd")
public Address getAdd() {
[Link]("JTCAppConfig -getAdd() ");
Address add=new Address();
[Link]("BTM Layout");
[Link]("Noida");
[Link]("noida
"); return add;
}

@Bean(name="myaccs")
public List<Account>
getAccounts(){ [Link]("JTCAppConfig -
getAccounts() ");
List<Account> myaccs=new ArrayList<>();
[Link](new Account(101,"SA",15000));

[Link] 28 Spring-5.2 Study Guide


Guide
[Link](new Account(102,"CA",25000));
[Link](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) {

[Link]("JTCAppConfig -createCustomer() ");


Customer cust=new Customer(101,"SomPrakash","Som@[Link]",12345);
[Link](myemails);
[Link](myphones);
[Link](myrefs);
[Link](myprops);
[Link](myadd);
[Link](myaccs);
return cust;
}
}

Lab9: Files required


1. [Link] 2. [Link]
3. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab9 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Customer cust=(Customer) [Link]("mycust");


[Link](cust);
[Link]([Link]());
[Link]([Link]());
}
}

[Link] 29 Spring-5.2 Study Guide


Guide
2. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
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();
[Link] = cid;
[Link] = cname;
[Link] = email;
[Link] = phone;
[Link] = emails;
[Link] = phones;
}

//Setters and Getters


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

3. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

[Link] 30 Spring-5.2 Study Guide


Guide
@Bean(name="myemails")
public List<String> getEmails(){
List<String> ems=new ArrayList<>();
[Link]("som@jtc");
[Link]("rai@jtc");
[Link]("sp@jtc");
return ems;
}
@Bean(name="myphones")
public List<String> getPhones(){
List<String> phs=new ArrayList<>();
[Link]("111");
[Link]("222");
[Link]("333");
return phs;
}
@Bean(name="mycust")
public Customer createCustomer(List<String> myemails,List<String> myphones)
{ Customer cust=new
Customer(101,"SomPrakash","Som@[Link]",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();
[Link](hai);
return h;
}
 In the above Example, We are explicitly specifying the Hello dependency called Hai

[Link] 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
'[Link]' 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. [Link] 2. [Link]
3. [Link] 4. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab10 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
}
}

[Link] 32 Spring-5.2 Study Guide


Guide
2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {
static {
[Link]("Hai - S.B");
}
public Hai() {
[Link]("Hai() - D.C");
}

public String toString() {


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

3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
Hai hai; //1. Dependency

static {
[Link]("Hello - S.B");
}
public Hello() {
[Link]("Hello() - D.C");
}

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


[Link]("Hello - setHai()");
[Link] = hai;
}

public void show() {


[Link]("Hello-show()");
[Link](hai);
}
}

[Link] 33 Spring-5.2 Study Guide


Guide
4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig
{ @Bean("myhello")
public Hello createHello(Hai hai) { //1. Dependency
Hello h=new Hello();
[Link](hai); //2. Setter Injection
return h;
}
}

Lab11: Files required


1. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

[Link] 34 Spring-5.2 Study Guide


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

Lab12: Files required


1. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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();
[Link](hai); //2. Setter Injection
return h;
}
}

[Link] 35 Spring-5.2 Study Guide


Guide
Lab13: Files required
1. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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();
[Link](hai); //2. Setter Injection
return h;
}
}

[Link] 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: [Link]: No
qualifying bean of type '[Link]' available: expected single matching
bean but found 3: myhai1,myhai2,hai

[Link] 37 Spring-5.2 Study Guide


Guide
Lab14: Files required
1. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*

[Link] 38 Spring-5.2 Study Guide


Guide
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

[Link] 39 Spring-5.2 Study Guide


Guide
Lab17: Files required
1. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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. [Link] Same as Lab10
2. [Link] Same as Lab10
3. [Link] Same as Lab10
4. [Link] Updated Here

4. [Link]
package [Link];

import [Link].*;
import [Link];
import [Link];
/*
* @Author : Som Prakash

[Link] 40 Spring-5.2 Study Guide


Guide
* @Company : jtcindia
* @Website : [Link]
* */
@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. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia

[Link] 41 Spring-5.2 Study Guide


Guide
* @Website : [Link]
* */
public class Lab19 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);
[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
}
}

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

[Link] 42 Spring-5.2 Study Guide


Guide
5. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class CustomerDAOImpl implements CustomerDAO
{ @Override
public void addCustomer() {
[Link]("----AddCustomer---");
}
}

6. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
A aobj; //1
CustomerDAO customerDAO; //2

public void setAobj(A aobj)


{ [Link] = aobj;
}
public void setCustomerDAO(CustomerDAO customerDAO)
{ [Link] = customerDAO;
}
public void show() {
[Link](aobj);
[Link]();
}
}

7. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]

[Link] 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. [Link] 2. [Link]
3. [Link] 4. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab20 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);
[Link]("---------Now Spring Container is Ready---- ");
}
}

[Link] 44 Spring-5.2 Study Guide


Guide
2. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {

Hello hello; //Dependency

public Hai() {
[Link]("Hai-D.C");
}
public void setHello(Hello hello) { //Setter Injection
[Link]("Hai - setHello()");
[Link] = hello;
}
}

3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

Hai hai; //Dependency

public Hello() {
[Link]("Hello-D.C");
}
public void setHai(Hai hai) { //Setter Injection
[Link]("Hello-setHai()");
[Link] = hai;
}
}

[Link] 45 Spring-5.2 Study Guide


Guide
4. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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;
}
}

[Link] 46 Spring-5.2 Study Guide


Guide
Lab21: Files required
1. [Link] Same as Lab19
2. [Link] Same as Lab19
3. [Link] Same as Lab19
4. [Link] Same as Lab19
5. [Link] Same as Lab19
6. [Link] Same as Lab19
7. [Link] Updated in Lab21

7. [Link]
package [Link];

import [Link];
import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@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();
}
}

[Link] 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
'[Link]' 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)
 [Link]: No qualifying
bean of type '[Link]' available: expected single matching bean but
found 3: myhai1,myhai2,myhai3

[Link] 48 Spring-5.2 Study Guide


Guide
Lab22: Files required
1. [Link] 2. [Link]
3. [Link] 4. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab22 {
public static void main(String[] args) {

ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);


[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
}
}

2. [Link]
package [Link];

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {
String msg;

public void setMsg(String msg)


{ [Link] = msg;
}

public String toString()


{ return msg;
}
}

[Link] 49 Spring-5.2 Study Guide


Guide
3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

@Autowired
Hai hai; //1

public void show() {


[Link](hai);
}
}

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

[Link] 50 Spring-5.2 Study Guide


Guide
Lab23: Files required
1. [Link] Same as Lab22
2. [Link] Same as Lab22
3. [Link] Same as Lab22
4. [Link] Updated in Lab23

4. [Link]
package [Link];

import [Link];
import [Link];

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

@Configuration
public class JTCAppConfig {

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

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

[Link] 51 Spring-5 Study Guide


Lab24: Files required
1. [Link] Same as Lab22
2. [Link] Same as Lab22
3. [Link] Same as Lab22
4. [Link] Updated in Lab24

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

@Configuration
public class JTCAppConfig {

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

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

@Bean(name="hai")
public Hai createHai() {
Hai hai=new Hai();
[Link]("I am also Hai Bean");
return hai;
}

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

[Link] 52 Spring-5.2 Study Guide


Lab25: Files required
1. [Link] Same as Lab22
2. [Link] Same as Lab22
3. [Link] Same as Lab22
4. [Link] Updated in Lab25

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

@Configuration
public class JTCAppConfig {

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

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

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

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

[Link] 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)
 [Link]: No qualifying
bean of type '[Link]' available: expected single matching bean but
found 3: myhai1,myhai2,myhai3

Lab26: Files required


1. [Link] Same as Lab22
2. [Link] Same as Lab22
3. [Link] Updated in Lab26
4. [Link] Updated in Lab26

3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link](hai);
}
}

[Link] 54 Spring-5.2 Study Guide


4. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link](hai);
}
}

[Link] 55 Spring-5.2 Study Guide


4. [Link]
package [Link];

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

Lab28: Files required


5. [Link] Same Lab22
6. [Link] Same Lab22
7. [Link] Updated in Lab28
8. [Link] Updated in Lab28

3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
@Autowired(required = false)
Hai hai; //1

public void show() {


[Link](hai);
}
}

[Link] 56 Spring-5.2 Study Guide


4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

@Configuration
public class JTCAppConfig {

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

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

@Bean(name="hai")
public Hai createHai() {
Hai hai=new Hai();
[Link]("I am also Hai Bean");
return hai;
}

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

[Link] 57 Spring-5.2 Study Guide


Lab29: Files required
1. [Link] Same Lab22
2. [Link] Same Lab22
3. [Link] Updated in Lab29
4. [Link] Updated in Lab29

3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link](hai);
}
}

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

[Link] 58 Spring-5.2 Study Guide


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

@Bean(name="myhai3")
public Hai createHai() {
Hai hai=new Hai();
[Link]("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
'[Link]' 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.

[Link] 59 Spring-5.2 Study Guide


Lab30: Files required
1. [Link] Same Lab22
2. [Link] Same Lab22
3. [Link] Updated in Lab30
4. [Link] Updated in Lab30

3. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link](hai);
}
}

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

[Link] 60 Spring-5.2 Study Guide


Lab31: Files required
1. [Link] Same Lab22
2. [Link] Same Lab22
3. [Link] Updated in Lab31
4. [Link] Updated in Lab31

3. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
@Autowired
@Qualifier("myhai")
Hai hai; //1

public void show() {


[Link](hai);
}
}

4. [Link]
package [Link];

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

[Link] 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. [Link] Same Lab22
6. [Link] Same Lab22
7. [Link] Updated in Lab32
8. [Link] Updated in Lab32

3. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link](hai);
}
}

[Link] 62 Spring-5.2 Study Guide


Guide
4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

Lab33: Files required


5. [Link] Same Lab22
6. [Link] Same Lab22
7. [Link] Updated in Lab33
8. [Link] Updated in Lab33

3. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {
@Autowired(required=false)
@Qualifier("myhai")
Hai hai; //1

public void show() {


[Link](hai);
}
}

[Link] 63 Spring-5.2 Study Guide


Guide
4. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {
@Bean(name="myhai")
public Hai createHai1() {
Hai hai=new Hai();
[Link]("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)
{ [Link] = aobj;
}

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

@Autowired(required=false)
public void setAobj(A aobj)
{ [Link] = aobj;
}

[Link] 64 Spring-5.2 Study Guide


Guide
Lab34: Files required
1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab34 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);
[Link]("---------Now Spring Container is Ready---- ");

Hello hello=(Hello)[Link]("myhello");
[Link]();
}
}

2. [Link]
package [Link];

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

public void setMsg(String msg)


{ [Link]("A - setMsg()");
[Link]=msg;
}
public String toString()
{ return msg;
}
}

[Link] 65 Spring-5.2 Study Guide


Guide
3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class B
{ String str;
//C.I

public B( String str) {


[Link]("B -1 arg");
[Link] = str;
}
public String toString()
{ return str;
}
}
4. [Link]
package [Link];

/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {
String msg; //Dependency

public void setMsg(String msg)


{ [Link] = msg;
}
public String toString()
{ return msg;
}
}

5. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

[Link] 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)
{ [Link]("Hello-
setAobj()"); [Link] = aobj;
}

@Autowired
public void setBobj(B bobj)
{ [Link]("Hello-
setBobj()"); [Link] = bobj;
}
public void show() {
[Link]("Hello-show()");
[Link](hai);
[Link](aobj);
[Link](bobj);
}
}
6. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
[Link]("I am Hai Bean");
return hai;
}

[Link] 67 Spring-5.2 Study Guide


Guide
@Bean(name="myao")
public A createA() {
A ao=new A();
[Link]("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. [Link] Same as Lab34
2. [Link] Same as Lab34
3. [Link] Same as Lab34
4. [Link] Same as Lab34
5. [Link] Updated in Lab35
6. [Link] Updated in Lab35

5. [Link]
package [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
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)
{ [Link]("Hello-
setAobj()"); [Link] = aobj;
}

[Link] 68 Spring-5.2 Study Guide


Guide
@Autowired (required=false)
public void setBobj(B bobj)
{ [Link]("Hello-
setBobj()"); [Link] = bobj;
}
public void show() {
[Link]("Hello-show()");
[Link](hai);
[Link](aobj);
[Link](bobj);
}
}
6. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
[Link]("I am Hai Bean");
return hai;
}

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

[Link] 69 Spring-5.2 Study Guide


Guide
 Following annotations provided in [Link] package
1) @PostConstruct init()
2) @PreDestroy destroy()
3) @Resource
Note:
 When you want to use JSR-250 Annotations you must add [Link] 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. [Link] Same Lab22
2. [Link] Same Lab22
3. [Link] Updated in Lab36
4. [Link] Updated in Lab36

3. [Link]
package [Link];

import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */

[Link] 70 Spring-5.2 Study Guide


Guide
public class Hello {

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

public void show() {


[Link]("Hello-show()");
[Link](hai);
}
}
4. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

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

Lab37: Files required


1. [Link] Same Lab22
2. [Link] Same Lab22
3. [Link] Updated in Lab37
4. [Link] Updated in Lab37

3. [Link]
package [Link];

import [Link];
/*

[Link] 71 Spring-5.2 Study Guide


Guide
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

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

public void show() {


[Link]("Hello-show()");
[Link](hai);
}
}
4. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

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

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

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

[Link] 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 [Link] file to
project build path.

Lab38: Files required


1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link]

1. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Lab38 {
public static void main(String[] args) {
ApplicationContext ctx=new AnnotationConfigApplicationContext([Link]);
[Link]("---------Now Spring Container is Ready---- ");
Hello hello=(Hello)[Link]("myhello");
[Link]();
}
}

2. [Link]
package [Link];
public class A {
String str; //Dependency
public void setStr(String str) {
[Link] = str;
}
public String toString()
{ return str;
}
}

[Link] 73 Spring-5.2 Study Guide


Guide
3. [Link]
package [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hai {
String msg; //Dependency

public void setMsg(String msg) {


[Link] = msg;
}

public String toString()


{ return msg;
}
}

4. [Link]
package [Link];

import [Link];
import [Link];
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
public class Hello {

@Inject
Hai hai; //ByType

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

public void show() {


[Link]("Hello-show()");
[Link](hai);
[Link](aobj);
}
}

[Link] 74 Spring-5.2 Study Guide


Guide
5. [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : [Link]
* */
@Configuration
public class JTCAppConfig {

@Bean(name="myhai")
public Hai createHai() {
Hai hai=new Hai();
[Link]("I am Hai Bean");
return hai;
}

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

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

[Link] 75 Spring-5.2 Study Guide


Guide

You might also like