简体   繁体   English

我可以从Java中的属性文件中调用方法吗?

[英]Can i Call methods From Properties File in Java

I am reading an Excel file using java, I have written some methods for my business logic and i want to implement using properties File. 我正在使用java读取Excel文件,我已经为我的业务逻辑编写了一些方法,并且我想使用属性File实现。

What i want to Do is : 我想做的是:

I am having 10 Columns and 10 methods, i need to declare this methods in Properties file use from that file. 我有10个列和10个方法,我需要在属性文件中使用该文件声明此方法。

Suppose for column A i want only 2 methods then i can use only those 2 methods, For next column B suppose 10 methods and So on. 假设对于列A,我只想要2种方法,那么我只能使用这2种方法,对于下一列B,假设有10种方法等等。

Can i Do this or is there any other way to implement this. 我可以这样做,还是有其他方法来实现这一点。

Please Suggest me if there is any other way to implement this. 如果有任何其他方法可以实现这一点,请建议我。 Thanks in Advance. 提前致谢。

To prevent misunterstandings: properties files are just text files, so you need to use Java code in order to process them. 为了防止误操作:属性文件只是文本文件,因此您需要使用Java代码来处理它们。 They'll only provide a String->String (key->value) mapping, thus you need to parse and interpret the strings yourself. 它们只提供String-> String(key-> value)映射,因此您需要自己解析和解释字符串。

That being said, here's a suggestion on what you might (want to) do: 话虽这么说,这是一个关于你可能(想)做什么的建议:

Since your question isn't that clear I'll make a couple of assumptions: 既然你的问题不是那么明确,我会做几个假设:

  1. Your properties file contains something like: 您的属性文件包含以下内容:

    column1=method1,method7 列1 =方法1,method7
    column2=method1,method2,method3 列2 =方法1,方法2,方法3
    etc. 等等

  2. The methods are all declared in one class using a common signature 这些方法都使用公共签名在一个类中声明

  3. When parsing your excel file you might want to apply the methods to the columns based on the properties file. 解析excel文件时,您可能希望根据属性文件将方法应用于列。

So, here are some hints on what you could do: 所以,这里有一些关于你可以做什么的提示:

  1. Parse the properties file and create a list of method names per column 解析属性文件并为每列创建方法名称列表
  2. When working on a cell/column get the method names for the respective column 处理单元格/列时,获取相应列的方法名称
  3. Use YourClass.class.getMethod(methodName, parameterTypes) to get the Method instances. 使用YourClass.class.getMethod(methodName, parameterTypes)获取Method实例。
  4. Call Method#invoke(...) to invoke the method. 调用Method#invoke(...)来调用该方法。

Edit: 编辑:

As an alternative to having all methods in one class you could also use the fully qualified class name, eg yourpackage.YourClass#method1 , split at # to get the class name and the method, then use Class.forName(fqcn) to get the class and finally call getMethod(...) on that. 作为在一个类中使用所有方法的替代方法,您还可以使用完全限定的类名,例如yourpackage.YourClass#method1 ,在#拆分以获取类名和方法,然后使用Class.forName(fqcn)来获取class,最后调用getMethod(...)

If the signature differs, you might have to use a more complicated notation and parse the parameter classes as well. 如果签名不同,您可能必须使用更复杂的表示法并解析参数类。

There be parsers ready to use for this, but I don't know any. 有解析器准备用于此,但我不知道。 However, some apache commons projects like El and Configuration might prove useful for this task. 但是,像ElConfiguration这样的一些apache公共项目可能对此任务有用。

It's not at all clear what you are trying to do, but in any case you definitely cannot "call" any Java methods from a .properties file (or call from Java to a method contained in a .properties file). 你完全不知道你想要做什么,但无论如何你绝对不能从.properties文件“调用”任何Java方法(或从Java调用到.properties文件中包含的方法)。 A .properties file is not executable, nor is it treated by any standard Java utility as executable/interpretable code. .properties文件不可执行,任何标准Java实用程序也不将其视为可执行/可解释代码。 In essence, all it contains is data, and data cannot call or be called. 实质上,它包含的只是数据,而数据无法调用或被调用。

What you could do however is roll your own framework for doing this, either by using reflection to map the textual method calls in your input file to actual method calls against one or more Java objects, or more simply (but less flexibly/extensibly) by storing a predetermined set of codes which you map directly to method calls using a switch / if-else block, or perhaps by storing your desired code as JavaScript and using an existing Java-based JavaScript interpreter to execute the code. 然而,你可以做的是通过使用反射来将输入文件中的文本方法调用映射到针对一个或多个Java对象的实际方法调用,或者更简单地(但不那么灵活/可扩展)存储一组预定的代码,使用switch / if-else块直接映射到方法调用,或者将所需的代码存储为JavaScript,并使用现有的基于Java的JavaScript解释器来执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM