Spring MVC完全注解方式配置web项目
在servlet 3.0 开始web项目可以完全不需要web.xml配置文件了,所以本文的配置只在支持servlet 3.0及以上的web容器中有效
使用的是spring mvc (4.3.2.release) + thymeleaf(3.0.2.release), 持久层使用的 spring的 jdbctemplate, ps:推荐一个很好用的对jdbctemplate封装的框架: 。 下面开始具体的配置:
配置spring mvc dispatcherservlet
dispatcherservlet 是spring mvc的核心, spring 提供了一个快速配置dispatcherservlet的类 abstractannotationconfigdispatcherservletinitializer,具体代码如下:
其中 onstartup() 是 webapplicationinitializer 接口中的方法,用户配置其他的 filter 和 listener
getrootconfigclasses() 获取配置类,我理解的相当于 applicationcontext.xml 创建的上下文
getservletconfigclasses()获取配置类,相当于 mvc-servlet.xml 创建的上下文
此类上不需要任何注解
package com.liulu.bank.config; import org.springframework.web.webapplicationinitializer; import org.springframework.web.filter.characterencodingfilter; import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer; import javax.servlet.filterregistration; import javax.servlet.servletcontext; import javax.servlet.servletexception; import java.nio.charset.standardcharsets; /** * user : liulu * date : 2016-10-7 15:12 */ public class webappinitializer extends abstractannotationconfigdispatcherservletinitializer implements webapplicationinitializer { @override protected class<?>[] getrootconfigclasses() { return new class<?>[]{rootconfig.class}; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[]{webconfig.class}; } /** * 配置dispatcherservlet 匹配的路径 * @return */ @override protected string[] getservletmappings() { return new string[]{"/"}; } /** * 配置其他的 servlet 和 filter * * @param servletcontext * @throws servletexception */ @override public void onstartup(servletcontext servletcontext) throws servletexception { filterregistration.dynamic encodingfilter = servletcontext.addfilter("encodingfilter", characterencodingfilter.class); encodingfilter.setinitparameter("encoding", string.valueof(standardcharsets.utf_8)); encodingfilter.setinitparameter("forceencoding", "true"); encodingfilter.addmappingforurlpatterns(null, false, "/*"); } }
配置 applicationcontext.xml,由rootconfig类实现
package com.liulu.bank.config; import com.mchange.v2.c3p0.combopooleddatasource; import org.springframework.context.annotation.*; import org.springframework.core.env.environment; import org.springframework.jdbc.core.jdbctemplate; import org.springframework.jdbc.datasource.datasourcetransactionmanager; import org.springframework.stereotype.controller; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.annotation.resource; import javax.sql.datasource; import java.beans.propertyvetoexception; /** * user : liulu * date : 2016-10-7 15:36 */ @configuration @propertysource("classpath:config.properties") // 导入属性文件 @enableaspectjautoproxy // 相当于 xml 中的 <aop:aspectj-autoproxy/> @enabletransactionmanagement // 开启注解事务 @componentscan(basepackages = {"com.liulu.lit", "com.liulu.bank"}, excludefilters = @componentscan.filter(classes = controller.class )) public class rootconfig { // 上面导入的属性文件中的属性会 注入到 environment 中 @resource private environment env; /** * 配置数据库连接池 c3p0, * @return * @throws propertyvetoexception */ @bean public datasource datasource() throws propertyvetoexception { combopooleddatasource datasource = new combopooleddatasource(); datasource.setjdbcurl(env.getproperty("db.url")); datasource.setdriverclass(env.getproperty("db.driver")); datasource.setuser(env.getproperty("db.user")); datasource.setpassword(env.getproperty("db.password")); datasource.setminpoolsize(integer.valueof(env.getproperty("pool.minpoolsize"))); datasource.setmaxpoolsize(integer.valueof(env.getproperty("pool.maxpoolsize"))); datasource.setautocommitonclose(false); datasource.setcheckouttimeout(integer.valueof(env.getproperty("pool.checkouttimeout"))); datasource.setacquireretryattempts(2); return datasource; } /** * 配置事物管理器 * @param datasource * @return */ @bean public platformtransactionmanager transactionmanager(datasource datasource) { return new datasourcetransactionmanager(datasource); } @bean public jdbctemplate jdbctemplate (datasource datasource) { return new jdbctemplate(datasource); } }
config.properties 文件在 resources 目录下
#数据库配置 db.url=jdbc:mysql://192.168.182.135:3306/bank db.driver=com.mysql.jdbc.driver db.user=root db.password=123456 #数据库连接池配置 #连接池中保留的最小连接数 pool.minpoolsize=5 #连接池中保留的最大连接数 pool.maxpoolsize=30 #获取连接超时时间 pool.checkouttimeout=1000
配置 servlet.xml, 由webconfig类实现
thymeleaf 模板配置也在下面
package com.liulu.bank.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.stereotype.controller; import org.springframework.web.servlet.viewresolver; import org.springframework.web.servlet.config.annotation.defaultservlethandlerconfigurer; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import org.thymeleaf.templateengine; import org.thymeleaf.spring4.springtemplateengine; import org.thymeleaf.spring4.templateresolver.springresourcetemplateresolver; import org.thymeleaf.spring4.view.thymeleafviewresolver; import org.thymeleaf.templatemode.templatemode; import java.nio.charset.standardcharsets; /** * user : liulu * date : 2016-10-7 15:16 */ @configuration @enablewebmvc // 启用 springmvc ,相当于 xml中的 <mvc:annotation-driven/> @componentscan(basepackages = {"com.liulu.bank.controller", "com.liulu.lit"}, includefilters = @componentscan.filter(classes = controller.class), usedefaultfilters = false) public class webconfig extends webmvcconfigureradapter { /** * 设置由 web容器处理静态资源 ,相当于 xml中的<mvc:default-servlet-handler/> */ @override public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { configurer.enable(); } /** * 下面三个bean 配置 thymeleaf 模板 * @return */ @bean public springresourcetemplateresolver templateresolver() { springresourcetemplateresolver templateresolver = new springresourcetemplateresolver(); templateresolver.setprefix("/web-inf/templates/"); templateresolver.setsuffix(".html"); templateresolver.settemplatemode(templatemode.html); templateresolver.setcharacterencoding(string.valueof(standardcharsets.utf_8)); return templateresolver; } @bean public templateengine templateengine(springresourcetemplateresolver templateresolver) { springtemplateengine templateengine = new springtemplateengine(); templateengine.settemplateresolver(templateresolver); return templateengine; } @bean public viewresolver viewresolver(templateengine templateengine) { thymeleafviewresolver viewresolver = new thymeleafviewresolver(); viewresolver.settemplateengine(templateengine); viewresolver.setcharacterencoding(string.valueof(standardcharsets.utf_8)); return viewresolver; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Docker镜像的基本操作