親手帶你搭建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);
}
}