Maven(5)-依赖关系(实战篇)

[工欲善其事,必先利其器]

上文中,我们简单介绍了依赖关系的基本理论与配置方式。但是由于这个知识点在我们日后的开发过程中会经常使用到,因此,我们在本篇中通过演示实例来说明依赖关系,请各位看官一定跟着步骤,亲自尝试一番。仔细观察通过这种方式对我们程序架构造成的影响。

特别的,这里以一份已经调试完成的工程为例,因此,与前文说的工程命名不一致,敬请谅解。

准备工作:

a.操作系统:win7 x64

b.开发工具:eclipse mars j2ee版本,maven3.0.5,jdk7

c.请读者参照。Maven简明教程(3)---在eclipse中创建工程与基本命令介绍。创建user-parent工程,并配置其pom文件

-------------------------------------------------------------------------------------------------------------------------------------

正文开始:

1.1以maven创建uesr-entity工程,工程类型为quickstart。建立的工程结构如下图所示:

Maven(5)-依赖关系(实战篇)_第1张图片

1.2作为演示,在user-entity工程中只创建一个实体类User.Java,具体内容如下:

[java]  view plain  copy
 
  1. package com.java.shiro.entity;  
  2.   
  3. public class User {  
  4.   
  5.     private Integer id; // 编号  
  6.     private String userName; // 用户名  
  7.     private String password; // 密码  
  8.       
  9.     //由于篇幅关系,set、get方法请读者自行完成  
  10. }  
1.3修改pom文件,具体内容如下:注意:这里存在的继承关系。点击保存之后,请在项目的pom文件上点击右键--->run as--->maven install。将项目安装到的本地仓库

[html]  view plain  copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.     <parent>  
  5.         <groupId>com.java.shirogroupId>  
  6.         <artifactId>user-parentartifactId>  
  7.         <version>0.0.1-SNAPSHOTversion>  
  8.         <relativePath>../user-parent/pom.xmlrelativePath>  
  9.     parent>  
  10.     <artifactId>user-entityartifactId>  
  11.     <packaging>jarpackaging>  
  12.     <properties>  
  13.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  14.     properties>  
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>junitgroupId>  
  18.             <artifactId>junitartifactId>  
  19.             <scope>testscope>  
  20.         dependency>  
  21.     dependencies>  
  22. project>  
2.1以maven创建uesr-dao工程,工程类型为quickstart。具体请参照Maven简明教程(3)---在eclipse中创建工程与基本命令介绍。建立的工程结构如下图所示:

Maven(5)-依赖关系(实战篇)_第2张图片
2.2创建接口UserDao.java,具体内容如下:

[java]  view plain  copy
 
  1. package com.java.shiro.dao;  
  2.   
  3. import com.java.shiro.entity.User;  
  4.   
  5. public interface UserDao {  
  6.     public User login(User user);  
  7. }  

2.3创建接口实现类UserDaoImpl.java,具体内容如下:

[html]  view plain  copy
 
  1. package com.java.shiro.dao.impl;  
  2.   
  3. import org.springframework.stereotype.Repository;  
  4. import com.java.shiro.dao.UserDao;  
  5. import com.java.shiro.entity.User;  
  6.   
  7. @Repository  
  8. public class UserDaoImpl implements UserDao{  
  9. //  作为演示,我们省略数据方面的知识  
  10.     public User login(User user) {  
  11.         System.out.println("查询当前用户");  
  12.         User u = new User(1,"Tom","1234");  
  13.         return u;  
  14.     }  
  15. }  
2.4修改pom文件为如下内容:点击保存之后,请在项目的pom文件上点击右键--->run as--->maven install。将项目安装到的本地仓库

[html]  view plain  copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.   
  5.     <parent>  
  6.         <groupId>com.java.shirogroupId>  
  7.         <artifactId>user-parentartifactId>  
  8.         <version>0.0.1-SNAPSHOTversion>  
  9.         <relativePath>../user-parent/pom.xmlrelativePath>  
  10.     parent>  
  11.   
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  15.     properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>log4jgroupId>  
  20.             <artifactId>log4jartifactId>  
  21.         dependency>  
  22.         <dependency>  
  23.             <groupId>junitgroupId>  
  24.             <artifactId>junitartifactId>  
  25.             <scope>testscope>  
  26.         dependency>  
  27.         <dependency>  
  28.             <groupId>com.java.shirogroupId>  
  29.             <artifactId>user-entityartifactId>  
  30.             <version>0.0.1-SNAPSHOTversion>  
  31.         dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframeworkgroupId>  
  34.             <artifactId>spring-contextartifactId>  
  35.             <version>4.1.7.RELEASEversion>  
  36.         dependency>  
  37.     dependencies>  
  38. project>  
