- 使用Maven创建工程并配置依赖项
首先创建一个Maven Project:
然后选择创建Maven 的webapp实例,当然也可以通过命令行方式创建Maven webapp的项目再转化并导入到MyEclipse中。
在pom.xml中需要对于Spring依赖项进行配置:
<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>wx.spring</groupId> <artifactId>wx.spring.helloworld</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>wx.spring.helloworld Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>wx.spring.helloworld</finalName> </build> <properties> <spring.version>4.0.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>2.框架配置(Spring MVC)
在本示例代码中,对于Spring MVC架构采取xml配置的方式,而对于IOC操作,采取Java Based Annotated方式。
l web.xml
web.xml是整个项目的总体配置文件,在该文件中需要制定项目Servlet的匹配方式以及整个项目的上下文的配置文件。在该web.xml文件中配置了一个Servlet,该Servlet是Spring MVC所需要的。而如果需要在Struts等其他框架中使用Spring,就需要声明一个listener。换言之,dispatcher-servlet与applicationContext是应用于不同场景的相同的配置文件。
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
2 dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="wx.spring.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3 applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="wx.spring.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3.代码
1 HelloWorldController
package wx.spring.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello(
@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "helloworld";
}
}
2 /WEB-INF/views/helloworld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring4 MVC -HelloWorld</title>
</head>
<body>
<h1>Hello : ${name}</h1>
</body>
</html>
3 MessagePrinter.java:服务接口类,具体的功能实现依赖于MessageService接口。
该类中的MessageService成员,会在运行时由Spring自动注入,而不需要自身初始化。
package wx.spring.helloworld.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
private MessageService service;
@Autowired
public void setMessageService(MessageService service) {
this.service = service;
}
public void printMessage() {
System.out.println(this.service.getMessage());
}
}
4 MessageService:接口类
package wx.spring.helloworld.bean;
public interface MessageService {
String getMessage();
}
5 MyConfiguration:配置类
package wx.spring.helloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import wx.spring.helloworld.bean.MessagePrinter;
import wx.spring.helloworld.bean.MessageService;
@Configuration
@ComponentScan(value="wx.spring.helloworld.test")
public class MyConfiguration {
@Bean
MessageService getMessageService() {
return new MessageService() {
public String getMessage() {
return "Hello World!IOC&DI";
}
};
}
@Bean
MessagePrinter getMessagePrinter(){
return new MessagePrinter();
}
}
6 Application
package wx.spring.helloworld.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import wx.spring.helloworld.bean.MessagePrinter;
import wx.spring.helloworld.bean.MessageService;
import wx.spring.helloworld.config.MyConfiguration;
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfiguration.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
5.运行
首先运行mvn package命令将整个项目打包,然后右击选择在Tomcat中运行。最好设置下文件编译的输出:
项目直接运行时,显示的是MVC的结果:
直接运行Main Application:
Spring FrameWork4(MVC + IOC)快速入门实例