The role of parent on the bean in the SPRING configuration _spring

Source: Internet
Author: User

Eliminates redundant parent-class configuration, such as transaction management:

<bean id= "BasicTxProxy2" abstract= "true"
		class= " Org.springframework.transaction.interceptor.TransactionProxyFactoryBean ">
		<property name=" TransactionManager "ref=" TransactionManager2 "/>
		<property name=" Transactionattributes ">
			< props>
				<prop key= "save*" >PROPAGATION_REQUIRED</prop>
				<prop key= "add*" >propagation_ required</prop>
				<prop key= "remove*" >PROPAGATION_REQUIRED</prop>
				<prop key= "update*" >PROPAGATION_REQUIRED</prop>
				<prop key= "*" >PROPAGATION_REQUIRED</prop>
			</props >
		</property>
	</bean>

<bean id= "Torganrelationservice" parent= "Basictxproxy" >
		<property name= "target" ref= Torganrelationserviceimpl "/>
	</bean>

Because there is no target attribute in the declaration, it must be declared as an abstract class, that is, abstract = true;

Attention

By default,
ApplicationContext (not beanfactory) instantiates all singleton beans. So it's important to note that if you just want to use a (parent) bean definition as a template and it specifies the class attribute, you have to set the ' abstract ' property to ' true ' or the application context will (try) to instantiate
Abstract
Bean. Note: Because the abstract= "true" is set in the set bean definition, it cannot be instantiated by the container, it only acts as a template for other beans to inherit, so that its properties can be undefined in the class body, directly in the bean's declaration <proerty/ > statement can be. After the child Bean inherits from him, you need to provide the corresponding property and set method, and you can get the value inherited from the parent bean in the child bean

If the child bean definition does not specify a class attribute, it will use the class attribute defined by the parent bean, and of course it can be overridden. In the latter case, the child Bean's class attribute value must be half-bean compatible, meaning it must accept the parent Bean's property value.

A child bean defines the methods that can inherit constructor parameter values, property values, and overwrite parent beans from the parent bean, and optionally add new values. If Init-method,destroy-method and/or static factory-method are specified, they overwrite the corresponding settings of the parent bean.

The remaining settings are always derived from the child bean definition: dependencies, automatic assembly mode, dependency checking, singleton, scope, and deferred initialization.

The following are general examples:

Parent class:

<bean id= "Abstractservicethread" class= "Com.project.schedual.ServiceThread" abstract= "true" >
		< Property Name= "Basedao" ref= "Basedao" ></property>
	</bean>

Sub class:

<!--Zhoushun Level 2 process-->
	<bean id= "Docreceiveflowthread" parent= "Abstractservicethread" >
		< Property Name= "Svc" ref= "Docreceiveflowservice" ></property>
	</bean>

Because the subclass automatically inherits the properties of the parent class, the SVC injects the attributes in the parent class class:

<bean
		id= "docreceiveflowservices"
		class= "Com.project.docReceiveFlow.service.DocReceiveFlowService" >		
		<property name= "Basedao" ref= "Basedao"/>
	</bean>
	
	<bean id=
		" Docreceiveflowservice "parent=" Basictxproxy >
		<property name= "Proxytargetclass" value= "true"/>  
		<property name= "target" ref= "docreceiveflowservices"/>
	</bean>	


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.