您的位置:首页 > 编程语言 > Java开发

Spring MVC代码实例系列-07:Spring MVC用JavaConfig代替xml搭建项目

2018-01-20 20:04 651 查看
超级通道 :Spring MVC代码实例系列-绪论

本章主要记录如何通过JavaConfig方式代替xml搭建Spring MVC项目的实现,主要涉及以下知识点:

- @Configuration:声明一个类是配置文件

- @Bean:注册一个bean实例

- @EnableWebMvc:开启注解,等同于mvc:annotation-driven

- @ComponentScan:开启自动装配,等同于 context:component-scan

- WebApplicationInitializer接口:替代web.xml启动web项目的接口

- AbstractAnnotationConfigDispatcherServletInitializer:WebApplicationInitializer的一个实现类,负责配置DispatcherServlet、初始化Spring MVC容器和Spring容器。

0.关于JavaConfig与XML

本文只是对JavaConfig配置方式的一个简单展示。 JavaConfig和XML的配置方式并不互斥,我习惯于JavaConfig和XML混搭的方式进行配置:

- 1.Spring的相关配置通过XML完成

- 2.基础服务如Solr、Redis等配置通过JavaConfig完成

- 3.业务类的bean注册和装配通过注解完成

1.目录结构

本项目参考第一章的 Spring MVC代码实例系列-01:Spring MVC项目简单搭建与Hello Wolrd

src
\---main
\---java
|   \---pers
|       \---configuration
|       |   \---MyWebApplicationInitializer.java
|       |   \---MySpringMVCConfiguration.java
|       \---myspringmvc
|           \---hello
|               \---HelloController.java
\---resources
\---webapp
\---helloworld
|   \---helloworld.jsp
\---WEB-INF
\---index.jsp


可以看到,项目中没有xml文件.

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>pers.hanchao</groupId>
<artifactId>myspringmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>mySpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<final-name>myspringmvc</final-name>
<springframework.version>5.0.0.RELEASE</springframework.version>
<servlet-api.version>3.1.0</servlet-api.version>
<jsp-api.version>2.0</jsp-api.version>
<javax-mail.version>1.5.0-b01</javax-mail.version>
<tomcat-plugin.version>2.2</tomcat-plugin.version>
</properties>
<dependencies>
<!--Cannot find the class file for javax.servlet.ServletContext.-->
<!-- servlet的jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>

<!-- 验证的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!--Maven Tomcat 组件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat-plugin.version}</version>
<configuration>
<path>/</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>myspringmvc</finalName>
</build>
</project>


3.MyWebApplicationInitializer.java

相当于web.xml

package pers.hanchao.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
* <p>实现了WebApplicationInitializer接口,是web.xml的替代方式</p>
* @author hanchao 2018/1/20 19:40
**/
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

/**
* <p>用于获取Spring应用容器的配置文件</p>
* @author hanchao 2018/1/20 19:55
**/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{MySpringMVCConfiguration.class};
}

/**
* <p>获取Spring MVC应用容器</p>
* @author hanchao 2018/1/20 19:55
**/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}

/**
* <p>负责指定需要由DispatcherServlet映射的路径</p>
* @author hanchao 2018/1/20 19:56
**/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}


4.MySpringMVCConfiguration.java

相当于spring-mvc-servlet.xml

package pers.hanchao.configuration;

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.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

//声明这是一个配置文件,类似于applicationcontext.xml
@Configuration
//开启注解,等同于<mvc:annotation-driven/>
@EnableWebMvc
//开启自动装配,等同于 context:component-scan base-package="pers.hanchao.*"
@ComponentScan(basePackages = "pers.hanchao.*")
public class MySpringMVCConfiguration {

/**
* 视图解析器,相当于:
* <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
*      <property name="prefix" value="/"/>
*      <property name="suffix" value=".jsp"/>
* </bean>
*/
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}


5.HelloController.java

package pers.hanchao.myspringmvc.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
* <p>Hello Spring MVC:用JavaConfig代替xml,消除xml配置文件</p>
* @author hanchao 2018/1/20 19:19
**/
@Controller
public class HelloController {

@GetMapping("/helloworld")
public String helloWorld(Model model){
model.addAttribute("helloworld","Hi Spring MVC");
return  "helloworld/helloworld";
}
}


6.helloworld.jsp

<%--
Created by IntelliJ IDEA.
User: hanchao
Date: 2018/1/20
Time: 19:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>${helloworld}</h3>
</body>
</html>


7.index.jsp

<html>
<body>
<h2>Hello World!</h2>
<a href="/helloworld">Hello World!</a>
</body>
</html>


8.result

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息