Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)

网友投稿 312 2023-01-07


Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)

工具篇:Intellij Idea+maven+Spring+SpringMVC

Spring+SpringMVC环境搭建

一、SpringMVC环境搭建

1、创建新项目

(1)、第一步是创建一个由Maven原型的项目,根据图片上的步骤一次选择Maven—–>create from archetype—->maven-archtype-webapp

(2)、第二步是填写GroupId 和ArtifactId

(3)、在位置1处选择我们maven安装的目录,在位置2处选择settings.xml的路径(我只是设置了一次之后就是默认的了)

(4)、设置项目名和项目的位置

到这里项目就创建成功了

2、添加Maven中项目所需要的Jar包

在pom.xml中添加项目SpringMVC所依赖的Jar包

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zbt

enviroment

1.0-SNAPSHOT

war

enviroment Maven Webapp

http://example.com

UTF-8

1.7

1.7

org.springframework

spring-aop

5.0.6.RELEASE

org.springframework

spring-aspects

5.0.6.RELEASE

org.springframework

spring-beans

5.0.6.RELEASE

org.springframework

spring-context

5.0.6.RELEASE

org.springframework

spring-core

5.0.6.RELEASE

org.springframework

spring-expression

5.0.6.RELEASE

org.springframework

spring-jdbc

5.0.6.RELEASE

org.springframework

spring-orm

5.0.6.RELEASE

org.springframework

spring-tx

5.0.6.RELEASE

org.springframework

spring-web

5.0.6.RELEASE

org.springframework

spring-webmvc

5.0.6.RELEASE

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet

jstl

1.2

taglibs

standard

1.1.2

enviroment

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zbt

enviroment

1.0-SNAPSHOT

war

enviroment Maven Webapp

http://example.com

UTF-8

1.7

1.7

org.springframework

spring-aop

5.0.6.RELEASE

org.springframework

spring-aspects

5.0.6.RELEASE

org.springframework

spring-beans

5.0.6.RELEASE

org.springframework

spring-context

5.0.6.RELEASE

org.springframework

spring-core

5.0.6.RELEASE

org.springframework

spring-expression

5.0.6.RELEASE

org.springframework

spring-jdbc

5.0.6.RELEASE

org.springframework

spring-orm

5.0.6.RELEASE

org.springframework

spring-tx

5.0.6.RELEASE

org.springframework

spring-web

5.0.6.RELEASE

org.springframework

spring-webmvc

5.0.6.RELEASE

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet

jstl

1.2

taglibs

standard

1.1.2

enviroment

3、配置web.xml文件

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

index.jsp

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/spring-mvc.xml

1

springMVC

*.do

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

index.jsp

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:/spring-mvc.xml

1

springMVC

*.do

5、配置spring-mvc.xml文件

在resources文件夹下新建一个spring-mvc.xml。spring-mvc.xml文件的功能是什么?

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

6、编写Controller类

package controller;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/student")

public class StudentController {

private static final Log logger = LogFactory.getLog(StudentController.class);

@RequestMapping("/hello")

public String sayHi(){

logger.info("sayHi are called!");

return "hello";

}

}

在WEB-INF/jsp文件夹下创建测试的hello.jsp

启动服务器,在浏览器中输入

http://localhost:8088/student/hello.do

显示结果为

注意事项

(1)、url-pattern最好配置成下面的形式

springMVC

*.do

而不是下面这种形式

springMVC

/*

配置成第二种模式会出现下面的情况

27-Jun-2018 15:04:02.761 警告 [http-nio-8088-exec-8] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'springMVC'

网页会出现如下情况

(2)、我们的spring-mvc.xml是放在resources文件夹下面的,以后就按照这种方式编写(注意这是在Intellij idea中,在Eclipse中我不知道会不是是一样的)

contextConfigLocation

classpath:/spring-mvc.xml

二、Spring+SpringMVC环境搭建

编写StudentService

package com.zbt.service;

import org.springframework.stereotype.Service;

/**

* Created by luckyboy on 2018/7/2.

*/

@Service

public class StudentService {

public String test(){

return "hello";

}

}

创建applicationConfig配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.3.xsd ">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.3.xsd ">

注意Spring这里的包扫描方式为,Spring扫描包的时候会排出@Controller注解,也就是不会扫描com.zbt.controller包

修改spring-mvc.xml中的包扫描方式

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-2.5.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

将StudentService注入到StudentController中

package com.zbt.controller;

import com.zbt.service.StudentService;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

/**

* Created by luckyboy on 2018/6/27.

*/

@Controller

@RequestMapping("/student")

private static final Log logger = LogFactory.getLog(StudentController.class);

//使用Autowired将StudentService注入到StudentController中

@Autowired

private StudentService stuService;

@RequestMapping("/hello.do")

public String sayHi(){

logger.info("sayHi are called!");

return "hello";

}

@RequestMapping("/spring_hello.do")

public String springSayHi(){

logger.info("sayHi are called by springSayHi");

return stuService.test();

}

}

在StudentService类上面添加@Service注解

package com.zbt.service;

import org.springframework.stereotype.Service;

/**

* Created by luckyboy on 2018/7/2.

*/

@Service

public class StudentService {

public String test(){

return "hello";

}

}

测试

启动服务器,在浏览器地址栏输入http://localhost:8088/student/spring_hello.do,响应如下

注意事项

我们在spring-mvc.xmlweZHKcslc中的配置包扫描时,如果包扫描配置如下

出现的错误

org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'stuService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zbt.service.StudentService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostPrhttp://ocessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)

at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1348)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)

at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:672)

at org.springframhttp://ework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:638)

at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:686)

at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:554)

at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:499)

at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:172)

at javax.servlet.GenericServlet.init(GenericServlet.java:158)

at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144)

at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)

at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)

at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4939)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5249)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)

at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)

at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)

at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)

at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)

at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)

at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)

at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)

at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)

at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)

at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)

at sun.rmi.transport.Transport$1.run(Transport.java:200)

at sun.rmi.transport.Transport$1.run(Transport.java:197)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.Transport.serviceCall(Transport.java:196)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at java.lang.Thread.run(Thread.java:748)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zbt.service.StudentService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)

... 67 more

会出现包无法扫描的情况,因此我们在spring-mvc.xml中应该配置成扫描所有的包


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务网关与注册中心(微服务网关与注册中心区别)
下一篇:综调联调接口测试方案(联调测试方案编写)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~