Spring MVC完整注解方法設置裝備擺設web項目。本站提示廣大學習愛好者:(Spring MVC完整注解方法設置裝備擺設web項目)文章只能為提供參考,不一定能成為您想要的結果。以下是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封裝的框架:https://github.com/selfly/dexcoder-assistant 。 上面開端詳細的設置裝備擺設:
設置裝備擺設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; } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。