Spring
一:Spring概述
-
什么是Spring?
-
分层设计(持久层mybatis、业务层、控制层) -
Java SE/Java EE应用full-stack(全栈式)轻量级 开源的框架
- 轻量级:
-
两大核心
2:Sprig框架的发展史
- 1997:IBM EJB
- 2002年的,EJB
- 2006年:EJB 3.0
- 2017年:spring 5
3:Spring优势
- 方便解耦,简化开发(IOC)
- spring容器,它可以对所有对象的创建和销毁提供维护,实现了解耦
- 耦合度:对象之间的一种关系。所以要合理的使用耦合度(Spring_IOC)
2.AOP编程的支持
3.声明式事务的支持(数据库)
- 通过配置(xml)来完成事务的支持,不再需要手动的编写代码
4.方便调试,能够使用Juit测试单元
5.方便集成各种优秀的框架
4:Spring体系结构
二:IOC :控制反转 -----》解耦
-
解耦的原则
-
自定义IOC
-
实现services层和dao层进行解耦(反射) -
步骤分析
- 创建Java项目,导入自定义的相关IOC坐标
- 编写Dao层和它的实现类
- 编写Servces层和实现类
- 编写测试代码
三:Spring框架入门案例
- 实现services层和dao层进行解耦
- 第一步:创建项目,导入依赖(Spring)
- 第二步:定义xml(解决硬编码)
- 第三步:容器中取相关对象
- 第四步:测试代码
四:SpringAPI
-
BeanFactory
-
ApplicationContext
-
常用类
-
常用方法
五:Spring配置文件
-
bean标签
-
bean标签的范围配置
-
bean的声明周期配置
六:bean的实例化
package com.Utils;
import com.dao.User02;
public class BeanFactory {
private static User02 user02;
static {
user02 = new User02();
}
public static User02 getUser02(){
return user02;
}
}
package com.Utils;
import com.dao.User02;
public class InstranceFactory {
private User02 user02;
public InstranceFactory(){
user02 = new User02();
}
public User02 getUser02(){
return user02;
}
}
-
实例
-
依赖注入:
-
它也是Spring框架的IOC的具体体现
-
在我们编写代码的时候,创建对象的权力交给Spring(IOC),代码中不可能出现没有依赖的情况。 IOC只能降低他们的依赖关系,但不能消除;Serveices —》Dao -
那这种类似业务层和数据持久层的依赖关系。在使用Spring以后,就让Spring来维护了。依赖注入 -
依赖注入分类
依赖注入:(基本类型/以用类型)
-
数组、List集合、map集合、自定义 -
环境搭建
-
导入依赖
-
配置文件
-
实验相关类
-
package com;
public class Book {
}
-------------------------------
package com;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class User {
private String name;
private int age;
private List<String> lists;
private List<Book> books;
private String[] arr;
private Book[] arrbook;
private Map<String,String> maps;
private Map<String,Book> map_books;
public User() {
}
public User(String name, int age, List<String> lists, List<Book> books, String[] arr, Book[] arrbook, Map<String, String> maps, Map<String, Book> map_books) {
this.name = name;
this.age = age;
this.lists = lists;
this.books = books;
this.arr = arr;
this.arrbook = arrbook;
this.maps = maps;
this.map_books = map_books;
}
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 List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public Book[] getArrbook() {
return arrbook;
}
public void setArrbook(Book[] arrbook) {
this.arrbook = arrbook;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Map<String, Book> getMap_books() {
return map_books;
}
public void setMap_books(Map<String, Book> map_books) {
this.map_books = map_books;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", lists=" + lists +
", books=" + books +
", arr=" + Arrays.toString(arr) +
", arrbook=" + Arrays.toString(arrbook) +
", maps=" + maps +
", map_books=" + map_books +
'}';
}
}
数组类型
List集合和数组一样
Map和properties集合
<property name="lists">
<list>
<value>aaa</value>
<value>bbb</value>
</list>
</property>
<property name="books">
<list>
<ref bean="book"></ref>
</list>
</property>
<property name="maps">
<map>
<entry key="tom" value="wudi"></entry>
<entry key="som" value="hahha"></entry>
</map>
</property>
<property name="map_books">
<map>
<entry key="kkk" value-ref="book"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="01">张三</prop>
<prop key="02">历史</prop>
</props>
</property>
</bean>
配置文件模块化(spring)
spring整合DBUtils(账户 增删改查 三层架构)
-
sql语句
-
CREATE DATABASE spring_db;
USE spring_db;
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
money DOUBLE
);
INSERT INTO account VALUES (NULL,'猪八戒',1000);
-
依赖坐标
-
创建实体类
-
package com.domain;
public class Account {
private int id;
private String name;
private double money;
public Account() {
}
public Account(int id, String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
-
创建DAO
-
Services
-
实现查询所有用户
Spring引入jdbc.properties文件
-
引入context命名空间和约束路径
-
xml文件配置
注解
|