0

I am using spring batch in that I am passing class name as parameter to reader in that I want set that as my target class name.is there a way that we can get class name from a String.I tried with using reflection in that I am getting that class successfully.but I not able to set as my target type class below is my code.

@Bean(name = "jMongoReader")
@StepScope
public MongoItemReader<Object> jsonDataReader(@Value("#{jobParameters[name]}") String className) {

    try {

        Class cls= Class.forName(className);
        reader.setTemplate(mongoConfig.getMongoTemplate());
        reader.setCollection("employeeInfo");
        reader.setTargetType((Class<? extends className>) className.class);
        reader.setQuery("{}");
        Map<String, Direction> sorts = new HashMap<String, Sort.Direction>(1);
        sorts.put("_id", Sort.Direction.ASC);
        reader.setSort(sorts);
        reader.setFields(keys);
}
1
  • className.class is always String in your case Commented Oct 21, 2015 at 9:57

1 Answer 1

1

You made a mistake on this line :

reader.setTargetType((Class<? extends className>) className.class);

It should be :

reader.setTargetType(cls);
Sign up to request clarification or add additional context in comments.

1 Comment

By the way, you cannot do Class<? extends className> with className a variable. You can do it only if it was a type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.