3.1以maven创建user-service工程,工程类型为quickstart具体请参照Maven简明教程(3)---在eclipse中创建工程与基本命令介绍。建立的工程结构如下图所示:

Maven(5)-依赖关系(实战篇)_第3张图片

3.2创建接口UserService.java,具体内容如下:

[java]  view plain  copy
 
  1. package com.java.shiro.service;  
  2.   
  3. import com.java.shiro.entity.User;  
  4.   
  5. public interface UserService {  
  6.     public User login(User user);  
  7. }  
3.3创建接口UserServiceImpl.java,具体内容如下:
[java]  view plain  copy
 
  1. package com.java.shiro.service.impl;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. import com.java.shiro.dao.UserDao;  
  7. import com.java.shiro.entity.User;  
  8. import com.java.shiro.service.UserService;  
  9.   
  10. @Service  
  11. public class UserServiceImpl implements UserService{  
  12.   
  13.     @Autowired  
  14.     private UserDao userDao;  
  15.       
  16.     public User login(User user) {  
  17.         return userDao.login(user);  
  18.     }  
  19. }  

3.4修改pom文件,具体内容如下:点击保存之后,请在项目的pom文件上点击右键--->run as--->maven install。将项目安装到的本地仓库

[html]  view plain  copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   
  5.   <parent>  
  6.     <groupId>com.java.shirogroupId>  
  7.     <artifactId>user-parentartifactId>  
  8.     <version>0.0.1-SNAPSHOTversion>  
  9.     <relativePath>../user-parent/pom.xmlrelativePath>  
  10.   parent>     
  11.     
  12.   <artifactId>user-serviceartifactId>  
  13.   <packaging>jarpackaging>  
  14.   
  15.    
  16.   
  17.   <properties>  
  18.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  19.   properties>  
  20.   
  21.   <dependencies>  
  22.     
  23.     <dependency>  
  24.         <groupId>org.springframeworkgroupId>  
  25.         <artifactId>spring-coreartifactId>  
  26.     dependency>  
  27.     <dependency>  
  28.         <groupId>org.springframeworkgroupId>  
  29.         <artifactId>spring-beansartifactId>  
  30.     dependency>  
  31.     <dependency>  
  32.          <groupId>org.springframeworkgroupId>  
  33.          <artifactId>spring-txartifactId>  
  34.         dependency>  
  35.     <dependency>  
  36.         <groupId>org.springframeworkgroupId>  
  37.         <artifactId>spring-contextartifactId>  
  38.     dependency>  
  39.     <dependency>  
  40.         <groupId>org.springframeworkgroupId>  
  41.         <artifactId>spring-context-supportartifactId>  
  42.     dependency>  
  43.     <dependency>  
  44.         <groupId>org.springframeworkgroupId>  
  45.         <artifactId>spring-webartifactId>  
  46.     dependency>  
  47.       
  48.     <dependency>  
  49.         <groupId>org.springframeworkgroupId>  
  50.         <artifactId>spring-webmvcartifactId>  
  51.     dependency>  
  52.       
  53.     <dependency>  
  54.         <groupId>org.springframeworkgroupId>  
  55.         <artifactId>spring-aopartifactId>  
  56.     dependency>  
  57.       
  58.       
  59.     <dependency>  
  60.         <groupId>org.springframeworkgroupId>  
  61.         <artifactId>spring-aspectsartifactId>  
  62.     dependency>  
  63.     
  64.     <dependency>  
  65.       <groupId>junitgroupId>  
  66.       <artifactId>junitartifactId>  
  67.       <scope>testscope>  
  68.     dependency>  
  69.       
  70.     <dependency>  
  71.         <groupId>com.java.shirogroupId>  
  72.         <artifactId>user-daoartifactId>  
  73.         <version>0.0.1-SNAPSHOTversion>  
  74.     dependency>  
  75.   dependencies>  
  76. project>  


4.1以maven创建user-web工程,工程类型为webapp。具体请参照前文。具体请参照Maven简明教程(3)---在eclipse中创建工程与基本命令介绍。建立的工程结构如下图所示:

