Spring Inner bean example

By | June 18, 2021

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

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

 

PaymentGateway.java

 

Order.java

 

applicationContext.xml

Here com.kkhindigyan.model.Order is configured as an inner bean.

 

ClientTest.java

If you run ClientTest.java as Java Application then it will give the below output: 

Google Pay
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.

Leave a Reply

Your email address will not be published.