[工欲善其事, its prerequisite]
Above, we briefly introduce the basic theory and configuration of dependency relationships. However, since this knowledge point is often used in our future development process, we illustrate the dependencies in this article by demonstrating examples, so please crossing must follow the steps and try it out for yourself. Take a closer look at the impact of this approach on our program architecture.
In particular, here is an example of a project that has been debugged, so it is not consistent with the name of the project mentioned earlier, so please understand.
Preparatory work:
A. Operating system: Win7 x64
B. Development tools: Eclipse Mars EE version, maven3.2.2,jdk7
C. Please refer to the reader. A concise tutorial for MAVEN (3)---Create a project and basic command introduction in Eclipse. Create a user-parent project and configure its pom file
--------------------------------------------------------------------------------------------------------------- ----------------------
Body Start:
1.1 Create the Uesr-entity project with MAVEN, the project type is QuickStart. For details, refer to Maven's Concise tutorial (3)---Create a project and basic command introduction in Eclipse. The engineering structure established is shown in the following diagram:
1.2 As a demonstration, create only one entity class User.java in the User-entity project, as follows:
Package com.java.shiro.entity;
public class User {
private Integer ID;//number
private string userName;//user name
private string password;//password
//because of the space relationship, the set, get method please the reader to do their own
}
1.3 Modify the Pom file as follows: Note: The inheritance relationship exists here. After you click Save, right-click on the project's POM file--->run as--->maven install. The local warehouse where the project is installed
<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/xsd/ Maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <parent> <groupid>com.java.shiro </groupId> <artifactId>user-parent</artifactId> <version>0.0.1-SNAPSHOT</version> & Lt;relativepath>, .... /user-parent/pom.xml</relativepath> </parent> <artifactId>user-entity</artifactId> < Packaging>jar</packaging> <properties> <project.build.sourceencoding>utf-8</ project.build.sourceencoding> </properties> <dependencies> <dependency> <groupid>junit& lt;/groupid> <artifactId>junit</artifactId> <scope>test</scope> </dependency> &L T;/dependencies> </project>
2.1 Create the Uesr-dao project with MAVEN, the project type is QuickStart. For details, refer to Maven's Concise tutorial (3)---Create a project and basic command introduction in Eclipse. The engineering structure established is shown in the following diagram:
2.2 Create the interface Userdao.java, the following details:
Package Com.java.shiro.dao;
Import Com.java.shiro.entity.User;
Public interface Userdao {public
user login (user user);
}
2.3 Create an interface implementation class Userdaoimpl.java, with the following details:
Package Com.java.shiro.dao.impl;
Import org.springframework.stereotype.Repository;
Import Com.java.shiro.dao.UserDao;
Import Com.java.shiro.entity.User;
@Repository public
class Userdaoimpl implements userdao{
// As a demonstration, we omit the knowledge of the data public
User login ( User user) {
System.out.println ("Query Current User");
User U = New User (1, "Tom", "1234");
return u;
}
}
2.4 Modify the Pom file as follows: After clicking Save, right-click on the pom file of the project--->run as--->maven install. The local warehouse where the project is installed
<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/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <parent> <groupId>com.java.shiro</groupId> < Artifactid>user-parent</artifactid> <version>0.0.1-SNAPSHOT</version> <relativepath> ... /user-parent/pom.xml</relativepath> </parent> <properties> <project.build.sourceEncoding> utf-8</project.build.sourceencoding> </properties> <dependencies> <dependency> <groupid >log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> < groupid>junit</groupid> <artifactId>junit</artifactId> <scope>test</scope> </ Dependency> <dependency> <groupid>com.java.shiro</groupId> <artifactId>user-entity</artifactId> <version>0.0.1-SNAPSHOT</version> & lt;/dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring -context</artifactid> <version>4.1.7.RELEASE</version> </dependency> </dependencies
> </project>
3.1 Create the User-service project with MAVEN, the project type is QuickStart. For details, refer to Maven's Concise tutorial (3)---Create a project and basic command introduction in Eclipse. The engineering structure established is shown in the following diagram:
3.2 Create the interface Userservice.java, the following details:
Package com.java.shiro.service;
Import Com.java.shiro.entity.User;
Public interface UserService {public
user login (user user);
}
3.3 Create the interface Userserviceimpl.java, the following details:
Package Com.java.shiro.service.impl;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.java.shiro.dao.UserDao;
Import Com.java.shiro.entity.User;
Import Com.java.shiro.service.UserService;
@Service public
class Userserviceimpl implements userservice{
@Autowired
private Userdao Userdao;
Public user login (user user) {
return userdao.login (user);
}
}
3.4 Modify the Pom file as follows: After clicking Save, right-click on the pom file of the project--->run as--->maven install. The local warehouse where the project is installed
<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/xsd/maven-4.0.0.xsd" > < modelversion>4.0.0</modelversion> <parent> <groupId>com.java.shiro</groupId> <arti Factid>user-parent</artifactid> <version>0.0.1-SNAPSHOT</version> <relativepath> ... /user-parent/pom.xml</relativepath> </parent> <artifactId>user-service</artifactId> < Packaging>jar</packaging> <properties> <project.build.sourceencoding>utf-8</project.buil d.sourceencoding> </properties> <dependencies> <dependency> <GROUPID>ORG.SPRINGF
ramework</groupid> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupid>org .springframework</groupid> <artifactId>spring-tx</artifactId> </dependency> &L T;dependency> <groupId>org.springframework</groupId> <artifactid>spring-context</ artifactid> </dependency> <dependency> <groupId>org.springframework</groupId> < ;artifactid>spring-context-support</artifactid> </dependency> <dependency> <groupid>or g.springframework</groupid> <artifactId>spring-web</artifactId> </dependency> <depend
Ency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-aop</artifactid> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-aspects& lt;/artifactid> </dependency> <dependency> <groupId>junit</groupId> <art ifactid>junit</artifactid> <scope>test</scope> </dependency> <dependency&
Gt <groupId>com.java.shiro</groupId> <artifactId>user-dao</artifactId> <version>0.0.1
-snapshot</version> </dependency> </dependencies> </project>
4.1 Create the User-web project with MAVEN, the project type is WebApp. Please refer to the preceding article for details. For details, refer to Maven's Concise tutorial (3)---Create a project and basic command introduction in Eclipse. The engineering structure established is shown in the following diagram:
4.2 Create the Usercontroller.java file with the following details:
Package Com.java.shiro.web.controller;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Com.java.shiro.entity.User;
Import Com.java.shiro.service.UserService;
@Controller
@RequestMapping ("/user") public
class Usercontroller {
@Autowired
private UserService UserService;
/**
* User Login
* @param user
* @param request
* @return
*
/@RequestMapping ("/login")
Public String Login (user user,httpservletrequest request) {
user resultuser=userservice.login (user);
if (resultuser==null) {
System.out.println ("Username or password is incorrect. ");
Return "index";
} else{
HttpSession session=request.getsession ();
Session.setattribute ("CurrentUser", resultuser);
return "redirect:/success.jsp";}}
}
4.3 Create the Applicationcontext.xml file with the following details:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd "default-lazy-init=" true "> <description>spring public configuration </description> <!- -Use annotation to automatically register beans and ensure that @required, @Autowired properties are injected--<context:component-scan base-package= "Com.java.shiro" > <context:exclude-filter type= "Annotation"
expression= "Org.springframework.stereotype.Controller"/> <context:exclude-filter type= "annotation" expression= "Org.springframework.web.bind.annotation.ControllerAdvice"/> </context:comp Onent-scan> </beans>
4.4 Create the Spring-mvc.xml file with the following details:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xml Ns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" Xmlns:ao p= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" x Mlns:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:s chemalocation= "Http://www.springframework.org/schema/aop HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP -4.0.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd Http://www.sprIngframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!--packages that use annotations, including subsets--&
Gt <context:component-scan base-package= "Com.java.shiro"/> <!--View Resolver--<bean id= "Viewresolver" class=
"Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/"/> <property name= "suffix" value= ". jsp" ></property> </bean> </beans>
4.5 Modify the Web. xml file with the following details:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" 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 "id=" webapp_id "version=" 2.5 "> <welcome-file-list> < Welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--spring configuration file--< Context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: Applicationcontext.xml</param-value> </context-param> <!--coded Filters--<filter> <filter-nam E>encodingfilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <async-supported>true</ async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8< /param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/* </url-pattern> </filter-mapping> <!--Spring Listener--<listener> <LISTENER-CLASS>ORG.SPR Ingframework.web.context.contextloaderlistener</listener-class> </listener> <!--add support for SPRINGMVC-- > <servlet> <servlet-name>springMVC</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </ Init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported > </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern& Gt;*.do</url-pattern> </servlet-mapping> </web-app>
4.5 Modify the Index.jsp file with the following details:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
4.6 Create the Success.jsp file with the following details:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
4.7 Modify the Pom file as follows:
<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> <parent> <groupId>com.java.shiro</groupId> <artifactId> User-parent</artifactid> <version>0.0.1-SNAPSHOT</version> <relativepath> ... /user-parent/pom.xml</relativepath> </parent> <artifactId>user-web</artifactId> < Packaging>war</packaging> <dependencies> <!--add servlet Support--<dependency> <groupi d>javax.servlet</groupid> <artifactId>javax.servlet-api</artifactId> </dependency> < ;d ependency> <groupId>javax.servlet.jsp</groupId> <artifactid>javax.servlet.jsp-api</
Artifactid> </dependency> <!--add JTL Support-<dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> < !--add spring Support-<dependency> <groupId>org.springframework</groupId> <artifactid>spri ng-core</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid&
Gt <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-tx</artifactId> </dependency> < Dependency> <groupId>org.springframework</groupId> <artifactid>spring-context</ artifactid> </dependency> <dependency> <groupId>org.springframework</groupId> <art Ifactid>spring-asm</artifactid> <classifier>sources</classifier> </dependency> < Dependency> <groupId>org.springframework</groupid> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </ dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-web mvc</artifactid> </dependency> <dependency> <groupId>com.java.shiro</groupId> &L T;artifactid>user-service</artifactid> <version>0.0.1-SNAPSHOT</version> </dependency > <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <sc ope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> < artifactid>aspectjrt</artifactid> <version>1.8.8</version> </dependency> </ Dependencies> <build> <finalname>user-web</finalname> </build> </project>
Test method:
Deploy the User-web project on Tomcat and start Tomcat. Enter http://localhost/user-web/in the browser address bar, enter the Index.jsp page, here because we do not really verify the user's username, password. So, just click on the login button. Immediately after that, the browser will prompt us that Tom has successfully landed. --------------------------------------------------------------------------------------------------------------- ----------------------
At this point, the Maven Concise tutorial (5)---the end of the dependency relationship (instance)