文章目录
1⃣️ 基础知识
程序的耦合和解耦
**耦合:**程序之间的依赖关系
注册驱动也可使用:
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
但是这种方法,在没有com.mysql.jdbc.Driver()直接不能通过编译
- 方法间的依赖
- 类之间的依赖
**解耦:**降低程序之间的依赖关系,做到编译期不依赖,运行时才依赖。
- 创建对象时使用反射来创建对象,而避免使用new关键字
- 通过读取配置文件来获取创建对象的的全限定类姓名
工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
IoC (Inversion of Control)控制反转
功能:解决程序的耦合
通过spring提供的工厂获取bean对象,ApplicationContext是容器接口,他的三个常用实现类:
加载类路径下的配置文件,要求配置文件必须在类路径下
ClassPathXmlApplicationContext
加载磁盘任意路径下的 配置文件
FileSystemXmlApplicationContext
用于读取注解创建容器
AnnotationConfigApplicationContext
ApplicationContext
构建核心容器时,创建对象采取立即加载的方式,只要读取xml文件就马上创建对象
BeanFactory(高层接口)
采用延迟加载,什么时候根据id获取对象,什么时候真正创建对象。
2⃣️Spring中的Bean
Spring创建bean对象方法
1 在resources中创建service.xml
文件
给对象起一个id通过id,使用全限定类名创建方法,其中service.impl.AccountServiceImpl
和Dao.impl.AccountImpl
是自己穿件的两个类。
此方法使用默认构造函数,如果没有默认构造函数就会出错
<bean id="accountService" class="service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="Dao.impl.AccountImpl"></bean>
第一种方式需要对对象进行强转
第二种方式直接传入对象的类,得到的就是目标结果
//获取 核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("service.xml");
//第一种方式。根据id获取bean对象
IAccountService as = (IAccountService)ac.getBean("accountService");
as.show();
//第二种方式
IAccountDao ad = ac.getBean("accountDao", IAccountDao.class);
System.out.println(ad);
2 通过另一个类间接生成对象
在service.xml
中配置普通工厂的bean,然后在配置目标对象的bean时,加入factory-bean
和 factory-method
指向普通工厂id和创建方法。
<bean id="accountDaoFactory" class="factory.AccountFactory"></bean>
<bean id="accountDao" factory-bean="accountDaoFactory" factory-method="getAccountDao"></bean>
工厂类,间接返回对象
public class AccountFactory {
public IAccountDao getAccountDao(){
return new AccountImpl();
}
}
调用方法不变还是通过Spring工厂根据输入id返回对象。
3 静态方法返回对象
public class AccountFactory {
public static IAccountDao getAccountDao(){
return new AccountImpl();
}
}
service.xml配置,与第一种方法不同,需要配置class和factory-method
<bean id="accountDao" class="factory.AccountFactory" factory-method="getAccountDao"></bean>
bean的作用范围
<bean>
标签的scpoe
属性
singleton:单例的(默认)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境会话范围
bean对象的生命周期
单例对象:
- 容器创建时,对象创建
- 容器销毁对象死亡
多例对象:
- 当我们使用对象时,spring框架进行创建
- 对象长时间不实用由 JVM垃圾回收器回收
3⃣️Spring依赖注入
注入的数据分类:
- 基本类型和String
- 其他bean类型
- 复杂类型/集合
注入方式:
- 构造函数
- set方法提供
- 注解提供
1 构造函数注入
在对象中加入变量,并且加入构造函数
private String name;
private int age;
private Date date;
public AccountImpl(String name, int age, Date date) {
this.name = name;
this.age = age;
this.date = date;
}
使用标签<constructor-arg>
加入参数,在<bean>
中加入标签,constructor-arg 属性
type:执行注入数据诶行
index:指定注入索引位置
name:指定参数名称 (常用)
value:基本类型String数据提供
ref:指定引用其他类型bean对象,在IOC容器中出现过的bean对象
service配置:
<bean id="accountDao" class="Dao.impl.AccountImpl">
<constructor-arg name="name" value="c"></constructor-arg>
<constructor-arg name="age" value="10"></constructor-arg>
<constructor-arg name="date" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
2 set注入
构造函数注入,强制输入数据类型,尽管可能不需要。set注入采用<property>
标签,不需要强制注入数据。
<bean id="accountDao" class="Dao.impl.AccountImpl">
<property name="name" value="c"></property>
<property name="age" value="10"></property>
<property name="date" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>