The application is a standard Java command line app:
Code:
public class SpringExample
{
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("de/znt/springexjdo/applicationContext_JDOInjection.xml");
MyService service = (MyService) ctx.getBean("myService");
service.printData();
}
}
The DAO Interface:
Code:
public interface Dao
{
List retrieveData(Class targetClass);
}
Implementation of the DAO interface:
Code:
public class JDODao implements Dao
{
private PersistenceManagerFactory pmf;
public JDODao(PersistenceManagerFactory pmf)
{
this.pmf = pmf;
}
public List retrieveData(Class targetClass)
{
List resultList = new ArrayList();
Query query = null;
PersistenceManager pm = pmf.getPersistenceManager();
try
{
query = pm.newQuery(Car.class);
Iterator resIt = ((Collection) query.execute()).iterator();
while (resIt.hasNext())
{
resultList.add(resIt.next());
}
} finally
{
if (query != null)
{
query.closeAll();
}
if (pm != null)
{
pm.close();
}
}
return resultList;
}
public void close()
{
}
}
The Service interface:
Code:
public interface MyService
{
void printData();
void computeSomething();
}
And the implementation:
Code:
public class MyServiceImpl implements MyService
{
private Dao dao;
public MyServiceImpl(Dao dao)
{
this.dao = dao;
}
public void printData()
{
Iterator carDataIt = ((List) dao.retrieveData(Car.class)).iterator();
while (carDataIt.hasNext())
{
Car car = (Car) carDataIt.next();
System.out.println(car.getManufacturer() + "/" + car.getType() + "/" + car.getColor());
}
}
public void computeSomething()
{
throw new UnsupportedOperationException();
}
}