A professional Spring Core mini project demonstrating the use of FactoryBean, Java-Based Configuration, and Dynamic Bean Creation using properties.
This project dynamically creates different image processor objects like:
- β Resize Processor
- β Filter Processor
- β Watermark Processor
based on the value provided inside the application.properties file.
- Spring
FactoryBeanimplementation - Dynamic object creation
- Property-based configuration
- Java annotation configuration
- Loose coupling using interfaces
- Easy to extend architecture
- Beginner-friendly project structure
- Java
- Spring Core
- FactoryBean
- Maven
- Annotation Configuration
src/main/java
β
βββ com.nit.config
β βββ AppConfig.java
β
βββ com.nit.factory
β βββ ImageProcessorFactoryBean.java
β
βββ com.nit.main
β βββ TestApp.java
β
βββ com.nit.sbeans
β βββ ImageProcessor.java
β βββ ResizeProcessor.java
β βββ FilterProcessor.java
β βββ WatermarkProcessor.java
β
βββ com.nit.properties
βββ application.propertiesThe application reads a property from:
image.process=resizeThe FactoryBean checks this value and dynamically creates the required implementation object.
| Property Value | Generated Bean |
|---|---|
| resize | ResizeProcessor |
| filter | FilterProcessor |
| watermark | WatermarkProcessor |
This project uses Spring's FactoryBean interface for custom object creation.
public class ImageProcessorFactoryBean implements FactoryBean<ImageProcessor>Instead of Spring directly creating objects, the factory decides which implementation to return at runtime.
public interface ImageProcessor {
public void process();
}Provides abstraction for all processors. :contentReference[oaicite:0]{index=0}
@Override
public ImageProcessor getObject() throws Exception {
if("resize".equalsIgnoreCase(imageProcessorType)) {
return new ResizeProcessor();
}
else if("filter".equalsIgnoreCase(imageProcessorType)) {
return new FilterProcessor();
}
if("watermark".equalsIgnoreCase(imageProcessorType)) {
return new WatermarkProcessor();
}
else {
throw new IllegalArgumentException("This service is not available");
}
}Dynamic object creation using property values. :contentReference[oaicite:1]{index=1}
@Configuration
@PropertySource("com/nit/properties/application.properties")Loads external properties into Spring container. :contentReference[oaicite:2]{index=2}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
ImageProcessor imageProcessor =
ctx.getBean(ImageProcessor.class);
imageProcessor.process();
}Runs the application and executes the dynamically generated bean. :contentReference[oaicite:3]{index=3}
git clone https://github.com/your-username/spring-factorybean-imageprocessor.gitOpen in:
- IntelliJ IDEA
- Eclipse
- VS Code
Inside application.properties
image.process=resizeAvailable options:
image.process=resizeimage.process=filterimage.process=watermarkExecute:
TestApp.javaImage resized successfully!Image Filtered successfully!Image Watermarked successfully!- FactoryBean
- Dynamic Bean Creation
- Spring Core
- Java Configuration
- Dependency Injection
- Interface-Based Design
- Loose Coupling
- Add grayscale processor
- Add compression processor
- Real image processing support
- Convert into Spring Boot application
- REST API integration
- File upload support
Contributions are welcome.
- Fork repository
- Create feature branch
- Commit changes
- Push code
- Open Pull Request
This project is licensed under the MIT License.
Developed using Java & Spring Core β€οΈ
If you found this project useful, give it a β on GitHub.