How spring uses namespace p to simplify bean configuration
•
Java
This article mainly introduces how spring uses namespace p to simplify bean configuration. It is introduced in great detail through example code, which has certain reference value for everyone's study or work. Friends in need can refer to it
Generally, we configure beans as follows:
<bean id="car1" class="com.gong.spring.beans.Car"> <property name="name" value="baoma"></property> </bean> <bean id="car2" class="com.gong.spring.beans.Car"> <property name="name" value="benchi"></property> </bean> <bean id="car3" class="com.gong.spring.beans.Car"> <property name="name" value="binli"></property> </bean> <bean id="student" class="com.gong.spring.beans.Student"> <property name="name" value="tom"></property> <property name="age" value="12"></property> <property name="score" value="98.00"></property> <property name="cars" ref="cars"> </property> </bean> <util:list id="cars"> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </util:list>
Note: cars is a public collection bean. There are name, age, score and car attributes of type list < car > in student.
After introducing the namespace, we can configure it as follows:
<bean id="car1" class="com.gong.spring.beans.Car" p:name="baoma"></bean> <bean id="car2" class="com.gong.spring.beans.Car" p:name="benchi"></bean> <bean id="car3" class="com.gong.spring.beans.Car" p:name="binli"></bean> <bean id="student" class="com.gong.spring.beans.Student" p:name="tom" p:age="12" p:score="98.00" p:cars-ref="cars"></bean> <util:list id="cars"> <ref bean="car1"/> <ref bean="car2"/> <ref bean="car3"/> </util:list>
Compared with the original, the code is much simpler.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码