Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the consensus on Spring p namespace for Beans?

It's been out for a while so I am wondering if people are using the p XML namespace within their Spring configuration files. Is it a helpful shortcut? Is it a good idea that ended up in the trash can?

Where does the Java community largely stand?

like image 901
HDave Avatar asked May 17 '10 20:05

HDave


People also ask

What is the use of P-namespace in Spring?

1.1 Spring P-namespace In spring, developers use the traditional <property> tag to set the properties of any bean. To avoid this, developers can use the p-namespace to perform the setter-based dependency injection. To use it, developers add a declaration in the spring configuration file i.e. <?

What is namespace in Spring?

So, the spring tx namespace is merely a way of identifying things which "belong to" Spring Transactions in an XML configuration document. Visiting the URL of the Spring TX namespace leads you to XML Schemas (rules for what elements, attributes, and values you can have) for the various versions of Spring Transactions.

Which property is replaced by C namespace in the Spring Framework?

Spring c-namespace is an XML shortcut and replacement of the <constructor-arg/> subelement of the <bean/> tag.

Can we have two beans with same name in Spring?

Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.


1 Answers

I use it in every single Spring project I've ever touched. I'd guess my current team has a codebase with at least 50 different Spring files and every one uses the p namespace. It's a lot less typing, and arguably more readable. For instance:

<bean id="fry" class="com.fox">
  <property name="leela" value="fracas" />
  <property name="hawking" ref="panucci" />
  <property name="bender">
    <ref local="uhura" />
  </property>
</bean>

Can much more easily be written as

<bean id="fry" class="com.fox"
  p:leela="fracas"
  p:hawking-ref="panucci"
  p:bender-ref="uhura" />

The only drawback is that you lose the ability to use the local semantic, though honestly I don't use it that often.

The Eclipse's Spring IDE supports the p-namespace and will autocomplete property names for beans. You can even modifier-click the property names to jump to their declarations and I believe the refactoring tools support changing property names too (even if they're in p-namespace notation).

Teammates may take a while to get used to it, but after they learn it they'll be thanking you for making the files that much more concise.

like image 157
jasonmp85 Avatar answered Oct 17 '22 20:10

jasonmp85