第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

哆啦的時光機 2021-11-17 17:31:17
我最近配置了基于 spring 的 maven 項目,我只想用 java 替換我所有的 XML(POM 除外)。我瀏覽了很多關于此的文章和文檔,但是,我在這里的原因是,我有疑問,我認為這些疑問可以由你們解決。眾所周知,每個動態(tài) Web 項目都有一個 XML,它web.xml在沒有框架的情況下被調(diào)用。現(xiàn)在如果我們集成了一些框架,比如Struts、Spring、ORM等,這些也是需要配置的,所以我們再寫一個XML配置文件。我配置了 spring 項目,所以我有一個部署描述符、應用程序上下文和調(diào)度程序 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>
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經(jīng)驗 獲得超2個贊

如果我理解它,您想擺脫所有XML配置。


然后首先你必須實現(xiàn)WebApplicationInitializer哪個替換web.xml配置文件。你可以這樣做:


public class CustomWebAppInitializer implements WebApplicationInitializer {

    @Override

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

        rootContext.register(RootConfig.class);


        ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootContext);

        servletContext.addListener(contextLoaderListener);


        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();

        webContext.register(MvcConfig.class);


        DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);

        dispatcher.setLoadOnStartup(1);

        dispatcher.addMapping("/");

    }

}

另一個步驟是為替換app-ctx.xml 的根上下文實現(xiàn) Spring 配置:


@Configuration

@EnableWebMvc

@ComponentScan({"com.mzk.mavenproject1.service", "com.mzk.mavenproject1.model"})

public class RootConfig {

// ... provide another custom beans when needed

}

最后一步是為 MVC 實現(xiàn)配置,替換dispatcher-servlet.xml:


@Configuration

@EnableWebMvc

@ComponentScan("com.mzk.mavenproject1.controller")

public class MvcConfig extends WebMvcConfigurerAdapter {

    @Bean

    ViewResolver internalViewResolver() {

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/views/");

        resolver.setSuffix(".jsp");

        return resolver;

    }


    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

    }


// ... provide another custom beans when needed

}

關于您關于課程數(shù)量的問題 - 是的,您只能通過兩個課程來完成:CustomWebAppInitializer并且MvcConfig所有內(nèi)容都只有一個上下文。


CustomWebAppInitializer.onStartup() 方法體將如下所示:


    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();

    webContext.register(MvcConfig.class);


    ContextLoaderListener contextLoaderListener = new ContextLoaderListener(webContext);

    servletContext.addListener(contextLoaderListener);


    DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);

    dispatcher.setLoadOnStartup(1);

    dispatcher.addMapping("/");


查看完整回答
反對 回復 2021-11-17
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號