spring study[2]

本文详细解析了Spring框架中BeanFactory与ApplicationContext的区别及使用。介绍了BeanFactory作为最简单的容器,提供依赖注入支持;而ApplicationContext则在此基础上提供了应用框架服务,如消息解析和事件发布。文中还对比了两者的加载机制和初始化过程,阐述了如何通过配置定义bean的生命周期。

Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for dependency injection.
Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application framework services such as the ability to resolve textual messages from a properties file and the ability to publish application
events to interested event listeners.

There are several implementations of BeanFactory in Spring. But the most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file.

Bean factory to read the bean definitions from the XML file. But the bean factory doesn’t instantiate the beans just yet.
When getBean() is called, the factory will instantiate the bean and begin setting the bean’s properties using dependency injection.


there are three classes implement ApplicationContext interface,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext.
A bean factory lazily loads all beans, deferring bean creation until the getBean() method is called. An application context is a bit smarter and preloads all singleton beans upon context startup.

a bean factory performs several setup steps before a bean is ready to use. The following list explains each of these steps in more detail:
1. The container finds the bean’s definition and instantiates the bean.
2. Using dependency injection, Spring populates all of the properties as specified in the bean definition.
3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post-ProcessBeforeInitialization() methods will be called.
6. If an init-method is specified for the bean, it will be called.
7. Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.

It is removed from the bean factory in two ways.
1. If the bean implements the DisposableBean interface, the destroy() method is called.
2. If a custom destroy-method is specified, it will be called.

ApplicationContext versus BeanFactory
The only difference here is that if the bean implements the ApplicationContextAware interface, the setApplicationContext() method is called.(after the 4th step)

Notice that there are two ways that a StudentServiceImpl can be given a reference to its StudentDao: either via its constructor or via the setStudentDao() method.

The singleton property of <bean> tells the context whether or not a bean is to be defined as a singleton.

Configuring a bean as a prototype may be useful if you’d like to use the Spring context as a factory for new instances of domain objects, such as Student or
Course objects. As prototype beans.

Declaring a custom init-method in your bean’s definition specifies a method that is to be called on the bean immediately upon instantiation(after calling constructor). Similarly, a custom destroy-method specifies a method that is called just before a bean is removed from the container.

Spring also provides two interfaces that perform the same functionality: InitializingBean and DisposableBean. The InitializingBean interface provides one method, afterPropertiesSet(), that will be called once all of a bean’s properties have been set. Similarly, DisposableBean’s one method, destroy(), will be
called when the bean is removed from the container.

Spring will automatically determine the type of the property being set and convert the value appropriately.

if we are creating an AOP proxy, we may not want the target bean to be accessible in our BeanFactory. In this case, configuring the proxy’s target using an inner bean would achieve this goal.

XML  Type
<list> java.awt.List, arrays
<set> java.awt.Set
<map> java.awt.Map
<props> java.awt.Properties

转载于:https://www.cnblogs.com/tongzhiyong/archive/2007/10/27/939833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值