Prefix “context” for element “context:component-scan” or "context:annotation-config" is not bound : Spring error

When you are working on spring or spring mvc application, you may encounter below error message.
org.xml.sax.SAXParseException: The prefix "context" for element "context:component-scan" is not bound .
or
org.xml.sax.SAXParseException: The prefix "context" for element "context:annotation-config" is not bound .

This issue generally occurs when you do not include context namespace.
For example: Lets say you have below xml configuration file.
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd" >

    <context:annotation-config />
    <context:component-scan base-package="controller" />

</beans>
Here you might get above exception because you are missing context namespace xmlns:context here.
When you add below line in above xml.
xmlns:context="http://www.springframework.org/schema/context"
so your final xml file will look as below:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd" >

    <context:annotation-config />
    <context:component-scan base-package="controller" />

</beans>
I hope it will solve context is not bound error.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012