Maven(5)-依赖关系(实战篇)_第4张图片

4.2创建UserController.java文件,具体内容如下:

[java]  view plain  copy
 
  1. package com.java.shiro.web.controller;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpSession;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9.   
  10. import com.java.shiro.entity.User;  
  11. import com.java.shiro.service.UserService;  
  12.   
  13. @Controller  
  14. @RequestMapping("/user")  
  15. public class UserController {  
  16.   
  17.     @Autowired  
  18.     private UserService userService;  
  19.       
  20.     /** 
  21.      * 用户登录 
  22.      * @param user 
  23.      * @param request 
  24.      * @return 
  25.      */  
  26.     @RequestMapping("/login")  
  27.     public String login(User user,HttpServletRequest request){  
  28.         User resultUser=userService.login(user);  
  29.         if(resultUser==null){  
  30.             System.out.println("用户名或密码错误!");  
  31.             return "index";  
  32.         }else{  
  33.             HttpSession session=request.getSession();  
  34.             session.setAttribute("currentUser", resultUser);  
  35.             return "redirect:/success.jsp";  
  36.         }  
  37.     }  
  38. }  
4.3创建applicationContext.xml文件,具体内容如下:

[html]  view plain  copy
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  9.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"  
  10.        default-lazy-init="true">  
  11.   
  12.     <description>Spring公共配置description>  
  13.   
  14.       
  15.     <context:component-scan base-package="com.java.shiro">  
  16.         <context:exclude-filter type="annotation"  
  17.                                 expression="org.springframework.stereotype.Controller"/>  
  18.         <context:exclude-filter type="annotation"  
  19.                                 expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  20.     context:component-scan>  
  21. beans>  

4.4创建spring-mvc.xml文件,具体内容如下:

[html]  view plain  copy
 
  1. xml version="1.0" encoding="UTF-8"?>      
  2. <beans xmlns="http://www.springframework.org/schema/beans"      
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  4.     xmlns:p="http://www.springframework.org/schema/p"    
  5.     xmlns:aop="http://www.springframework.org/schema/aop"     
  6.     xmlns:context="http://www.springframework.org/schema/context"    
  7.     xmlns:jee="http://www.springframework.org/schema/jee"    
  8.     xmlns:tx="http://www.springframework.org/schema/tx"    
  9.     xsi:schemaLocation="      
  10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    
  11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd    
  13.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd    
  14.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">      
  15.   
  16.       
  17.     <context:component-scan base-package="com.java.shiro" />  
  18.   
  19.       
  20.     <bean id="viewResolver"  
  21.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  22.         <property name="prefix" value="/" />  
  23.         <property name="suffix" value=".jsp">property>  
  24.     bean>  
  25.   
  26. beans>    
4.5修改web.xml文件,具体内容如下:

