一、Spring的概述
1、什么是Spring
Spring是一个企业中常用的一站式的解决框架,用于解决web开发当中各个层级之间的问题,说白了就是spring可以支持我们各个层级包括我们的数据展示层,我们的业务层,我们的持久层等都有对应的解决方案。
2、Spring的优点
- 方便解耦,简化开发:Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理。
- AOP编程的支持:Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监管等功能。
- 声明式事务的支持:只要通过配置就可以完成对事务的管理,而无需手动编程。
- 方便程序的测试:Spring对Junit4支持,可以通过注解方便的测试Spring程序。
- 方便集成各种优秀框架:Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架的直接支持。
- 降低JavaEE API的使用难度:Spring对JavaEE开发中一些非常难用的API(JDBC、远程调用等),都提供了封装使这些API应用难度大大降低。
3、Spring的架构体系
Spring的架构体系
- Test:测试模块,spring可以与我们的junit进行整合做测试非常方便
- Core container :核心容器,就是用来装javaBean对象。
- AOP:切面编程
- Aspects:切面编程应用的一个模块,与我们aop共同组成spring当中的切面编程
- Data Access:数据访问
- Web:对数据访问的支持
- Transactions:用于支持我们的事物处理。用于解决我么业务层的事物处理问题。
二、Spring框架的入门案例
第一步:首先创建maven web工程
跳过骨架选择
创建maven工程
选择web工程
选择web工程
解决JDK版本问题和web.xml文件缺失的问题
解决JDK版本问题
在pom.xml文件中,加入下面的插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
创建web.xml文件
在webapp目录下创建WEB-INF文件夹,在WEB-INF中创建web.xml文件,加入以下内容:
<?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"
version="2.5">
<display-name>01_helloWorld</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
最后更新项目
第三步:导入Spring框架的核心jar包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
第四步:创建接口和实现类
UserDao接口
package com.itheima.dao;
public interface UserDao {
public void sayHello();
}
实现类UserDaoImpl
package com.itheima.dao;
public class UserDaoImpl implements UserDao {
private String userName;
@Override
public void sayHello() {
System.out.println("HelloWorld");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
第五步:在applicationContext.xml中配置Spring容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.itheima.dao.UserDaoImpl">
<property name="userName" value="张三"></property>
</bean>
</beans>
第六步:开发测试用例
@Test
public void getUser() throws Exception {
//创建spring容器的对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//利用容器对象创建UserDao对象
UserDao bean = (UserDao) context.getBean("userDao");
//调用方法
bean.sayHello();
}