Announcement Announcement Module
Collapse
No announcement yet.
Using Abstract Class with out look-up method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    models.AbstractModel is an abstract class? Where is the implementation of this class? how can spring create an abstract class for you without implementation?

    Comment


    • #3
      detailed code

      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.

      Comment


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

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment


            • #7
              thanks for all,

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

              Comment

              Working...
              X