纯java配置SpringMVC
一般情况下,我们会在web.xml下配置好Spring和SpringMVC,并指定好它们的配置文件
是最常用的也是最方便的方法
例如:
web.xml <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param> <!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
今天介绍的是纯java配置,不基于xml,
web.xml中一句代码都不用写。
文件目录:


1.Spring容器的配置文件
package com.mvc.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration //标识是配置文件
@ComponentScan("com.mvc.server") //指定自动扫描的包
public class RootConfig { //在这里可以配置任何的bean,是基于Spring容器的 }
2.SpringMVC容器的配置文件
package com.mvc.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration
@EnableWebMvc //表示这个类是SpringMVC的配置文件
@ComponentScan("com.mvc.action")//注意扫描的包
public class WebConfig extends WebMvcConfigurerAdapter{ @Bean
public ViewResolver viewResolver(){
//配置视图解析器
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver; } @Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer){
//配置静态资源的处理
configurer.enable(); } }
3.配置自定义的DispatcherServlet,在tomcat启动的时候,会找到这个类,并自动加载它,所以web.xml中不用再写任何有关Spring配置的代码
package com.mvc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyDispatcherServlet
extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};//加载Spring的配置类
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};加载SpringMVC的配置类
} @Override
protected String[] getServletMappings() {
return new String[]{"/"}; //映射路径
} }
以上,Spring和SpringMVC的配置文件就写好了
4.下面编写控制器,这里的代码就是平时写的类型
package com.mvc.action; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.mvc.server.PersonServer; @Controller
public class MyController { @Autowired
private PersonServer p;
public MyController(){ System.out.println("MyController...");
} @RequestMapping("test")
public String test(Map map){ map.put("p", p.getPerson()); System.out.println("test..."); return "home";
}
}
5.编写服务层
package com.mvc.server; import org.springframework.stereotype.Service; import com.mvc.entity.Person; @Service
public class PersonServer {
public PersonServer(){
System.out.println("PersonServer.."); } public Person getPerson(){ return new Person("aaa",);
}
}
6.实体类
package com.mvc.entity;
public class Person {
public Person(){}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
配置好后,启动tomcat,就可以正常访问了。
需要注意的是WebConfig.java、RootConfig.java、MyDispatcherServlet.java这三个配置类,其他的没什么区别

纯java配置SpringMVC的更多相关文章
- 使用Java配置SpringMVC
在此之前,一直使用的是XML的方式配置SpringMVC,现在为了适应Servlert3.0以及JavaConfig的Spring配置方式,在这里记录一下使用Java代码配置SpringMVC.首先, ...
- eclipse中纯java配置log4j日志
1.新建java项目log4Test 2.新建目录lib,把log4j-1.2.9.jar包放入lib目录 3.右键工程,选择Properties->Java Build Path->Li ...
- 纯Java配置使用slf4j配置log4j
工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...
- java日志-纯Java配置使用slf4j配置log4j(转)
工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...
- Spring学习日志之纯Java配置的MVC框架搭建
依赖引入 <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifa ...
- spring纯java注解式开发(一)
习惯了用XML文件来配置spring,现在开始尝试使用纯java代码来配置spring. 其实,spring的纯java配置,简单来说就是将bean标签的内容通过注解转换成bean对象的过程,没什么神 ...
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
经过<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理>和<基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - ...
- JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...
- springmvc java配置
配置DispatcherServlet DispatcherServlet的是SpringMVC的核心.在这里请求会第一次接触都框架,它要负责将请求路由到其他的组件之中. 使用Java配置将Dispa ...
随机推荐
- <%@ page contentType="text/html; charset=utf-8" language="java"%>每一个字符的含义
contentType="text/html:网页类型htmlcharset=utf-8"网页编码类型language="java"网页编程语言<% @ ...
- JDK源码分析:hashCode()方法
提问: 1.hashCode()源码是怎么实现的. 2.hashCode()是为了配合基于散列的集合而设计的 3.hash数据结构,如何做到存取的时间复杂度为O(1)的.{函数算>逐个比较} 答 ...
- CSS3与页面布局学习笔记(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...
- 关于在线编辑器的选择:tinymce - nilcms
一开始使用的是百度开发的编辑器:ueditor.使用方便,很容易就部署了.现在发现此编辑器也就做一些安全性的更新,而且对于这个编辑器也越来越不喜欢了. 1.臃肿.[1.4.3.3 PHP 版本].下载 ...
- ASP.NET MVC 简介
1. ASP.NET MVC 是什么? ASP.NET MVC是微软官方提供的以MVC模式为基础的ASP.NET Web应用程序(Web Application)框架,它由Castle的MonoRai ...
- iOS开发---有用的网址(持续更新)
http://ios.jobbole.com/88403/ iOS开发之OCR光学识别储蓄卡以及信用卡 http://ios.jobbole.com/87649/ iOS中常用的第三 ...
- C#读取ini文件的方法
最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...
- 关于json序列化循环引用导致出错
以下是错误信息: Caused by: java.lang.IllegalStateException: circular reference error Offending field: meth ...
- 【代码笔记】iOS-替换电话号码中间4位为-号
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...
- Android Fragment生命周期
Fragment与Activity的生命周期关系: 刚打开Activity:Fragment onAttach > Fragment onCreate > Fragment onCreat ...