Spring 3 的基于 Java 的配置,替换 XML

我最近配置了基于 spring 的 maven 项目,我只想用 java 替换我所有的 XML(POM 除外)。我浏览了很多关于此的文章和文档,但是,我在这里的原因是,我有疑问,我认为这些疑问可以由你们解决。


众所周知,每个动态 Web 项目都有一个 XML,它web.xml在没有框架的情况下被调用。


现在如果我们集成了一些框架,比如Struts、Spring、ORM等,这些也是需要配置的,所以我们再写一个XML配置文件。


我配置了 spring 项目,所以我有一个部署描述符、应用程序上下文和调度程序 servlet。


WEB.XML


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

         version="3.1">


    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/app-ctx.xml</param-value>

    </context-param>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

     <servlet>

        <servlet-name>dispatcher</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>2</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>dispatcher</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

    <session-config>

        <session-timeout>

            30

        </session-timeout>

    </session-config>

    <welcome-file-list>

        <welcome-file>redirect.jsp</welcome-file>

    </welcome-file-list>

</web-app>

app-ctx.xml

</beans>


哆啦的时光机
浏览 143回答 1
1回答

慕姐8265434

如果我理解它,您想摆脱所有XML配置。然后首先你必须实现WebApplicationInitializer哪个替换web.xml配置文件。你可以这样做:public class CustomWebAppInitializer implements WebApplicationInitializer {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onStartup(ServletContext servletContext) throws ServletException {&nbsp; &nbsp; &nbsp; &nbsp; AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();&nbsp; &nbsp; &nbsp; &nbsp; rootContext.register(RootConfig.class);&nbsp; &nbsp; &nbsp; &nbsp; ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootContext);&nbsp; &nbsp; &nbsp; &nbsp; servletContext.addListener(contextLoaderListener);&nbsp; &nbsp; &nbsp; &nbsp; AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();&nbsp; &nbsp; &nbsp; &nbsp; webContext.register(MvcConfig.class);&nbsp; &nbsp; &nbsp; &nbsp; DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);&nbsp; &nbsp; &nbsp; &nbsp; ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.setLoadOnStartup(1);&nbsp; &nbsp; &nbsp; &nbsp; dispatcher.addMapping("/");&nbsp; &nbsp; }}另一个步骤是为替换app-ctx.xml 的根上下文实现 Spring 配置:@Configuration@EnableWebMvc@ComponentScan({"com.mzk.mavenproject1.service", "com.mzk.mavenproject1.model"})public class RootConfig {// ... provide another custom beans when needed}最后一步是为 MVC 实现配置,替换dispatcher-servlet.xml:@Configuration@EnableWebMvc@ComponentScan("com.mzk.mavenproject1.controller")public class MvcConfig extends WebMvcConfigurerAdapter {&nbsp; &nbsp; @Bean&nbsp; &nbsp; ViewResolver internalViewResolver() {&nbsp; &nbsp; &nbsp; &nbsp; InternalResourceViewResolver resolver = new InternalResourceViewResolver();&nbsp; &nbsp; &nbsp; &nbsp; resolver.setPrefix("/WEB-INF/views/");&nbsp; &nbsp; &nbsp; &nbsp; resolver.setSuffix(".jsp");&nbsp; &nbsp; &nbsp; &nbsp; return resolver;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void addResourceHandlers(ResourceHandlerRegistry registry) {&nbsp; &nbsp; &nbsp; &nbsp; registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");&nbsp; &nbsp; }// ... provide another custom beans when needed}关于您关于课程数量的问题 - 是的,您只能通过两个课程来完成:CustomWebAppInitializer并且MvcConfig所有内容都只有一个上下文。CustomWebAppInitializer.onStartup() 方法体将如下所示:&nbsp; &nbsp; AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();&nbsp; &nbsp; webContext.register(MvcConfig.class);&nbsp; &nbsp; ContextLoaderListener contextLoaderListener = new ContextLoaderListener(webContext);&nbsp; &nbsp; servletContext.addListener(contextLoaderListener);&nbsp; &nbsp; DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);&nbsp; &nbsp; ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);&nbsp; &nbsp; dispatcher.setLoadOnStartup(1);&nbsp; &nbsp; dispatcher.addMapping("/");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java