如何使用Javaconfig代替web.xml配置spring
2017-12-19 15:51
741 查看
我们知道使用spring时候最让人烦的是大量的xml配置文件。在使用spring框架时候在web.xml中配置spring传统配置是:<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置加载任意目录下的任意的xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_*.xml</param-value>
</context-param>
配置springmvc:
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc的配置文件的位置与名字 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
使用javaconfig配置:
第一步:定义一个类实现WebApplicationInitializer接口 实现onStartup方法
编写:
public void onStartup(ServletContext servletContext) throws ServletException {
//配置spring
servletContext.setInitParameter("contextConfigLocation","classpath:applicationContext_1.xml");
servletContext.addListener(new ContextLoaderListener());
//配置springmvc
ServletRegistration.Dynamic springMVC= servletContext.addServlet("spring",new DispatcherServlet());//加载springMVC核心类
springMVC.addMapping("/");
springMVC.setInitParameter("contextConfigLocation","classpath:servlet.xml");//设置springmvc的配置文件位置
完成在这里已经完成了。
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置加载任意目录下的任意的xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_*.xml</param-value>
</context-param>
配置springmvc:
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc的配置文件的位置与名字 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
使用javaconfig配置:
第一步:定义一个类实现WebApplicationInitializer接口 实现onStartup方法
编写:
public void onStartup(ServletContext servletContext) throws ServletException {
//配置spring
servletContext.setInitParameter("contextConfigLocation","classpath:applicationContext_1.xml");
servletContext.addListener(new ContextLoaderListener());
//配置springmvc
ServletRegistration.Dynamic springMVC= servletContext.addServlet("spring",new DispatcherServlet());//加载springMVC核心类
springMVC.addMapping("/");
springMVC.setInitParameter("contextConfigLocation","classpath:servlet.xml");//设置springmvc的配置文件位置
完成在这里已经完成了。
相关文章推荐
- SpringMVC+Spring4+hibernate5使用java类配置spring和springmvc环境(不用配置web.xml)
- spring 在web.xml 里面如何使用多个xml配置文件
- Spring Web工程web.xml零配置即使用Java Config + Annotation
- JAVA_WEB项目之使用Spring的xml配置方式在项目中管理Lucene检索框架
- 译--Spring中 bean定义profiles的使用 javaConfig 和XML配置
- JavaEE 下面使用Spring 时 web.xml 中ContextLoaderListener的作用及配置,多个配置文件时contextConfigLocation的作用
- spring 在web.xml 里面如何使用多个xml配置文件
- Spring web基于javaconfig类配置 的web.XML配置
- 使用javaConfig代替xml配置
- Struts2.3使用Sitemesh如何配置web.xml?
- 如何使用多个Spring的xml配置文件(多模块配置)
- 如何在web.xml配置spring,struts2配置以及过滤器
- 在java程序项目中如何使用xml配置文件存储信息简述
- 在web.xml中通过contextConfigLocation配置spring
- java 使用hibernate访问sqlserver如何配置hibernate.cfg.xml
- java使用hibernate访问mysql 如何配置hibernate.cfg.xml
- 如何使用多个Spring的xml配置文件
- 使用spring web.xml里的配置 - 请求处理器DispatcherServlet
- 如何使用多个Spring的xml配置文件
- 如何使用多个Spring的xml配置文件(多模块配置)