Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What xmlns & schemaLocation to use in Spring context file?

Tags:

java

spring

Do I need all of the below xmlns & xsi:schemaLocation in below spring context file ?

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
            http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <context:annotation-config />

    <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <bean id = "controller" class="simpleController"/>


</beans>

Since I'm just defining a bean class is just using

xmlns="http://www.springframework.org/schema/beans" and its corresponding schema location enough ? 

I don't need the other definitions ? If not when would I need to use these definitions ?

like image 461
user701254 Avatar asked Jun 10 '13 10:06

user701254


3 Answers

  • You need the main bean namespace since you are using XML to define your bean definitions.
  • You need the context namespace since you are using it to define <context:annotation-config />
  • You don't need the oxm namespace unless you are doing xml marshalling/unmarshalling operations via the spring-oxm dependency.
  • You don't need the p namespace unless you are planning to inline setter injection in your bean definitions.
  • You don't need the tx namespace unless you are introducing transactional management via the spring-tx dependency.
  • You don't need the aop namespace unless you are doing any AOP operations via the spring-aop dependency.
  • You don't need the jee namespace unless you are planning to do any Java EE operations like jndi-lookup
like image 118
dimitrisli Avatar answered Oct 12 '22 17:10

dimitrisli


You need only two namespaces.

  • The default one: beans
  • and the context namespace

Any other namespace can be deleted in your application context XML.

Furthermore I suggest you to point to the default XSD instead to the version specific XSD.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">
like image 6
saw303 Avatar answered Oct 12 '22 19:10

saw303


<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">

</beans>
like image 1
Sangram Badi Avatar answered Oct 12 '22 19:10

Sangram Badi