(第02节)集成Sping框架
通过第一节创建好的Web项目,接下来就是集成Spring框架
首先让我们看下创建好的Web项目的基本结构
其中,Java跟test是我自己创的,然后就是一般的webapp文件,和pom配置文件,要在web项目中继承spring框架,就是先从pom入手,在pom文件中添加Spring相关依赖:
<properties> <spring.version>5.0.4.RELEASE</spring.version> </properties> <!--Spring start--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end -->
然后在/src/mian/resources目录下创建applicationContext配置文件,具体代码如下:
最后的com.xjx可改为自己的包名
注:
<context:component-scan base-package="com.xjx"/>是将com.xjx包下的所有JAVA类注册成bean
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.xjx"/> </beans>
接着,在web.xml下添加代码如下:
注:<context-param>是整个项目的全局变量,param-name是参数名,value是值
ContextCleanupListeners
<!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> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class> </listener> </web-app>
好了,配置完成,接下来就可以测试下,当然就是helloword了
我就开始偷懒了,截图启动
我上面的报错可能是因为在配置Maven的时候把自己的JavaHome改成了D:\JDK但是不影响使用,先用着,遇到不可以的再说
成为更好的自己
posted on 2019-03-30 19:53 CodeFish-Xiao 阅读(140) 评论(1) 编辑 收藏 举报