Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6596011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:00:57+00:00 2026-05-25T18:00:57+00:00

I want to have inject a bean based on a String parameter passed from

  • 0

I want to have inject a bean based on a String parameter passed from client.

public interface Report {
    generateFile();
}

public class ExcelReport extends Report {
    //implementation for generateFile
}

public class CSVReport extends Report {
    //implementation for generateFile
}

class MyController{
    Report report;
    public HttpResponse getReport() {
    }
}

I want report instance to be injected based on the parameter passed. Any help would be greatly appretiated. Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:00:58+00:00Added an answer on May 25, 2026 at 6:00 pm

    Use Factory method pattern:

    public enum ReportType {EXCEL, CSV};
    
    @Service
    public class ReportFactory {
    
        @Resource
        private ExcelReport excelReport;
    
        @Resource
        private CSVReport csvReport
    
        public Report forType(ReportType type) {
            switch(type) {
                case EXCEL: return excelReport;
                case CSV: return csvReport;
                default:
                    throw new IllegalArgumentException(type);
            }
        }
    }
    

    The report type enum can be created by Spring when you call your controller with ?type=CSV:

    class MyController{
    
        @Resource
        private ReportFactory reportFactory;
    
        public HttpResponse getReport(@RequestParam("type") ReportType type){
            reportFactory.forType(type);
        }
    
    }
    

    However ReportFactory is pretty clumsy and requires modification every time you add new report type. If the report types list if fixed it is fine. But if you plan to add more and more types, this is a more robust implementation:

    public interface Report {
        void generateFile();
        boolean supports(ReportType type);
    }
    
    public class ExcelReport extends Report {
        publiv boolean support(ReportType type) {
            return type == ReportType.EXCEL;
        }
        //...
    }
    
    @Service
    public class ReportFactory {
    
        @Resource
        private List<Report> reports;
    
        public Report forType(ReportType type) {
            for(Report report: reports) {
                if(report.supports(type)) {
                    return report;
                }
            }
            throw new IllegalArgumentException("Unsupported type: " + type);
        }
    }
    

    With this implementation adding new report type is as simple as adding new bean implementing Report and a new ReportType enum value. You could get away without the enum and using strings (maybe even bean names), however I found strongly typing beneficial.


    Last thought: Report name is a bit unfortunate. Report class represents (stateless?) encapsulation of some logic (Strategy pattern), whereas the name suggests it encapsulates value (data). I would suggest ReportGenerator or such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have HttpModule to inject javascripts, css links into HEAD element from
I want to have a class which implements an interface, which specifies the specific
Assume we have a simple @Configuration : @Configuration public class FooBarConfiguration { @Bean public
I have a Spring bean, let's say: @TransactionAttribute(TransactionAttributeType.REQUIRED) public class AImpl implements A {
I have a bean that i want to inject with a named list using
I have a service that I want to inject into multiple client classes. I
I want to have a PHP script send a XML formatted string to another
I want to have a web based admin to upload, delete files and folders
i have to inject a dll into some process .now i want that after
(Java Spring WebApp) I have: public class PersonValidator implements Validator { private PersonDAO d;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.