1 Autowire自动装配
1.1 使用:只需在<bean>中使用autowire元素
<bean ></bean>


2.2 依赖关系
2.2.1 Spring允许通过使用depends-on属性设定bean的前置bean,依赖的bean必须在本bean实例化前创建好
2.2.2 如果依赖多个bean,可以使用逗号或空格的方式隔开
<!-- bean的依赖关系 -->
<bean ></bean>

3.2 可以使用scope进行配置

Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

4 使用外部属性文件
4.1 bean配置有时需要系统部署信息,如文件路径、数据源配置信息等,需要将其与bean配置文件分离
4.2 Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器,可以使用外部属性文件,使用${var}
4.3 spring2.5后,可通过<context:property-placeholder> 直接使用PropertyPlaceholderConfigurer ,使用locaiton属性指明配置文件路径,需要引入context schema
<!-- 引入外部属性文件 -->
<context:property-placeholder location="db.properties"/>

db.properties与properties.xml必须同级目录,否则会报错,

class path resource [db.properties] cannot be opened because it does not exist
Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

 

 

<!-- 使用外部属性配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<bean ></property>
</bean>

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-08-17
  • 2022-03-07
  • 2022-12-23
  • 2021-07-14
猜你喜欢
  • 2022-02-27
  • 2021-12-21
  • 2021-10-19
  • 2021-10-24
  • 2022-12-23
  • 2021-11-14
相关资源
相似解决方案