I have a Spring project with following classes:
ReportingDAO.java: has method updateData(). (Newly added)
In Reporting.java there is a main(String []args) method from where I have to call updateData() of Reporting.java.
Reporting.java:
@autowired
IPersistenceService persistenceService;
public static void main(String []args)
{
ApplicationContext context = new ClassPathXMLApplicationContext(applicationContext.xml) // context has values populated in it.
persistenceService.updateData(); // I am getting persistenceService as null here.
}
IPersistenceService.java
public void updateData();
@service
PersistenceService.java (implements IPersistenceService)
public void updateData()
{
ReportingDAO reportingDao = new ReportingDAO ();
reportingDao.updateData();
}
The problem is I m not able to call the updateData() method from my main() class method. As I am getting the null for persistenceService.
I am not sure what to be added in applicationContext.xml.
Thanks in advance.
Your problem is that you can't mix and match using the Spring bean factory and calls to new. The moment you instantiate an object it is out of Spring's hands.
Either add the service to your Spring configuration or instantiate the objects you need by calling new.