I have a requirement where I need to remove JNDI lookup code from class and @Autowire the same.
Current class:
public class BankProcessor {
public Collection getAccountInformation(String customerId){
Context context = new InitialContext();
ArrayList accountBeanList = new ArrayList();
CustomerHomeLocal customerHome = (CustomerHomeLocal)context.lookup(CustomerHomeLocal.JNDI_NAME);
CustomerLocal customer = customerHome.findByPrimaryKey(customerId);
Iterator accountIterator = customer.getAccounts().iterator();
}
}
Expected class
public class BankProcessor {
@Autowired
private CustomerHomeLocal customerHome;
public Collection getAccountInformation(String customerId){
ArrayList accountBeanList = new ArrayList();
CustomerLocal customer = customerHome.findByPrimaryKey(customerId);
Iterator accountIterator = customer.getAccounts().iterator();
}
}
I have tried creating codeSnippet from classfactory.Code().createCodeSnippetStatement("CustomerHomeLocal customerHome;")
but not able to add it to CtType
.
Also, I can't use createField()
method as I want to autowire user defined classes and adding user-defined jar,importing classes in my code will be a tidious job.
Any pointer on this would be appreaciated. Thanks!