I am using JSF + Spring+ Hibernate
protected @Inject ChartOfAccount chartOfAccount;
I basically want to populate chartOfAccount
from the list
for (DistributionEntry de : getDistributionEntries()) {
chartOfAccount.setAccount(de.getAccount());
chartOfAccountList.add(chartOfAccount);
}
for each iteration I want new object of chartOfAccount
otherwise you know list contain same object with latest value.
Solution One: use new keyword :-p
for (DistributionEntry de : getDistributionEntries()) {
ChartOfAccount coa= new ChartOfAccount();
coa.setAccount(de.getAccount());
chartOfAccountList.add(coa);
}
Solution Two : applicationContext.getBean
for (DistributionEntry de : getDistributionEntries()) {
chartOfAccount= applicationContext.getBean(ChartOfAccount.class);
chartOfAccount.setAccount(de.getAccount());
chartOfAccountList.add(chartOfAccount);
}
But I have read the certain articles that to avoid use of applicationContext.getBean
If I avoid to use applicationContext.getBean
, what is the best way to handle this type of situation ? and both behavior will same? (ApplicationContext.getBean vs new keyword )
Note: My Managed bean is @Scope("session") and Model is @Scope(BeanDefinition.SCOPE_PROTOTYPE), so as we all know that for one session it is singleton and prototype for different session.