spring study[2]

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.