In this post, We will learn about the getBean() overloaded methods in Spring Framework Example using a Demo Project.
Spring BeanFactory has the following five getBean() Overloaded methods:
- Object getBean(String name) throws BeansException;
- <T> T getBean(Class<T> requiredType) throws BeansException;
- <T> T getBean(Class<T> requiredType, Object… args) throws BeansException;
- <T> T getBean(String name, Class<T> requiredType) throws BeansException;
- Object getBean(String name, Object… args) throws BeansException;
Now Let’s try to understand the above getBean() overloaded methods using the below demo Project.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>SpringgetBeanOverloadedMethodsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Employee.java
Java Class which is registered as Spring bean in Spring Container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.kkhindigyan.model; public class Employee { private Integer employeeId; private String employeeName; private Double salary; private Address address; public Employee(Integer employeeId, String employeeName, Double salary, Address address) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.salary = salary; this.address = address; } public Integer getEmployeeId() { return employeeId; } public String getEmployeeName() { return employeeName; } public Double getSalary() { return salary; } public Address getAddress() { return address; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + "]"; } } |
Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkhindigyan.model; public class Address { private String addressLine1; private String addressLine2; private String city; private Long zipCode; public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getZipCode() { return zipCode; } public void setZipCode(Long zipCode) { this.zipCode = zipCode; } @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
applicationContext.xml
Spring bean XML Config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="employee" class="com.kkhindigyan.model.Employee" scope="prototype"> <constructor-arg name="employeeId" value="102883" type="java.lang.Integer" index="0"></constructor-arg> <constructor-arg name="employeeName" value="Sean" type="java.lang.String" index="1"></constructor-arg> <constructor-arg name="salary" value="80000" type="java.lang.Double" index="2"></constructor-arg> <constructor-arg name="address" ref="address" type="com.kkhindigyan.model.Address" index="3"></constructor-arg> </bean> <bean id="address" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value=""></property> <property name="addressLine2" value=""></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> </beans> |
ClientTest.java
Client Program with the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("1.==================================================================="); Object object = applicationContext.getBean("employee"); Employee employee = (Employee)object; System.out.println(employee); Address address = employee.getAddress(); System.out.println(address); System.out.println("2.==================================================================="); Employee employee2 = applicationContext.getBean(Employee.class); Address address2 = employee2.getAddress(); System.out.println(employee2); System.out.println(address2); System.out.println("3.==================================================================="); Employee employee3 = applicationContext.getBean(Employee.class, 10001,"KK",80000.00,address2); Address address3 = employee3.getAddress(); System.out.println(employee3); System.out.println(address3); System.out.println("4.==================================================================="); Employee employee4 = applicationContext.getBean("employee", Employee.class); Address address4 = employee4.getAddress(); System.out.println(employee4); System.out.println(address4); System.out.println("5.==================================================================="); Object object2 = applicationContext.getBean("employee", 10002,"PK",70000.00,address2); Employee employee5 =(Employee)object2; Address address5 = employee5.getAddress(); System.out.println(employee5); System.out.println(address5); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
1.=================================================================== Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] 2.=================================================================== Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] 3.=================================================================== Employee [employeeId=10001, employeeName=KK, salary=80000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] 4.=================================================================== Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] 5.=================================================================== Employee [employeeId=10002, employeeName=PK, salary=70000.0] Address [addressLine1=, addressLine2=, city=Delhi, zipCode=959049] |
That’s all about getBean() overloaded methods in Spring Framework Example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.