1 前言
前面更新过几篇关于 Struts2 框架和 Hibernate 框架的文章, 但鉴于这两种框架在今天的主流开发中已经很少用了, 所以关于这两种框架的更新权当兴趣, 有时间也还会更新的现在主流开发中代替这两个框架的是 SpringMVC 和 MyBatis 等等, 接下来主要精力集中于 SpringSpringMVCSpring Boot 等等, 特别是 Spring Boot 框架, 当然肯定也会有其他方面的内容, 知无不写, 写无不尽
2Spring 框架概述
(1)Spring 框架在三层架构中的位置
Spring 框架在三层架构中的位置在 SSH 框架之 Hibernate 系列 (一) 这篇文章中已经提到过, 主要描述与下图:
(2)Spring 是什么
Spring 是一个轻量级的 Java 开源框架, 兴起于 2003 年, 它分层架构的特点为 J2EE 应用程序开发提供集成的框架 Spring 的核心是控制反转 (IOC) 和面向切面(AOP), 是一个一站式的框架
(3)Spring 框架的优势与功能(一站式)
Spring 相当于一个容器, 可以将所有对象创建和依赖关系维护, 交给 Spring 管理
AOP 编程的支持, 容器中装什么对象就有什么功能
Spring 提供面向切面编程, 可以方便的实现对程序进行权限拦截运行监控等功能声明式事务的支持只需要通过配置就可以完成对事务的管理, 而无需手动编程, 使得程序的测试更为方便
Spring 不仅不排斥各种优秀的开源框架, 如: StrutsHibernateMyBatis 等, 还能帮其他框架管理对象, 降低 JavaEE API 的使用难度
Spring 对 JavaEE 中的 API 如 JDBCJavaMail 等提供了封装, 使这些 API 应用难度大大降低
Spring 支持 JUnit 测试支持, 可以通过注解方便的测试 Spring 程序方便集成各种优秀框架
3 搭建 Spring 开发环境并完成第一个测试实例
(1)新建一个 web 项目, 导入 Spring 所需要的基本包, 如下图所示:
(2)创建一个 Bean 对象, 代码如下:
- package com.spring.bean;
- public class Student {
- // 基本属性
- private String name; // 姓名
- private int age; // 年龄
- private String grade; // 年级
- private String course; // 科目
- private int rank; // 排名
- //setter 和 getter 方法
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getGrade() {
- return grade;
- }
- public void setGrade(String grade) {
- this.grade = grade;
- }
- public String getCourse() {
- return course;
- }
- public void setCourse(String course) {
- this.course = course;
- }
- public int getRank() {
- return rank;
- }
- public void setRank(int rank) {
- this.rank = rank;
- }
- }
(3)在 src 下新建配置文件 applicationContext.xml, 将注册对象写入容器, 代码如下:
- applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
- <!-- 将 User 对象交给 spring 容器管理 -->
- <bean name="stu" class="com.spring.bean.Student" ></bean>
- </beans>
(4)书写测试类 TestDemo.java, 代码如下:
- package com.spring.test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.spring.bean.Student;
- public class TestDemo {
- @Test
- public void test(){
- // 创建容器对象
- ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
- // 向容器获取 user 对象
- Student student = (Student) ac.getBean("stu");
- //3 打印 stu 对象
- System.out.println(student);
- }
- }
(5)使用 JUnit4 进行测试, 结果如下图:
本项目运行环境: jdk1.7
4Spring 配置详解
- <!-- Bean 元素: 使用该元素描述需要 spring 容器管理的对象
- class 属性: 被管理对象的完整类名.
- name 属性: 给被管理的对象起个名字. 获得对象时根据该名称获得对象.
- 可以重复. 可以使用特殊字符.
- id 属性: 与 name 属性一模一样.
- 名称不可重复. 不能使用特殊字符.
- 结论: 尽量使用 name 属性.
- -->
- <bean name="stu" class="com.spring.bean.Student" ></bean>
- <!--
- 可以通过 import 属性导入其他 spring 配置文件
- <import resource="路径 /*.xml"/>
- -->
5Spring 框架的思想
(1)IOC:Inverse Of Control, 即控制反转, 将对象创建的方式交给了 Spring 容器;
(2)DI:Dependency Injection, 即依赖注入, IOC 的实现需要 DI 支持
6Spring 属性注入
(1)set 方法注入:
- <bean name="stu" class="com.spring.bean.Student" >
- <!-- 值类型注入: 为 stu 对象中名为 name 的属性注入 "孔乙己" 作为值 -->
- <property name="name" value="孔乙己" ></property>
- <property name="age" value="22" ></property>
- <property name="grade" value="大三" ></property>
- <property name="rang" value="1" ></property>
- <!-- 引用类型注入: 为 course 属性注入下方配置的 course 对象 -->
- <property name="course" ref="course" ></property>
- </bean>
- <!-- 将 course 对象配置到容器中 -->
- <bean name="course" class="com.spring.bean.Course" >
- <property name="name" value="Java 开发" ></property>
- <property name="page" value="889" ></property>
- </bean>
(2)构造函数注入:
- <bean name="stu1" class="com.spring.bean.Student" >
- <!-- name 属性: 构造函数的参数名 -->
- <!-- index 属性: 构造函数的参数索引 -->
- <!-- type 属性: 构造函数的参数类型 -->
- <constructor-arg name="name" index="0" type="java.lang.Integer" value="compasser" ></constructor-arg>
- <constructor-arg name="course" ref="course" index="1" ></constructor-arg>
- </bean>
来源: https://www.cnblogs.com/compassblog/p/8457785.html