如何使用java代码代替xml实现SSM
本篇内容介绍了"如何使用java代码代替xml实现SSM"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
SpringBoot推荐开发者使用Java配置来搭建框架,SpringBoot中大量的自动化配置都是通过Java代码配置实现的,而不是XML配置,同理,我们自己也可以使用纯Java来搭建一个SSM环境,即在项目中不存在任何XML配置,包括web.xml
环境要求:
Tomact版本必须在7以上
1.在IDEA中创建一个普通的maven项目

在pom.xml加入项目中需要用到的依赖
4.0.0 com.xiao.ssm SSM-demo 1.0-SNAPSHOT war 8 8 2.2 8888 UTF-8 org.springframework spring-webmvc 5.2.12.RELEASE javax.servlet javax.servlet-api 4.0.1 provided javax.servlet.jsp jsp-api 2.2 org.apache.tomcat.maven tomcat7-maven-plugin ${tomcat.version} ${webserver.port} /${project.artifactId} ${project.build.sourceEncoding} central https://maven.aliyun.com/nexus/content/repositories/central central https://maven.aliyun.com/nexus/content/repositories/central
2.添加Spring配置
创建SpringConfig.java文件
package com.xiao.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;/** * @author xiaoss * @Configuration注解标识该类是一个配置类,作用类似于:applicationContext.xml文件 * @ComponentScan注解标识配置包扫描,useDefaultFilters表示使用默认的过滤器,excludeFilters里面的配置表示除去Controller里面的注解, * 即项目启动时候,spring容器只扫描除了Controller类之外的所有bean * @date 2021年10月29日 16:43 */@Configuration@ComponentScan(basePackages = "com.xiao",useDefaultFilters = true, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})public class SpringConfig {}3.添加SpringMVC配置
创建SpringMVCConfig.java文件
package com.xiao.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;/** * @author xiaoss * @date 2021年10月29日 16:56 */@Configuration//@ComponentScan(basePackages = "com.xiao",useDefaultFilters = true,// excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})@ComponentScan(basePackages = "com.xiao")public class SpringMVCConfig extends WebMvcConfigurationSupport { /** * 配置静态资源过滤,相当于 > * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/js/**").addResourceLocations("classpath:/"); } /** * 配置视图解析器 * @param registry */ @Override protected void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/jsp/",".jsp"); } /** * 路径映射 * @param registry */ @Override protected void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/hello3").setViewName("hello"); } /** * json转换配置 * @param converters */ @Override protected void configureMessageConverters(List> converters) { FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter(); converter.setDefaultCharset(Charset.forName("UTF-8")); FastJsonConfig fastJsonConfig=new FastJsonConfig(); fastJsonConfig.setCharset(Charset.forName("UTF-8")); converter.setFastJsonConfig(fastJsonConfig); converters.add(converter); }} 4.配置web.xml
创建WebInit.java文件
package com.xiao.config;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;/** * @author xiaoss * @date 2021年10月29日 17:13 */public class WebInit implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //首先来加载SpringMVC的配置文件 AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext(); ctx.register(SpringMVCConfig.class); //添加DispatcherServlet ServletRegistration.Dynamic springmvc=servletContext.addServlet("springmvc",new DispatcherServlet(ctx)); //给DispatcherServlet添加路径映射 springmvc.addMapping("/"); //给DispatcherServlet添加启动时机 springmvc.setLoadOnStartup(1); }}WebInit的作用类似于web.xml,这个类需要实现WebApplicationInitializer接口,并实现其方法,当项目启动时,onStartup方法会被自动执行,我们可以在这里进行项目初始化操作,如:加载SpringMVC容器,添加过滤器,添加Listener,添加Servlet等
注:
由于在onStartup里面只加载了springmvc配置,没有加载spring容器,如果要加载Spring容器
方案一:
修改springmvc配置,在配置的包扫描中也去扫描@Configuration注解
推荐 方案二:
去掉springConfig文件,讲所有的配置都放到springmvc里面
5.测试
5.1创建HelloController类
package com.xiao.control;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @author xiaoss * @date 2021年10月29日 17:00 */@RestControllerpublic class HelloController { @GetMapping("/hello") public String hello(){ return "hello"; }}运行结果:

5.2创建HelloController2类
package com.xiao.control;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;/** * @author xiaoss * @date 2021年10月29日 22:17 */@Controllerpublic class HelloController2 { @GetMapping("hello2") public String hello2(){ return "hello"; }}运行结果:
5.3路径映射
6.JSON配置
SpringMVC可以接收json参数,也可以返回json参数,这一切依赖于HttpMessageConverter
HttpMessageConverter可以将一个json字符串转为对象,也可以将一个对象转为json字符串,实际上它的底层还是依赖具体的json库
SpringMVC中默认提供了Jackson和gson的HttpMessageConverter,分别是:
MappingJackson2HttpMessageConverterGsonHttpMessageConverter
"如何使用java代码代替xml实现SSM"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!