码迷,mamicode.com
首页 > 编程语言 > 详细

intellij idea通过maven创建spring项目

时间:2016-03-16 22:29:29      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

1,创建maven项目,选择maven-archetype-webapp作为archetype,这样在项目结构里会自动生成web项目所需的一些文件和结构。

技术分享技术分享

2,在pom.xml文件中加入spring的依赖包,根据自己需求加入所需的依赖包,如spring-web,spring-context-support, spring-webmvc等

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.5.RELEASE</version>
  </dependency>

 

3,加入了spring所需的依赖包之后,由于web的入口先读取web.xml的配置,所以需要在web.xml文件中配置servlet的拦截器处理,如下所示:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         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>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

4,配置完成后,所有发起的url请求都会先经过spring的拦截器进行处理,DispatcherServlet会根据web.xml定义的sevlet-name在同级搜索对应的spring配置文件:spring-servlet.xml,这个配置文件对所有url请求生效,spring-servlet.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
    <context:component-scan base-package="com.harlen"></context:component-scan>
    <mvc:annotation-driven/>
</beans>

 

注:spring配置文件的命名空间是一个参考xml节点命名规范的来源,因此必须要声明命名空间之后对应的配置节点才会生效。这里的配置是通知spring基于com.harlen的package进行扫描,该声明在spring创建IOC容器时需要用到,任何在这个package内出现的类,通过注解声明的,都会被spring当做bean组件添加到IOC容器提供服务。

5,在src/main下创建java目录,并创建package为com.harlen,然后打开IDE的module设置(快捷键是command+向下箭头),将java声明为Sources module,test声明为Tests。

技术分享

6,

 

intellij idea通过maven创建spring项目

标签:

原文地址:http://www.cnblogs.com/harlenzhang/p/5285329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!