當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot-基础(servlet3+springMVC去除所有的xml)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot-基础(servlet3+springMVC去除所有的xml)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在一些基于spring/spring mvc的java web項目中,總是會有一些xml配置文件,如web.xml、applicationContext.xml等,本文的目標是消除這些xml文件,使用代碼和注解替代。由于本文是基于servlet3,所以首先采用的servlet3的容器,例如tomcat7以上版本、jetty8及以上版本。
消除web.xml
下面是一個典型的web.xml,包涵spring/spring mvc的配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> </web-app>下一步去除web.xml文件,使用java代碼替代下。
Spring MVC提供了一個接口WebApplicationInitializer,用于替代web.xml文件。實現這個借口的類會在Servlet容器啟動的時候自動加載并運行。
將以上代碼替換為Java代碼:
此時便可以刪除web.xml。
去除spring mvc的配置文件dispatcher-servlet.xml
一個典型的spring mvc配置文件如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><mvc:annotation-driven/><context:component-scan base-package="org.wuqinghua.web"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean> </beans>Spring提供了@Configuration注解替代xml配置文件,@Bean注解可以替代xml中的來創建bean。將以上xml配置文件轉換為Java代碼。
@Configuration @EnableWebMvc //開啟支持mvc @ComponentScan(basePackages = "org.wuqinghua.web") public class SpringMVCConfig {@Beanpublic InternalResourceViewResolver internalResourceViewResolver() {InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();viewResolver.setPrefix("/WEB-INF/views/");viewResolver.setSuffix(".jsp");return viewResolver;} }去除spring配置文件applicationContext.xml
spring配置文件中的配置比較多,根據不同的項目還有不同的配置,一下提供了一個簡單的配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="org.wuqinghua"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"></context:exclude-filter></context:component-scan><context:property-placeholder location="classpath:dataSource.properties"/><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean> </beans>其中的配置文件dataSource.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/taotao?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root將以上代碼轉換為java代碼為
@Configuration @ComponentScan(basePackages = "org.wuqinghua", excludeFilters = {@ComponentScan.Filter(value =Controller.class)}) public class SpringConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}/*** 必須添加static** @return*/@Beanpublic static PropertyPlaceholderConfigurer loadProperties() {PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();ClassPathResource resource = new ClassPathResource("dataSource.properties");placeholderConfigurer.setLocation(resource);return placeholderConfigurer;} }修改MyWebAppInitializer.java
public class MyWebInitializer implements WebApplicationInitializer {/*** Servlet容器啟動時會自動運行該方法** @param servletContext* @throws ServletException*/@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();rootContext.register(SpringConfig.class);servletContext.addListener(new ContextLoaderListener(rootContext));AnnotationConfigWebApplicationContext webContext = newAnnotationConfigWebApplicationContext();webContext.register(SpringMVCConfig.class);ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(webContext));registration.setLoadOnStartup(1);registration.addMapping("/");} }?
總結
以上是生活随笔為你收集整理的SpringBoot-基础(servlet3+springMVC去除所有的xml)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光谱感应式水处理器/广谱感应水处理器
- 下一篇: 怎么使用firebird maestro