还不会Spring框架?亲手带你搭建Spring框架---初学者必看--详解

Spring项目搭建流程

创建一个maven的web项目

在这里插入图片描述
2、创建项目之后,会发生报错,缺少web-inf下的web.xml文件
在这里插入图片描述
3、从其他项目中拷贝下来web.xml文件
在这里插入图片描述
项目不报错拉。

MVC设计模式创建包路径和目录

利用MVC设计模式:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

开始搭建spring框架

配置pom.xml文件:

<!-- 整合spring框架 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.1.3.RELEASE</version>
</dependency>

创建applicationContext.xml文件

在这里插入图片描述

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	


</beans>

测试spring框架

创建User对象类:

配置applicationContext.xml

<bean id="user" class="cn.tedu.pojo.User"></bean>

写测试类

写测试类:
public class TestSpring {

	@Test
	public void test() {
		//加载spring的文件
		ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
		//获取bean对象
		User user = (User)cpx.getBean("user");
		System.out.println(user);
	}
	
}

运行结果

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章