I am working on a Spring-MVC application since some time. Recently I ran into some problems with @Scheduled methods, and I noticed that the whole configuration is getting lo
There are a lot of things that can be improved with your code and configuration. Lets start with your dao, don't store the Session
in a instance variable and I strongly suggest to use constructor injection for the required dependencies. Taking this into account your dao(s) should look something like this.
@Transactional
@Repository
public class CanvasDAOImpl implements CanvasDAO{
private final SessionFactory sessionFactory;
@Autowired
public CanvasDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}
@overrride
public returnType methodName(params..){
Session session = this.sessionFactory.getCurrentSession();
// Do stuff with the session.
}
}
No more setters (especially not for the Session
!) just a plain class. The same applies for the @Service
classes.
@Service
@Transactional
public class CanvasServiceImpl implements CanvasService {
private final CanvasDAO canvasDAO;
public CanvasServiceImpl(CanvasDAO canvasDAO) {
this.canvasDAO=canvasDAO;
}
//methods
}
In your configuration you have explicitly defined all your @Repository
and @Service
beans. You also have a
which already detects all @Component
s. Remove all explicitly declared @Repository
and @Service
beans. This will really clean up your configuration!.
In your hibernate configuration the c3p0
and connection
properties are useless as you are injecting a DataSource
and hibernate is't managing it but Spring is. Remove those lines. Also to cleanup this configuration further instead of specifying each and every class it needs to proces add packagesToScan
so it will automatically detect @Entity
annotated beans.
is already implied by the use of
so you can remove it as it basically duplicates things.
You have explicitly defined a RequestMappingHandlerMapping
which doesn't do anything as there is already one registered by
. It only takes up memory, the MappingJackson2HttpMessageConverter
is registered automatically of Jackson2 is detected on the class path so no need to do that.
Chancing the Locale
doesn't work for all URLs as you forgot to register the interceptor with the
element.
When applying all that advice to your classes and configuration the remaining configuration looks like this.
org.hibernate.dialect.PostgreSQL9Dialect
false
update
However you really should split things up in what is loaded by the ContextLoaderListener
basically everything BUT @Controller
s and what is loaded by the DispatcherServlet
just @Controller
s and web related beans.
So the servlet-context.xml
should look something like this.
Then add what was removed and a
(or modify it) to the root-context.xml
.
org.hibernate.dialect.PostgreSQL9Dialect
false
update
Remove the
from your security-context.xml
.
Finally let the DispatcherServlet
load the servlet-context.xml
instead of the security-context.xml
.
Now as you aren't loading beans twice per context anymore you should have a reduced memory footprint and you shouldn't have 4 scheduled jobs any more. Also your Email
class should, ideally, also be a Spring managed bean and I would suggest using the JavaMailSender
API which simplifies sending emails.
So basically the task ahead for you is to mainly remove things, you will end up with less code and less configuration and still achieve the same.
For more hands-on-advice I'm for hire ;)...