0

The class is as following:

class ReportControllerBase {
    String reportName = "Report";

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    // ...
}

class AnnualReportController extends ReportControllerBase {
    // ...
}

class SkinCareAnnualReprotController extends AnnualReportController {
    String productName;

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }
}

And the XML-based configuration is as following:

<bean id="annualReportController" class="AnnualReportController">
    <property name="reportName" value="Annual Report"/>
</bean>

<bean id="annualSpecialReportController" class="AnnualReportController">
    <property name="reportName" value="Annual Special Report"/>
</bean>

<bean id="skinCareAnnualReprotController" class="SkinCareAnnualReprotController" parent="annualReportController">
    <property name="productName" value="A famous skin care product"/>
</bean>

The Bean annualReportController and annualSpecialReportController is the instance of the same Class. And skinCareAnnualReprotController is inherited from bean annualReportController.

How to implement this configuration in annotation-based configuration in Spring?

1 Answer 1

1
@Configuration
public class ReportConfiguration {
    @Bean public AnnualReportController annualReportController() {
        AnnualReportController annualReportController = new AnnualReportController();
        annualReportController.setReportName("Annual Report");
        return annualReportController;
    }

     @Bean public AnnualReportController annualSpecialReportController() {
        AnnualReportController annualSpecialReportController = new AnnualReportController();
        return withAnnualSpecialReportName(annualSpecialReportController);
    }

     @Bean public SkinCareAnnualReportController skinCareAnnualReportController() {
        SkinCareAnnualReportController skinCareAnnualReportController = new SkinCareAnnualReportController();
        skinCareAnnualReportController.setProductName("A famous skin care product");
        return withAnnualSpecialReportName(skinCareAnnualReportController);
    }

    // in this instance, a helper method is arguably overkill,
    // but I've included it for demonstration
    private <T extends AnnualReportController> T withAnnualSpecialReportName(T report) {
        report.setReportName("Annual Special Report");
        return report;
    }
}

Take a look at the Spring 3.1 documentation for more information.

Sign up to request clarification or add additional context in comments.

8 Comments

You're also going to need to add @Qualifier to @Bean so you can inject the various AnnualReportControllers separately.
@sourcedelica: you shouldn't need to if the field name is the same as the bean name
Right - I guess I was assuming that injection was going to be done using @Autowire, not xml.
Thanks a lot! I still confused on how to implement skinCareAnnualReprotController inherit annualReportController, not annualSpecialReportController.
I've updated to include the other two beans. Also, you have some misspellings of 'report', which I've corrected in my answer ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.