使用注解配置ssm开发环境,更容易理解spring原理实现及加载流程。以下是使用注解配置ssm开发环境步骤:

一、好比建房子先打地基,搭建ssm环境需要先配置最基本的内容:

package myproject1.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.MultipartConfigElement;
//AbstractAnnotationConfigDispatcherServletInitializer 里的方法是配置webapp的api,因此需要继承该类配置自己的ssm开发环境
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {//配置IoC容器上下文的方法@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class<?>[]{RootConfig.class};}//配置DispatcherServlet上下文的方法@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class<?>[]{WebConfig.class};}//配置DispatcherServlet的拦截内容@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}//配置上传文件@Overrideprotected void customizeRegistration(javax.servlet.ServletRegistration.Dynamic registration) {String filepath = "d:/mvc/uploads";long singleMax = (long) (5*Math.pow(2,20));long totalMax =(long)(10* Math.pow(2,20));registration.setMultipartConfig(new MultipartConfigElement(filepath,singleMax,totalMax,0));}
}

二、根据上面的配置,建立RootConfig类:

package myproject1.config;import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;import javax.sql.DataSource;import java.util.Properties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPoolConfig;@Configuration
@ComponentScan(value = "myproject1.*",includeFilters = {@Filter(type = FilterType.ANNOTATION,value = Service.class)})
@EnableTransactionManagement
public class RootConfig implements TransactionManagementConfigurer {private DataSource dataSource=null;//连接池配置@Bean(name="dataSource")public DataSource initDataSource(){if(dataSource!=null){return dataSource;}Properties property = new Properties();property.setProperty("driverClassName","com.mysql.jdbc.Driver");property.setProperty("url","jdbc:mysql://localhost:3306/mydata");property.setProperty("username","root");property.setProperty("password","123456");property.setProperty("maxActive","200");property.setProperty("maxIdle","20");property.setProperty("maxWait","30000");try{dataSource = BasicDataSourceFactory.createDataSource(property);}catch (Exception e){e.printStackTrace();}return  dataSource;}//连接工厂配置,管理数据库连接@Bean(name="sqlSessionFactory")public SqlSessionFactoryBean initSqlSessionFactory(){SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();sqlSessionFactory.setDataSource(initDataSource());Resource resource = new ClassPathResource("myproject1/mybatis-config.xml");sqlSessionFactory.setConfigLocation(resource);return sqlSessionFactory;}//扫描器配置@Beanpublic MapperScannerConfigurer initMapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("myproject1.*");msc.setSqlSessionFactoryBeanName("sqlSessionFactory");msc.setAnnotationClass(Repository.class);return msc;}//redis 配置@Bean(name = "redisTemplate")public RedisTemplate initRedisTemplate(){JedisPoolConfig poolConfig =new JedisPoolConfig();poolConfig.setMaxIdle(50);poolConfig.setMaxTotal(100);poolConfig.setMaxWaitMillis(20000);JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig);connectionFactory.setHostName("localhost");connectionFactory.setPort(6379);connectionFactory.afterPropertiesSet();RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();RedisSerializer stringRedisSerializer = new StringRedisSerializer();RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(connectionFactory);redisTemplate.setDefaultSerializer(stringRedisSerializer);redisTemplate.setKeySerializer(stringRedisSerializer);redisTemplate.setHashValueSerializer(stringRedisSerializer);redisTemplate.setHashValueSerializer(stringRedisSerializer);return redisTemplate;}//配置事务管理器@Override@Bean(name="annotationDrivenTransactionManager")public PlatformTransactionManager annotationDrivenTransactionManager() {DataSourceTransactionManager transactionManager= new DataSourceTransactionManager();transactionManager.setDataSource(initDataSource());return transactionManager;}
}

三、建立WebConfig类

