Saturday, 31 August 2013

is it compulsory to make @repository and Interface in spring mvc

is it compulsory to make @repository and Interface in spring mvc

I am using Spring MVC + Hibernate.
@Serice
@Service("PersistenceTemplate")
@Transactional(readOnly = false, rollbackFor = Exception.class)
public class PersistenceTemplate {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
protected Logger logger = Logger.getLogger(PersistenceTemplate.class
.getName());
public PersistenceTemplate() {
super();
}
// save
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal = (Long) session.save(entity);
return getGenVal;
}
}
@Controller
@Resource(name = "PersistenceTemplate")
private PersistenceTemplate pt;
pt.save(entityFoo);
My Project is running well. But is it compulsory to make @repository Class
and Interface for implementation like following, which is alternative to
above
Interface
public interface IOperations{
<T> long save(final T e);
}
@repository
@Repository
public class PersistenceTemplate implements IOperations {
@Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
protected Logger logger = Logger.getLogger(PersistenceTemplate.class
.getName());
public PersistenceTemplate() {
super();
}
// saveE
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal = (Long) session.save(entity);
return getGenVal;
}
Service
@Service("serviceLayer")
public class ServiceLayer {
@Autowired
IOperations op = null;
public ServiceLayer() {
super();
}
@Transactional(readOnly = false, rollbackFor = Exception.class)
public <T> long save(T e) {
op.save(e);
return 0;
}
}
Now I think interface is no matter for spring mvc because it manage proxy
by default if no found!
which implementation will u prefer either with @repository and interface
or without @repository and interface.

No comments:

Post a Comment