美文网首页
Spring bean学习整理

Spring bean学习整理

作者: 沥人土土 | 来源:发表于2017-04-11 09:35 被阅读0次

Bean Life Cycle

2 important bean lifecycle callback methods:


1) initialization callbacks
2) Destruction callbacks


It is recommended that you do not use the InitializingBean or DisposableBean callbacks, because XML configuration gives much flexibility in terms of naming your method.

Default initialization and destroy methods:
If you have too many beans having initialization and or destroy methods with the same name, you don't need to declare init-method and destroy-method on each individual bean. Instead framework provides the flexibility to configure such situation using default-init-method and default-destroy-method attributes on the <beans> element as follows:

<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-3.0.xsd"    default-init-method="init"     default-destroy-method="destroy">   
  <bean id="..." class="...">       
<!-- collaborators and configuration for this bean go here -->     
  </bean>
</beans>

Bean Post Processors

the
BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic etc. After Spring container finishes instantiating, configuring and initializing a bean you can also implement some custom logic by plugging in one or more BeanPostProcessor implementations.

Set Order property to implement the Ordered interface.

An ApplicationContext automatically detects any beans that are defined with implementation of the BeanPostProcessor interface and registers these beans as post-processors, to be then called appropriately by the container upon bean creation.

Bean Definition Inheritance


Contain lots of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on.

Child definition can override some values or add others.

Spring Bean definition inheritance has nothing to do with Java class inheritance but inheritance concept is same.

Bean Definition Template:

You can create a Bean definition template which can be used by other child bean definitions without putting much effort. While defining a Bean Definition Template, you should not specify class attribute and should specify abstract attribute with a value of true as shown below:

<?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-3.0.xsd">
<bean id="beanTeamplate" abstract="true"> 
  <property name="message1" value="Hello World!"/> 
  <property name="message2" value="Hello Second World!"/>         
  <property name="message3" value="Bean bean bean"/> 
</bean> 
<bean id="helloWorld" class="com.springtest.HelloWorld" parent="beanTeamplate"> 
  <property name="message1" value="Hello World!"/>
   <property name="message3" value="Bean bean bean"/> 
</bean>
</beans>

The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions.

Dependency Injection
Dependency Injection (or sometime called wiring) helps in gluing these classes together and same time keeping them independent

Injecting Inner Beans
inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.

相关文章

网友评论

      本文标题:Spring bean学习整理

      本文链接:https://www.haomeiwen.com/subject/gpryattx.html