Skip to content

Prakharpanchal7240/spring-bean-lifecycle-xml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Spring Bean Lifecycle using XML Configuration

This project demonstrates the complete Spring Bean Lifecycle using Spring Core and XML-based configuration.

It shows how Spring creates, initializes, manages, and destroys beans inside the IoC Container.


📚 Concepts Covered

  • Spring IoC Container
  • Bean Creation
  • Dependency Injection (Setter Injection)
  • XML Bean Configuration
  • Bean References (ref)
  • Bean Properties (property)
  • Custom Initialization Method
  • Custom Destruction Method
  • Bean Lifecycle
  • Maven Project Structure

🛠 Technologies Used

  • Java
  • Spring Framework (Core)
  • Maven
  • XML Configuration
  • Eclipse IDE

📁 Project Structure

src
├── main
│   ├── java
│   │   └── example
│   │       └── message_converters_withlifecycle
│   │            ├── App.java
│   │            ├── MessageConverter.java
│   │            ├── IMessageConverter.java
│   │            ├── HtmlMessageConverter.java
│   │            └── PDFMessageConverter.java
│   │
│   └── resources
│       └── com
│           └── configuration
│               └── application-context.xml

⚙️ Bean Configuration

The project configures beans using application-context.xml.

<bean id="msgConverter"
      class="example.message_converters_withlifecycle.MessageConverter"
      init-method="init"
      destroy-method="destroy">

    <property name="converter" ref="html"/>
    <property name="message" value="Hello guys"/>

</bean>

🔄 Bean Lifecycle Flow

Container Starts
        │
        ▼
Bean Instantiation
        │
        ▼
Dependency Injection
        │
        ▼
init() Method
        │
        ▼
Bean Ready to Use
        │
        ▼
Application Running
        │
        ▼
Context Closed
        │
        ▼
destroy() Method

▶️ Sample Output

Message Converter object created
Init method called
MessageConverter [converter=example.message_converters_withlifecycle.HtmlMessageConverter@..., message=Hello guys]
Destruction method called

🚀 How to Run

  1. Clone the repository
git clone <repository-url>
  1. Open the project in Eclipse or IntelliJ IDEA.

  2. Update Maven dependencies.

  3. Run:

App.java

📖 What You Will Learn

  • How Spring creates beans
  • Bean lifecycle inside the IoC container
  • XML-based bean configuration
  • Setter Dependency Injection
  • Bean references using ref
  • Custom initialization and destruction callbacks
  • Proper closing of ApplicationContext

📌 Future Improvements

  • BeanPostProcessor
  • InitializingBean
  • DisposableBean
  • @PostConstruct
  • @PreDestroy
  • Annotation-based configuration
  • Java Config
  • Prototype Bean Lifecycle
  • Bean Scopes

👨‍💻 Author

Prakhar Panchal

If you found this project helpful, consider giving it a ⭐.

About

Demonstrates the Spring Bean Lifecycle (Initialization & Destruction) using XML configuration, Dependency Injection, and custom init/destroy methods in a Maven-based Spring Core project.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages