Jitendra Pareek
Jitendra Pareek

Reputation: 637

Spring MVC + Hibernate + Maven : Multiple annotations found in bean declaration

I am developing a web application in Spring MVC based on maven with hibernate using eclipse juno. When I am trying to configure hibernate in servlet-context.xml showing error as follows :

cvc-elt.1: Cannot find the declaration of element 'beans'.

servlet-context.xml looks like this :

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">
 <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<context:component-scan base-package="com.webworld.crm" />


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  <property name="driverClassName" value="${database.driver}" />  
  <property name="url" value="${database.url}" />  
  <property name="username" value="${database.user}" />  
  <property name="password" value="${database.password}" />  
</bean>  

<bean id="mysessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    <property name="dataSource" ref="dataSource"></property>  

    <property name="mappingResources">  
    <list>  
    <value>employee.hbm.xml</value>  
    </list>  
    </property>  

    <property name="hibernateProperties">  
        <props>  
            <prop key="hibernate.dialect">MySQL5Dialect</prop>  
            <prop key="hibernate.hbm2ddl.auto">update</prop>  
            <prop key="hibernate.show_sql">true</prop>  

        </props>  
    </property>  
</bean>  

<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
    <property name="sessionFactory" ref="mysessionFactory"></property>  
</bean>  

<bean id="d" class="com.javatpoint.EmployeeDao">  
    <property name="template" ref="template"></property>  
</bean>
</beans>

The file structure is as follows :

enter image description here

Please answer this as soon as possible.

Upvotes: 0

Views: 7400

Answers (2)

M. Deinum
M. Deinum

Reputation: 124898

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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">

The problem is you specified spring-mvc.xsd as the root namespace, the namespace you can use without a prefix. So <annotation-driven /> corresponds to <mvc:annotation-driven />. However there is no <bean> tag in spring-mvc.xsd as that is in spring-beans.xsd.

To make it work you need to either prefix all your <bean />, <property /> etc. with beans: as that is what you specified for the spring-beans.xsd as prefix. Or switch the root namespace to spring-beans.xsd and prefix <annotation-driven /> with mvc.

Example with beans: prefix

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

Example with different root namespace

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            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">

    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" />

Upvotes: 3

Manjunath D R
Manjunath D R

Reputation: 371

For first bean definition you have given **beans:**bean prefix as below.

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

But for rest of all bean definitions you have missed to provide the prefix "beans". I think this could be problem as parser is not able to identify this tag. Please try giving prefix to all the tags and try.

Upvotes: 0

Related Questions