Spring框架两大核心:
IOC(控制反转)/ DI(依赖注入)
AOP(面向切面编程)
springIOC容器:管理项目,管理Bean,(bean容器)
bean=========》计算机世界中可以重复利用的组件
javaBean=====》Java世界中可以重复利用的组件
实体类========》需要实例化使用
bean > javaBean > 实体类
springIOC容器管理Bean的优势:符合高内聚,低耦合的编程思想
-------------------------初步实现解耦合---------------------------
解耦合实现步骤:
1.通过反射创建对象
2.将需要创建对象的完全限定类名 单独保存在资源文件中
//先导坐标
public class Test01 {
public static void main(String[] args) throws Exception{
/**********************建立数据库链接对象方式1(耦合性强)************************/
// //1.注册驱动
// DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
// //2.获取连接
// Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/2204db?serverTimezone=GMT","root","123456");
// System.out.println(connection);
/**********************建立数据库链接对象方式2(反射解耦)************************/
// //1.注册驱动(反射的方式)
// Class.forName("com.mysql.cj.jdbc.Driver");
// //2.获取连接
// Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/2204db?serverTimezone=GMT","root","123456");
// System.out.println(connection);
/**********************建立数据库链接对象方式3(深层次解耦)************************/
//1.创建资源对象
Properties properties = new Properties();
//2.创建流对象(前面创建了jdbc.properties)
InputStream is = Test01.class.getClassLoader().getResourceAsStream("jdbc.properties");
//3.加载流
properties.load(is);
//4.通过key获取value
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
//5.注册驱动
Class.forName(driver);
//6.获取连接
Connection connection = DriverManager.getConnection(url,name,pwd);
System.out.println(connection);
}
}
---------------------------IOC(控制反转)--------------------
创建好spring主配置文件后,加入类
<!-- spring主配置文件===SpringIOC容器 -->
<?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 class="com.apesource.dao.DemoDaoImp" id="dao"></bean>
<!--一个类配置一个<bean></bean>标签 class是类的路径 id是唯一标识 -->
<bean class="com.apesource.service.DemoServiceImp" id="service"></bean>
<bean class="com.apesource.controller.DemoControllerImp" id="controller"></bean>
</beans>
获取容器中对象的方式:
spring容器的两大核心对象
ApplicationContext:子接口,默认管理bean的方式是单例(性能好,不安全)
BeanFactory:父接口,默认管理bean的方式是多例(性能差,安全)
public class Test01 {
public static void main(String[] args) {
//加载spring的主配置文件
ApplicationContext/* BeanFactory*/ applicationContext =
new ClassPathXmlApplicationContext("beans.xml");
//获取对象
IDemoController controller =
(IDemoController) applicationContext.getBean("controller");
controller.save();
}
}
获取容器的方式有三种:
(1)ClassPathXmlApplicationContext("beans.xml")-------->通过xml的“相对路径”获取spring容器
(2)FileSystemXmlApplicationContext("D:\Mavena\Maven_Spring\src\main\resources\beans.xml")------>通过xml的“绝对路径”获取spring容器
(3)AnnotationConfigApplicationContext()------------------->通过加载配置类获取spring容器
=========================DI注入================================
注入方式:
支持类型
自建的类(串联调用)
String和基本数据类型
复杂类型(构造注入不支持)
1.set注入
步骤:
①类中写set方法
1》基本数据和String
package com.apesource.pojo;
/**
* @Author Maven
* @VERSION 1.0
* @since 2022/7/14
*/
public class Teacher {
private String tname;
private int tage;
private double tmoney;
private String thobby;
//set注入
public void setTname(String tname) {
this.tname = tname;
}
public void setTage(int tage) {
this.tage = tage;
}
public void setTmoney(double tmoney) {
this.tmoney = tmoney;
}
public void setThobby(String thobby) {
this.thobby = thobby;
}
@Override
public String toString() {
return "Teacher{" +
"tname='" + tname + '\'' +
", tage=&