package myproject1.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;@Configuration  //标记这个配置类
@ComponentScan(value = "myproject1.*",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
@EnableWebMvc
@EnableAsync
public class WebConfig extends AsyncConfigurerSupport {  //AsyncConfigurerSupport 是为了开启新线程才继承的,不用不需要//视图解析器配置@Bean(name = "internalResourceViewResolver")public ViewResolver initViewResolver(){InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();viewResolver.setPrefix("WEB-INF/jsp/");viewResolver.setSuffix(".jsp");return viewResolver;}//数据转换器配置,例如把数据转化为json数据返回用户@Bean(name = "requestMapperingHandlerAdapter")public HandlerAdapter initRequestMappingHandleAdapter(){RequestMappingHandlerAdapter rmhd = new RequestMappingHandlerAdapter();MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();MediaType mediaType = MediaType.APPLICATION_JSON;List<MediaType> mediaTypes = new ArrayList<>();mediaTypes.add(mediaType);jsonConverter.setSupportedMediaTypes(mediaTypes);rmhd.getMessageConverters().add(jsonConverter);return rmhd;}public Executor getAsyncExecutor(){ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();taskExecutor.setCorePoolSize(5);taskExecutor.setMaxPoolSize(10);taskExecutor.setQueueCapacity(200);taskExecutor.initialize();return taskExecutor;}
}

四、mybatis-base.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><settings><setting name="lazyLoadingEnabled" value ="true"/><setting name="aggressiveLazyLoading" value ="false"/></settings><mappers><mapper resource="myproject1/mapper/RedPacket.xml"/><mapper resource="myproject1/mapper/UserRedPacket.xml"/></mappers></configuration>

spring基础--注解配置ssm开发环境相关推荐

  1. 【Spring 基础注解】对象创建相关注解、注入相关注解、注解扫描详解

    Spring 基础注解(2.x) 注解基础概念 注解的作用 Spring 注解的发展历程 对象创建相关注解 @Component @Repository.@Service.@Contoller @Sc ...

  2. ssm如何支持热部署_IntelliJ IDEA基于SpringBoot如何搭建SSM开发环境

    之前给大家在博文中讲过如何通过eclipse快速搭建SSM开发环境,但相对而言还是有些麻烦的,今天小编给大家介绍下如何使用IntelliJ IDEA基于SpringBoot来更快速地搭建SSM开发环境 ...

  3. (十一)Spring 基础注解(对象创建相关注解、注入相关注解)

    注解编程 目录 注解基础概念 注解的作用 Spring 注解的发展历程 Spring 基础注解(Spring 2.x) 对象创建相关注解 @Component @Repository.@Service ...

  4. 阿里云ACP企业级互联网架构ACP实验之本地配置EDAS开发环境

    精选30+云产品,助力企业轻松上云!>>> 实验概述 企业级分布式应用服务(Enterprise Distributed Application Service, 简称 EDAS)是 ...

  5. 配置HADOOP开发环境

    考虑到Windows平台尽管界面友好,但Hadoop环境配置较"怪异",需借助cygwin,这个过程并不优雅.正好我手上另有一套ubuntu环境,用着也很顺手,就在ubuntu中安 ...

  6. vscode中装js解释器_h5学习记录(1)--vscode配置js开发环境

    文笔不是很好,第一次写东西,主要为了记录h5的学习过程.今天记录的是vscode配置js开发环境. 什么是VSCode Visual Studio Code (简称VS Code/VSC) 是一款于2 ...

  7. 在Visual Studio Code配置GoLang开发环境

    在Visual Studio Code配置GoLang开发环境 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Visu ...

  8. [转]Aptana Studio 3配置Python开发环境图文教程

    转载URL:http://www.cr173.com/html/49260_1.html 一.安装Aptana Studio 3 安装完运行时建议将相关默认工作目录设定在英文的某个目录下.避免可能出现 ...

  9. eclipse配置python开发环境_Eclipse中配置python开发环境详解

    Eclipse中配置python开发环境详解 1.下载python安装包.python-2.6.6.msi.并安装. 默认python会安装在C:\Python26下,查看环境变量,如果没有在path ...

  10. Python办公自动化 2.1开发环境搭建:PyCharm社区版配置Anaconda开发环境

    课程大纲 第二章 Python10分钟入门 [2.1]:PyCharm社区版配置Anaconda开发环境 [2.2]:Python基础知识及正则表达式入门 第三章 Python操作Excel [3.1 ...

最新文章

  1. pandas使用read_csv函数读取文件最后N行数据并保留表头、pandas使用read_csv函数读取网络url链接数据
  2. Linux下 制作本地yum安装源 openssl离线安装 gcc-c++离线安装
  3. python3.6.0安装教程-Python 3.6.0下载及安装教程
  4. 基于OHCI的USB主机 —— 寄存器(初始化)
  5. 饼图大小调整_Excel做的双层饼图,太漂亮了
  6. Xamarin 跨移动端开发系列(01) -- 搭建环境、编译、调试、部署、运行
  7. 程序设计 关键字解释
  8. java 修改源码_再谈给应用程序diy启动画面和java源代码补丁修改
  9. JDK各个版本的新特性jdk1.5-jdk8
  10. 32bit还是64bit
  11. 微信小程序支付封装-复制即用
  12. python基于大数据的招聘信息实时数据分析系统的设计与实现
  13. 做web网站开发的流程、步骤
  14. 代理IP是什么意思?浏览器代理和代理服务器是什么(小白必看,看了必会,不看血亏)
  15. 《万历十五年》的读后感作文4000字
  16. 二维码解码芯片最新三款的二维码芯片MCU不同之处
  17. 定制自己的火狐搜索插件 searchplugins
  18. Lumerical Python API学习笔记(二)
  19. halcon 区域分割 check_difference分割
  20. 字符串常见方法总结:方法的作用、参数、返回值(构造方法可省略)1. 构造方法2. 静态方法3. 其它方法

热门文章

  1. 郝斌JAVASE大纲
  2. c语言单片机编程教学大纲,单片机应用技术[C语言]教学大纲.doc
  3. pypyodbc 连接Access数据库常见报错整理
  4. 基于网络安全相关的开源项目技术预研分析报告
  5. 无监督学习之稀疏编码,自编码
  6. 软工文档-操作手册和用户手册的区别
  7. php第一季视频教程 李,李炎恢老师PHP系列课程第一季基础视频教程_PHP教程
  8. 厦门大学邢兆雨:从统计专业到王亚南经济研究院博士生!
  9. 【20保研】厦门大学软件学院暑期夏令营招生简章
  10. midi是计算机合成音乐文件,多媒体音频详解.ppt