Spring-Context 7: Use p-namesapce and c-namespace to simplify bean Definitions

Source: Internet
Author: User

Bean definitions in Spring can be defined in a variety of ways. Even if you use xml for configuration, there are many different ways. For example, the following bean definition: <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.xsd"> <bean id = "person" class = "Person"> <property name = "name" value = "Tom"/> <property name = "age "value =" 20 "/> </bean> </beans> beans have three rows, use p-namespace And can be simplified to one line. <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "person" class = "Person" p: name = "Tom" p: age = "20"/> </beans> so what is p-namespace? It is used to replace the <property/> node in xml to define bean attributes. What is this amazing p? It uses the xml Extension Configuration format of namespace. The configuration format of beans is defined in an xsd format (that is, http://www.springframework.org/schema/beans/spring-beans.xsd), but p does not have an xsd format file that corresponds to it, but it can be parsed by the spring kernel. The above only demonstrates the use of p-namespace injection when the property is a common value. What should I do if the property is referenced by another bean? Very simple. This is a normal method for injecting attributes. <Bean id = "messageService" class = "SimpleMessageService"/> <bean id = "messageHandler" class = "MessageHandler"> <property name = "messageService"> <ref bean =" messageService "/> </property> </bean> uses p-namespace. <Bean id = "messageService" class = "SimpleMessageService"/> <bean id = "messageHandler" class = "MessageHandler" p: messageService-ref = "messageService"/> with the-ref suffix, it indicates a reference to a bean. Since the setter method can use p-namespace to inject beans, is there any shorthand for Constructor injection? The answer is yes, that is, c-namespace. The principle and usage are similar to those of p-namespace. Before using c-namespace: <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: c = "http://www.springframework.org/schema/c" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "person" class = "Person"> <constructor-arg name = "name"> <value> Tom </value> </ construct Or-arg> <constructor-arg name = "age" value = "20"/> </bean> </beans> after c-namespace is used: <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: c = "http://www.springframework.org/schema/c" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "person" c: name = "Tom "C: age =" 20 "/> </beans> You can also use the-ref suffix to reference another bean. <Bean id = "messageService" class = "SimpleMessageService"/> <bean id = "messageHandler" class = "MessageHandler" c: messageService-ref = "messageService"/> when the constructor injection is described in the previous section, you can use the constructor index to inject dependencies. c-namespace also supports this method. <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: c = "http://www.springframework.org/schema/c" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "person" c: _ 0 = "Tom" c: _ 1 = "20"/> <bean id = "messageService" class = "SimpleMessageService"/> <bean id = "mes SageHandler "class =" MessageHandler "c: _ 0-ref =" messageService "/> </beans> is it very powerful. But it is too strong and easy to hurt. It is best to reach an agreement with the project members before using these skills in the project. Please download the source code in this example on GitHub.

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.