Spring / Java调用变量从一个方法作为变量

时间:2014-08-08 13:40:25

标签: java sql spring jsp web

我有一个变量我正在调用“UtilityStartDate”,让我们说它在Class 1中,它从存储过程中提取值。

第1类

 @Override
    public void populateUtility(Date utilityStartDate, String utilityStatus) {
        if(this.Utility != null) {
            this.Utility.populate(utilityStartDate, utilityStatus);
        }
    }

我想在不同的类(Class 2)中填充它,目前它正在使用常量。 “READ_DASHBOARD_START_DATE”,也用作“dateFrom”。 2级

        @Loggable(value = LogLevel.DEBUG)
    public List <MeterReadChart> populateMeterReadChart(String customerId) {

        Calendar cal = Calendar.getInstance();
        String dateTo = DateUtils.formatDate(cal.getTime());
        **cal.add(Calendar.YEAR, Constant.READ_DASHBOARD_START_DATE);
        String dateFrom = DateUtils.formatDate(cal.getTime());**

        return meterReadingDao.populateMeterReadingDetail(customerId, dateFrom, dateTo);

    }

我是java / spring的新手,不知道如何从Class 1调用它到Class 2中的Method

1 个答案:

答案 0 :(得分:1)

您可以使用Spring IOC将Class1的引用引用到Class2中。

Class Class1 
    {

     // Populated value to be stored
     private Date utilityStartDate;

    // method called by Class2 to get the populated value
     public Date getUtilityStartDate()
    {
        return utilityStartDate;
     }

   // Method to populated. (Your logic)
   @Override
   public void populateUtility(Date utilityStartDate, String utilityStatus) {
    if(this.Utility != null) {
        this.Utility.populate(utilityStartDate, utilityStatus);
       }
    }

  }

    Class Class2
    {
     private Class1 class1;

     public void setClass1(Class1 class1)
     {
        this.class1 = class1;
     }


    @Loggable(value = LogLevel.DEBUG)
    public List <MeterReadChart> populateMeterReadChart(String customerId) {

        Calendar cal = Calendar.getInstance();
        String dateTo = DateUtils.formatDate(cal.getTime());
        **cal.add(Calendar.YEAR, Constant.READ_DASHBOARD_START_DATE);**
        // getting Date from Class1
        String dateFrom = DateUtils.formatDate(class1.getUtilityDate());

            return meterReadingDao.populateMeterReadingDetail(customerId, dateFrom, dateTo);

        }
    }