The point of using annotations is to remove the need for xml configuration. If you are using the Spring @Service
and @Repository
annotations then you can remove all the service and dao definitions from the xml posted above and replace them with with one line.
<context:component-scan base-package="x.y.z.service, x.y.z.repository" />
You can then update all your service classes to be like below using the @Autowired
annotation to have Spring inject the relevant DAO:
@Service
@Transactional
public class CanvasServiceImpl implements CanvasService {
@Autowired
private CanvasDAO canvasDAO;
}
You also load the Spring security context multiple times in your web.xml. Once via The ContextLoaderListener and once via the RequestDispatcher configuration:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/appServlet/security-applicationContext.xml</param-value>
</context-param>
<session-config>
<session-timeout>1440</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/security-applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
</web-app>
And then, just to finish things off you also import the web application configuration in the security context each time it is loaded.
<import resource="servlet-context.xml" />
I would normally configure as outlined here. I believe this is considered best practice. Essentially you have the Dispatcher Servlet initialize only the web related stuff. You have the Listener initialize the non-web stuff.
http://simone-folino.blogspot.co.uk/2012/05/dispatcherservlet-vs.html
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…