Spring MVC + Hibernate + Maven:在 bean 声明中找到多个注释

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

我正在 Spring MVC 中开发一个基于 maven 的 Web 应用程序,使用 eclipse juno 进行休眠。 当我尝试在 servlet-context.xml 中配置休眠时,显示如下错误:

cvc-elt.1: 找不到元素 'beans'.

的声明

servlet-context.xml 看起来像这样:

<?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>

文件结构如下:

请尽快回答。

对于第一个 bean 定义,您给出了 **beans:**bean 前缀,如下所示。

<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>

但是对于其余的所有 bean 定义,您都没有提供前缀 "beans"。 我认为这可能是个问题,因为解析器无法识别此标记。请尝试为所有标签添加前缀并尝试。

<?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">

问题是您将 spring-mvc.xsd 指定为根命名空间,您可以在没有前缀的情况下使用该命名空间。所以<annotation-driven />对应<mvc:annotation-driven />。但是,spring-mvc.xsd 中没有 <bean> 标记,而 spring-beans.xsd 中则没有。

要使其正常工作,您需要为所有 <bean /><property /> 等添加前缀 beans:,因为这是您为 spring-beans.xsd 指定的前缀.或者将根命名空间切换到 spring-beans.xsd 并在 <annotation-driven /> 前加上 mvc.

带有 beans: 前缀的示例

<?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>

具有不同根名称空间的示例

<?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/" />