Spring官方文档学习(3.1)

The IoC container

终于到了关键的内容了,IoC容器可以说是Spring的核心内容。这一点我们可以从Spring的Modules 那节的架构图看出来。
本节目录

1. Introduction to the Spring Ioc container and beans
2. Container overview
3. Bean overview
4. Dependencies
5. Bean scopes
6. Customizing the nature of a bean
7. Bean definition inheritance
8. Container Extension Points
9. Annotation-based container configuration
10. Classpath scanning and managed components
11. Using JSR 330 Standard Annotations
12. Java-based container configuration
13. Enviroment abstraction
14. Registering a LoadTimeWeaver
15. Additional Capabilities of the ApplicationContext
16. The beanFactory

6.1 Introduction to the Spring Ioc container and beans

org.springframework.beansorg.springframework.context 是Spring 框架IoC 容器的基础包。
BeanFactoryApplicationContext接口提供了对Object的各种管理功能。

6.2 Container overview

ClassPathXmlApplicationContextFileSystemXmlApplicationContext是在独立应用中创建实例的类。
这是一个简单的Spring配置文件模板。

<?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 id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>

根据模板的相关信息,可以实例化一个容器。并从容器中实例化对象。

// 创建容器
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// 获取所需实例对象
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// 使用对象
List<String> userList = service.getUsernameList();

6.3 Bean overview

IoC容器管理一个或多个Bean。这些Bean根据你的配置创建出来。这一块的东西很杂。

6.4 Dependencies

依赖,什么是依赖?依赖就是一个实例化一个bean需要的东西,可以是bean的属性,引用的其他bean。

  • 依赖注入
  • 详细的依赖和配置
  • 使用depends-on
  • 懒初始化的bean
  • 自动注入
  • 方法注入

6.5 Bean scopes

  • The singleton scope
  • The prototype scope
  • Singleton beans with prototype-bean dependencies
  • Request, session, and global session scopes
  • Custom scopes

从scope的类型来看,Spring本身就使用了很多的设计模式,这样能让我们使用框架的时候就相当于使用了这些设计模式。

6.6 Customizing the nature of a bean

为bean设置初始化、销毁方法,以及一些其他的配置。

6.7 Bean definition inheritance

bean也能像类一样继承,就是不知道在不使用配置文件,用注解配置之后还能不能这么方便。

6.8 Container Extension Points


  • Customizing beans using a BeanPostProcessor
  • Customizing configuration metadata with a BeanFactoryPostProcessor
  • Customizing instantiation logic with a FactoryBean

这里的内容我不是很能理解意义在哪里,可能是工作中没有用到吧,如果有人知道欢迎指点一下。

6.9 Annotation-based container configuration


  • @Required
  • @Autowired
  • Fine-tuning annotation-based autowiring with @Primary
  • Fine-tuning annotation-based autowiring with qualifiers
  • Using generics as autowiring qualifiers
  • CustomAutowireConfigurer
  • @Resource
  • @PostConstruct and @PreDestroy

基于注解的容器配置,感觉还是有个配置文件方便一点啊。

6.10 Classpath scanning and managed components

  • @Component and further stereotype annotations
  • Meta-annotations
  • Automatically detecting classes and registering bean definitions
  • Using filters to customize scanning
  • Defining bean metadata within components
  • Naming autodetected components
  • Providing a scope for autodetected components
  • Providing qualifier metadata with annotations

扫描添加注解的类,并默默的生成bean。这个功能海华丝特别实用的。

6.11 Using JSR 330 Standard Annotations

这个是用java原生的注解。

6.12 Java-based container configuration

  • Basic concepts: @Bean and @Configuration
  • Instantiating the Spring container using
    AnnotationConfigApplicationContext
  • Using the @Bean annotation
  • Using the @Configuration annotation
  • Composing Java-based configurations

这里是用注解配置bean,还是特别重要的。
}

6.13 Environment abstraction

6.15 Additional Capabilities of the ApplicationContext

6.16 The BeanFactory

在写了这么多,看了这么多之后,我不禁产生了一个困惑。这么看能记得住吗,这么多东西。哪怕是把目录背下来,或者弄一个项目来实现文档中所有的功能,我觉得这些都不是学习框架最好的办法。最好的方法是让框架来适应你的需求,而不是你按照框架的功能来学习。至于如何实现,等我有了一个完整的答案,我会写出来的,现在就不乱写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值