How to figure out which class to use in Spring? -
I have some code that parses text data in java objects, many parsers based on the JSN value are available. Like when type = 1, use parser1, use type = 2 parser2 etc.
My code is like the following:
interface pars {data pars (input data); } Class Parser1 implementer Parser {} Class Parser2 Tools Persers {} Switch (Type) {Case 1: Return Pace 1 Paras (data); Case 2: Return Pace 2 Paras (data); Default: return zero; } I do not like the check type manually and manually select parsed. Is spring a way to handle parser with mapping type?
Thank you!
You can not escape the check for mapping, you must tell the spring in the same way or Second, the parser must be selected in each state.
You can apply it very well by using parseffects. The parser method will be found for example:
public class parserfactor {@Autowire @Qualifier ("parser1" Parser parser 1; @ Etowair @ qualifier ("parser 2") parser parser 2; Public static parser picar (string value) {if (value.equals ("x")} {return parser1; } And if (value.equals ("y")) {return parser2; If you want, you can create a list of parsers and configure it with @resource annotations. You can instantiate your parser using SpringConfig XML:
& lt; Bean id = "parse 1" class = "com.mycompany.parsers.Parser1" /> & Lt; Bean id = "parser 2" class = "com.myCompany.parsers.Parser2" />
Comments
Post a Comment