In this post, We will learn about the Spring Inner bean example using a Demo Project.
In the Spring framework, whenever a bean is used for only one particular property, it’s advised to declare it as an inner bean. Usually, the Spring inner bean is supported by both setter injection ‘property‘ and constructor injection ‘constructor-arg‘. Like Inner classes are the classes that are defined inside the scope of another class. Similarly, inner beans are the beans that are defined in the scope of another bean.
Injecting Inner Beans
1 2 3 4 5 |
<bean id="outerBean" class="com.kkjavatutorials.OuterBean"> <property name="innerBean"> <bean class="com.kkjavatutorials.InnerBean"/> </property> </bean> |
An inner bean usually does not require a defined id or name; if specified, the container does not use such a value as an identifier. The second key point is that Inner beans are always anonymous and they are always created within the scope of outer bean. It is not possible at all to inject inner beans into collaborating beans other than into the enclosing bean or to access them independently.
Now Let’s try to understand the Spring Inner bean 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>SpringInnerBeanExample</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> |
PaymentGateway.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 |
package com.kkhindigyan.model; public class PaymentGateway { private String paymentGatewayId; private Order order; public PaymentGateway(String paymentGatewayId, Order order) { super(); this.paymentGatewayId = paymentGatewayId; this.order = order; } public Order getOrder() { return order; } public String getPaymentGatewayId() { return paymentGatewayId; } /*public String getPaymentGatewayId() { return paymentGatewayId; } public void setPaymentGatewayId(String paymentGatewayId) { this.paymentGatewayId = paymentGatewayId; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; }*/ } |
Order.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 |
package com.kkhindigyan.model; public class Order { private String itemName; private Double itemPrice; private String address; public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public Double getItemPrice() { return itemPrice; } public void setItemPrice(Double itemPrice) { this.itemPrice = itemPrice; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Order [itemName=" + itemName + ", itemPrice=" + itemPrice + ", address=" + address + "]"; } } |
applicationContext.xml
Here com.kkhindigyan.model.Order is configured as an inner bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?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="paymentGateway" class="com.kkhindigyan.model.PaymentGateway"> <constructor-arg name="paymentGatewayId" value="Google Pay"></constructor-arg> <constructor-arg name="order"> <bean class="com.kkhindigyan.model.Order"> <property name="itemName" value="iphone 11"></property> <property name="itemPrice" value="80000.00"></property> <property name="address" value="MG Road,Mumbai"></property> </bean> </constructor-arg> </bean> </beans> |
ClientTest.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 |
package com.kkhindigyan.org; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.PaymentGateway; public class ClientTest { public static void main(String[] args) { AbstractApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); PaymentGateway paymentGateway = applicationContext.getBean(PaymentGateway.class); System.out.println(paymentGateway.getPaymentGatewayId()); System.out.println(paymentGateway.getOrder()); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) applicationContext.close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
Order [itemName=iphone 11, itemPrice=80000.0, address=MG Road,Mumbai]
That’s all about Spring Inner bean 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
getBean() overloaded methods in Spring Framework
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.