Results 1 to 7 of 7

Thread: Using Abstract Class with out look-up method

  1. #1

    Default Using Abstract Class with out look-up method

    Hi,

    have a small doubt using abstract classes in Spring,

    how can I refer the abstract classes in applicationcontext.xml file with out using look-up method.

    I have created an abstract class which have to be used by many classes,

    and declared beans like this:

    <bean name="revenue" class="controllers.Controller">
    <property name="abstractModel" ref="abstractModelClass"/>
    </bean>

    <bean id="abstractModelClass" class="models.AbstractModel" abstract="true">
    <property name="countryBean" ref="countryBean"/>
    <property name="extChargeSubServiceBean" ref="extChargeSubServiceBean"/>
    <property name="revenueBean" ref="revenueBean"/>
    <property name="extSubscriptionTicketServiceBean" ref="extSubscriptionTicketServiceBean"/>
    <property name="subscriptionBean" ref="subscriptionBean"/>
    </bean>


    I like to use like that, but the IDE is showing error which was highlighted, and saying here u cant use reference.


    can u pls any one tell me how can we implement abstract classes with out using look-up method.

    Thanks in advance,
    Raja.

  2. #2
    Join Date
    Nov 2007
    Posts
    420

    Default

    models.AbstractModel is an abstract class? Where is the implementation of this class? how can spring create an abstract class for you without implementation?

  3. #3

    Default detailed code

    Quote Originally Posted by bdangubic View Post
    models.AbstractModel is an abstract class? Where is the implementation of this class? how can spring create an abstract class for you without implementation?
    Hi,

    thanks for the reply and here I am giving the detailed code, so please take a look and inform me if you have any doubts.


    Abstract class:

    public abstract class AbstractModel {
    private static final Logger LOG = Logger.getLogger(AbstractModel.class);

    private String startDateStr;

    private String endDateStr;
    public abstract Object getView();
    }

    Controller Class:

    public class RevenueController extends AbstractController {

    private AbstractModel abstractModel;

    public AbstractModel getAbstractModel() {
    return abstractModel;
    }

    public void setAbstractModel(AbstractModel abstractModel) {
    this.abstractModel = abstractModel;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    return new ModelAndView("revenue", "revenueView", getAbstractModel().getView());
    }
    }


    Implementation of Abstract Class:

    public class RevenueModel extends RevenueController {

    class InnerRevenueModel extends AbstractModel {
    public Object getView() {
    List<Country> countries = getRevenueBean().billedCountries();
    InnerRevenueModel revenue = new InnerRevenueModel();
    revenue.listSubscriptions();
    revenue.getRevenueFromExternal(countries);
    return revenue;
    }

    }

    }


    and coming to applicationContext.xml

    <bean name="revenue" class="controllers.Controller">
    <property name="abstractModel" ref="abstractModelClass"/>
    </bean>

    <bean id="abstractModelClass" class="AbstractModel" abstract="true">
    <property name="countryBean" ref="countryBean"/>
    <property name="extChargeSubServiceBean" ref="extChargeSubServiceBean"/>
    <property name="revenueBean" ref="revenueBean"/>
    <property name="extSubscriptionTicketServiceBean" ref="extSubscriptionTicketServiceBean"/>
    <property name="subscriptionBean" ref="subscriptionBean"/>
    </bean>


    so, when the applicationcontext runs, it have to refer the url to the corresponding controller and have to do dependency injection.
    When executing the modelandview method it have to call indirectly the getView() method in RevenueModel class.

    so can you please explain me how can we refer the abstract class in applicationcontext.xml file with out using look-up method.


    Thanks in advance,
    Raja.

  4. #4
    Join Date
    Nov 2008
    Location
    Tunisia
    Posts
    67

    Default

    you can not reference an abstract class. You have to make reference to its implementation.

  5. #5

    Default

    can you please elaborate it means by changing the posted applicationcontext.xml file, because I have tried by refering to its implementation, but not worth showing same error.

  6. #6
    Join Date
    Nov 2007
    Posts
    420

    Default

    Quote Originally Posted by raja_jan09 View Post
    can you please elaborate it means by changing the posted applicationcontext.xml file, because I have tried by refering to its implementation, but not worth showing same error.
    you have two concrete classes here that you posted, RevenueController and RevenueModel. you can define only those in your applicationContext.xml. you cannot define a bean that is abstract as you can't create a new instance of that bean and Spring certainly can't either.

  7. #7

    Default

    thanks for all,

    removed abstract class and declared an empty method, so that I can override that method in the sub classes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •