P1 Spring-5.2.5 - Study Guide
P1 Spring-5.2.5 - Study Guide
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.
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.
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
7) Copy 21-Binary Jars into required Folder to use in your Eclipse Projects.
1. Lab1A.java 2. Lab1B.java
3. A.java 4. B.java
5. Hello.java 6. JTCAppConfig.java
Hello h=(Hello)ctx.getBean("myhello");
h.show();
}
}
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;
}
}
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.
Usage:
@Scope(value="singleton")
@Scope(value=" prototype ")
@Scope("singleton")
@Scope("prototype")
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) {
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 {
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");
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")
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();
}
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) {
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
* */
@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();
}
}
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) {
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()");
} }
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.
@Import({JTCAppConfig1.class,JTCAppConfig3.class})
public class JTCAppConfig2{
}...
@ImportResource({"jtc1.xml","jtc2.xml"})
public class JTCAppConfig {
1. Lab5.java
package com.jtcindia.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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;
}
}
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;
}
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 {
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 {
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) {
Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
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;
}
}
1. Lab7.java
package com.jtcindia.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
Hello hello=(Hello)ctx.getBean("myhello");
hello.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;
}
</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">
</beans>
All of these types of Bean Properties can be Injected with Setter Injection or Constructor Injection. Lab8:
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) {
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() {}
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
* */
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;
}
@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));
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) {
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;
}
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 {
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
Note: In the case of Collections Only , Container creates the Empty Object and passes that as
parameter .(Ref. Lab9)
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
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) {
Hello hello=(Hello)ctx.getBean("myhello");
hello.show();
}
}
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");
}
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;
}
}
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;
}
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;
}
}
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;
}
}
@Bean(name="myhello",autowire = Autowire.BY_NAME)
@Bean(name="myhello",autowire = Autowire.BY_BYTE)
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
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;
}
}
4. JTCAppConfig.java
package com.jtcindia.spring;
import java.util.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
@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;
}
}
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;
}
}
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;
}
}
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
@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;
}
}
1. Lab19.java
package com.jtcindia.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/*
* @Author : Som Prakash
* @Company : jtcindia
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();
}
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
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
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---- ");
}
}
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 {
public Hello() {
System.out.println("Hello-D.C");
}
public void setHai(Hai hai) { //Setter Injection
System.out.println("Hello-setHai()");
this.hai = hai;
}
}
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;
}
}
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();
}
}
Using @Autowired:
@Autowired can be used in two ways.
1. ByType autowire process
2. ByName autowire process
A) @Autowired(required=true) / @Autowired
Identified single bean will be Injected Directly without any setter method.
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
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) {
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;
import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hello {
@Autowired
Hai hai; //1
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();
}
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();
}
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();
}
}
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();
}
}
Identified single bean will be Injected Directly without any setter method.
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
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
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
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();
}
}
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
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();
}
}
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
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="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();
}
}
A) @Autowired(required=true) / @Autowired
@Autowired(required=true)
@Qualifier("myhai")
Identified single bean will be Injected Directly without any setter method.
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
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();
}
}
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
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();
}
}
B) @Autowired(required=false)
@Qualifier("myhai")
Identified single bean will be Injected Directly without any setter method.
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
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();
}
}
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
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();
}
}
@Autowired
public void setAobj(A aobj)
{ this.aobj = aobj;
}
@Autowired(required=false)
public void setAobj(A aobj)
{ this.aobj = aobj;
}
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
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
public class Hai {
String msg; //Dependency
5. Hello.java
package com.jtcindia.spring;
import org.springframework.beans.factory.annotation.Autowired;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Autowired
private Hai hai; //Field 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;
}
@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();
}
}
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
@Autowired (required=false)
public void setAobj(A aobj)
{ System.out.println("Hello-
setAobj()"); this.aobj = aobj;
}
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();
}
}
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)
3. Hello.java
package com.jtcindia.spring;
import javax.annotation.Resource;
/*
* @Author : Som Prakash
* @Company : jtcindia
* @Website : www.jtcindia.org
* */
@Resource //ByType(Default)
Hai hai; //Field Injection
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();
}
}
3. Hello.java
package com.jtcindia.spring;
import javax.annotation.Resource;
/*
@Resource(name="myhai2") //ByName
Hai hai; //Field Injection
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();
}
}
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.
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;
}
}
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
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();
}
}