# Spring知识整理

# 杂乱无章

# ClassPathXmlApplicationContext

加载xml配置文件. 使用的方式:

  1. 在controller的构造方法中使用.

# spring配置文件

# <bean/>参数

  1. init-method 用于指定创建对象时使用调用哪个初始化方法 !!! 请了解初始化顺序

  2. 配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer用来加载properties配置文件

<bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:spring/xxx.properties"/>
</bean>

在配置了此bean的配置文件中可以使用${key}来引用properties文件中的软编码.

  1. destroy-method 在((ClassPathXmlApplicationContext) context).close();会执行destroy-method

# FactoryBean的用法

spring创建bean有两种方式, 一种是普通的bean, 另一种是工厂bean, 即FactoryBean.

FactoryBean的实现类有以下三个方法:

  1. getObject(); 获取对应bean的实例
  2. getObjectType(); 返回bean的类型
  3. isSingleton(); 创建的bean是否单例

# @Autowired使用在构造函数/成员变量

java变量初始化顺序: 静态变量或静态语句块–>实例变量或初始化语句块–>构造方法–>@Autowired

在构造方法中初始化会优于成员变量初始化.

修改于: 8/11/2022, 3:17:56 PM