original post «configuring Tomcat and Spring framework»
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/springConfig.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
..
..
</web-app>
and simple spring configuration springConfig.xml <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="simpleBean" class="ru.softlogic.SimpleBean"/>
</beans>
Usage spring bean in servlet:
public class SimpleServlet extends HttpServlet {
@Override
public void init(final ServletConfig config) throws ServletException {
final ServletContext servletContext = config.getServletContext();
final WebApplicationContext webApplicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
final SimpleBean bean = (SimpleBean) webApplicationContext.getBean("simpleBean");
if (bean == null){
throw new ServletException("Couldn't acquire spring bean 'simpleBean'");
}
}
}
read more in spring documentation about IoC and beans http://static.springframework.org/spring/docs/2.5
Комментариев нет:
Отправить комментарий