【问题标题】:what is the bean id for enum class in java spring?java spring中枚举类的bean id是什么?
【发布时间】:2012-01-31 10:14:29
【问题描述】:

这是我将字符串转换为枚举以用于 case 语句的方法...

   public int AnalyseRow(String UserName, String SymbolName, Date TransDate, double OpenPrice, double HighPrice, double LowPriceee, double ClosePrice, int Volume, String Splits)
        {
            System.out.println("analyse row detected ........................... X");

            String SLPoint = ""; 
            String SLType = ""; 
            String TLDPoint  = "";
            String TLUPoint = "";
            double STPoint; 
            double LTPoint;
            double MAPoint;

            boolean bTLUIsHorizontal  = false;
            double dPointDef;
            double dHorizontalPoints; 
            boolean TLDHorizontalIsSet  = false;
            boolean TLUHorizontalIsSet  = false;

            double dEntryPrice = 0; 
            double dStopLossPrice = 0;

            List objOpenLTOrders = null; 

            int intOpenLTOrdersCount = 0 ;

            List objLiveLTOrders = null; 

            int intLiveLTOrdersCount=0;

            List objOpenSTOrders=null;

            int intOpenSTOrdersCount=0;

            List objLiveSTOrders=null;
            int intLiveSTOrdersCount=0;
            List objContractSettings=null; 


            double dVolume = 0;
            double dGrossProfit=0;
            double dNetProfit=0;
            double dBrokerageCharge=0;
            double dSpreadCharge=0;

                        for(int i=0; i<count;i++)
                        {
    //                      'MsgBox(objSettingsServiceDataRow.SettingName & " - " & objSettingsServiceDataRow.SettingValue)
                            String caseOf = (settingsBusinessService.readRow(1).getSETTINGNAME());
    //                      Select Case objSettingsServiceDataRow.SettingName
                            EnumRule enum1 = EnumRule.valueOf("caseOf");


                            switch(enum1)
                            {
                                case caseOf:
                                {
                                    if( (settingsBusinessService.GetAllSettings(UserName, settings)).size() != 0)
                                    {
                                        //if (TransDate > tradingBusinessService.GetMaxStockDataTransDate(UserName, SymbolName).AddDays(Double.valueOf((objSettingsServiceDataRow.SettingValue))))
                                        {
                                            SaveLog(UserName, "WARNING DateTime Difference : Difference exceeded specified amount on trans " + SymbolName + " " + TransDate, Now(), 2);
                                            return 1;
                                            //break;
                                        }


                                    }
                                }


 }

我的枚举类:

package com.ib.client.mts.backend.BusinessService;

public enum EnumRule {

    caseOf, EUR

}

这个类的 bean id 是什么? 我正在尝试属性类型,但它面临的错误是我需要一个我从未使用过的构造函数? 枚举类是否有任何特定的 bean id 类型?

【问题讨论】:

  • 为什么需要一个 spring bean 来进行枚举?
  • 那么方法和Spring依赖注入有什么关系呢?
  • 没有 bean 我得到错误!!
  • 您的包 com.ib.client.mts.backend.BusinessService 应该是 com.ib.client.mts.backend.businessService 以遵循标准命名约定。
  • @Aritra 请发布您遇到的错误。

标签: java spring


【解决方案1】:

编辑:根据How assign bean's property an Enum value in Spring config file?,您可以简单地使用枚举值:

<bean name="yourBean" class="your.pakage">
   <property name="type" value="EUR" />
</bean>

您可以使用静态 valueOf 方法作为工厂方法从 java 枚举创建 Spring bean。

使用您的 EnumRule:

<bean id="yourBean" class="com.ib.client.mts.backend.BusinessService.EnumRule" factory-method="valueOf">
    <constructor-arg>
        <value>EUR</value>
    </constructor-arg>
</bean>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多