[html]  view plain  copy
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jspwelcome-file>  
  8.     welcome-file-list>  
  9.   
  10.       
  11.     <context-param>  
  12.         <param-name>contextConfigLocationparam-name>  
  13.         <param-value>classpath:applicationContext.xmlparam-value>  
  14.     context-param>  
  15.       
  16.     <filter>  
  17.         <filter-name>encodingFilterfilter-name>  
  18.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  19.         <async-supported>trueasync-supported>  
  20.         <init-param>  
  21.             <param-name>encodingparam-name>  
  22.             <param-value>UTF-8param-value>  
  23.         init-param>  
  24.     filter>  
  25.     <filter-mapping>  
  26.         <filter-name>encodingFilterfilter-name>  
  27.         <url-pattern>/*url-pattern>  
  28.     filter-mapping>  
  29.       
  30.     <listener>  
  31.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  32.     listener>  
  33.   
  34.       
  35.     <servlet>  
  36.         <servlet-name>springMVCservlet-name>  
  37.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  38.         <init-param>  
  39.             <param-name>contextConfigLocationparam-name>  
  40.             <param-value>/WEB-INF/spring-mvc.xmlparam-value>  
  41.         init-param>  
  42.         <load-on-startup>1load-on-startup>  
  43.         <async-supported>trueasync-supported>  
  44.     servlet>  
  45.     <servlet-mapping>  
  46.         <servlet-name>springMVCservlet-name>  
  47.         <url-pattern>*.dourl-pattern>  
  48.     servlet-mapping>  
  49. web-app>  

4.5修改index.jsp文件,具体内容如下:

[html]  view plain  copy
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. >  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title heretitle>  
  8. head>  
  9. <body>  
  10. <form action="${pageContext.request.contextPath }/user/login.do" method="post">  
  11.     userName:<input type="text" name="userName" value="${user.userName }"/><br/>  
  12.     password:<input type="password" name="password" value="${user.password }"><br/>  
  13.     <input type="submit" value="login"/><font color="red">${errorMsg }font>  
  14. form>  
  15. body>  
  16. html>  

4.6创建success.jsp文件,具体内容如下:

[html]  view plain  copy
 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. >  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title heretitle>  
  8. head>  
  9. <body>  
  10. 欢迎:${currentUser.userName }  
  11. body>  
  12. html>  
4.7修改pom文件,具体内容如下:

[html]  view plain  copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0modelVersion>  
  4.   
  5.     <parent>  
  6.         <groupId>com.java.shirogroupId>  
  7.         <artifactId>user-parentartifactId>  
  8.         <version>0.0.1-SNAPSHOTversion>  
  9.         <relativePath>../user-parent/pom.xmlrelativePath>  
  10.     parent>  
  11.   
  12.     <artifactId>user-webartifactId>  
  13.     <packaging>warpackaging>  
  14.   
  15.     <dependencies>  
  16.   
  17.           
  18.         <dependency>  
  19.             <groupId>javax.servletgroupId>  
  20.             <artifactId>javax.servlet-apiartifactId>  
  21.         dependency>  
  22.   
  23.         <dependency>  
  24.             <groupId>javax.servlet.jspgroupId>  
  25.             <artifactId>javax.servlet.jsp-apiartifactId>  
  26.         dependency>  
  27.   
  28.           
  29.         <dependency>  
  30.             <groupId>javax.servletgroupId>  
  31.             <artifactId>jstlartifactId>  
  32.         dependency>  
  33.   
  34.   
  35.           
  36.         <dependency>  
  37.             <groupId>org.springframeworkgroupId>  
  38.             <artifactId>spring-coreartifactId>  
  39.         dependency>  
  40.         <dependency>  
  41.             <groupId>org.springframeworkgroupId>  
  42.             <artifactId>spring-beansartifactId>  
  43.         dependency>  
  44.         <dependency>  
  45.             <groupId>org.springframeworkgroupId>  
  46.             <artifactId>spring-txartifactId>  
  47.         dependency>  
  48.         <dependency>  
  49.             <groupId>org.springframeworkgroupId>  
  50.             <artifactId>spring-contextartifactId>  
  51.         dependency>  
  52.         <dependency>  
  53.             <groupId>org.springframeworkgroupId>  
  54.             <artifactId>spring-asmartifactId>  
  55.             <classifier>sourcesclassifier>  
  56.         dependency>  
  57.         <dependency>  
  58.             <groupId>org.springframeworkgroupId>  
  59.             <artifactId>spring-context-supportartifactId>  
  60.         dependency>  
  61.   
  62.         <dependency>  
  63.             <groupId>org.springframeworkgroupId>  
  64.             <artifactId>spring-webartifactId>  
  65.         dependency>  
  66.   
  67.         <dependency>  
  68.             <groupId>org.springframeworkgroupId>  
  69.             <artifactId>spring-webmvcartifactId>  
  70.         dependency>  
  71.   
  72.   
  73.         <dependency>  
  74.             <groupId>com.java.shirogroupId>  
  75.             <artifactId>user-serviceartifactId>  
  76.             <version>0.0.1-SNAPSHOTversion>  
  77.         dependency>  
  78.   
  79.         <dependency>  
  80.             <groupId>junitgroupId>  
  81.             <artifactId>junitartifactId>  
  82.             <scope>testscope>  
  83.         dependency>  
  84.         <dependency>  
  85.             <groupId>org.aspectjgroupId>  
  86.             <artifactId>aspectjrtartifactId>  
  87.             <version>1.8.8version>  
  88.         dependency>  
  89.     dependencies>  
  90.     <build>  
  91.         <finalName>user-webfinalName>  
  92.     build>  
  93. project>  

测试方法:

将user-web项目部署在tomcat上,启动tomcat。在浏览器地址栏中输入http://localhost/user-web/,进入index.jsp页面,这里由于我们没有真正的验证用户的用户名,密码。因此,直接点击登陆按钮即可。紧接着,浏览器会提示我们tom已经成功登陆!

-------------------------------------------------------------------------------------------------------------------------------------

你可能感兴趣的:(Maven)