我想通过实现InitializingBean接口来初始化一些数据。
InitDataListener类:
public class InitDataListener implements InitializingBean, ServletContextAware { public void afterPropertiesSet() throws Exception { System.out.println("init:afterPropertiesSet()"); } @Override public void setServletContext(ServletContext servletContext) { System.out.println("init:setServletContext()"); } }
springmvc中以加入配置:
<bean class="cn.edu.svtcc.microcourse.listener.InitDataListener"> </bean>
web.xml配置
<!-- 配置springmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- /配置springmvc -->
启动项目时没有任何反应
InitDataListener 加个@service注释看看
启动时首先加载web.xml,,,web.xml你有没有配置 引用这个类所在的springmvc.xml