spring bean abstract="true"
2014-09-30 10:55
344 查看
Abstract beans in Spring are somewhat different from abstract classes. In fact, abstract bean in Spring doesn't even have to be mapped to any class. Take this as an example:
And classes:
Note that
not have any parent (abstract or not) base class in common. Parent abstract bean definition is used only to group common
properties, so you avoid repetition in XML.
On the other hand introducing abstract
that both
from would be a good idea:
But still
doesn't have to define a class. Treat abstract beans as a way to reduce duplication in XML when several concrete beans have same dependencies.
<bean id="dao" abstract="true"> <property name="dataSource" ref="dataSource"/> <property name="someHelper" ref="someHelper"/> </bean> <bean id="fooDao" class="FooDao" parent="dao"> <property name="fooHelper" ref="fooHelper"/> </bean> <bean id="barDao" class="BarDao" parent="dao"> <property name="barHelper" ref="barHelper"/> </bean>
And classes:
public class FooDao { private DataSource dataSource; private SomeHelper someHelper; private FooHelper fooHelper; //setters } public class BarDao { private DataSource dataSource; private SomeHelper someHelper; private BarHelper barHelper; //setters }
Note that
FooDaoand
BarDaodo
not have any parent (abstract or not) base class in common. Parent abstract bean definition is used only to group common
properties, so you avoid repetition in XML.
On the other hand introducing abstract
Daoclass
that both
FooDaoand
BarDaoinherit
from would be a good idea:
public abstract Dao { protected DataSource dataSource; protected SomeHelper someHelper; //setters } public class FooDao extends Dao { private FooHelper fooHelper; //setters } public class BarDao extends Dao { private BarHelper barHelper; //setters }
But still
daobean
doesn't have to define a class. Treat abstract beans as a way to reduce duplication in XML when several concrete beans have same dependencies.
相关文章推荐
- Spring abstract="true" merge="true"
- Spring 定义:abstract="true"(转http://blog.163.com/yangyanninhao@126/blog/static/239900292007101863043493/)
- Spring 定义:abstract="true"
- Spring 定义:abstract="true"
- spring abstract = “true” 抽象bean及继承(模板)
- Spring 定义:abstract="true" ,可适用模板
- spring 依赖对象的注入方式(3种) 和 公共属性的设置abstract="true" parent
- proxy-target-class="true"对Spring实例创建的影响
- spring bean scope="prototype" 有状态bean 无状态bean
- Attribute "scope" must be declared for element type "bean"-[spring]
- Spring中<ref local=""/>与<ref bean=""/>区别
- Spring中<ref local=""/>与<ref bean=""/>区别
- spring3 页面form标签 报错 Neither BindingResult nor plain target object for bean name "company"
- <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
- Spring中<ref local=""/>与<ref bean=""/>区别(转载)
- spring3 页面form标签 报错 Neither BindingResult nor plain target object for bean name "company"
- spring security no bean defined "bean名为过滤器的名字"
- Spring配置default-lazy-init="true"
- spring3 页面form标签 报错 Neither BindingResult nor plain target object for bean name "company"
- spring3 页面form标签 报错 Neither BindingResult nor plain target object for bean name "company"