在spring servlet.xml

时间:2017-10-09 17:11:11

标签: java xml spring spring-mvc servlets

我是一个初学者,我想在我的servlet.xml中添加所有bean,因为只有一个控制器在监听。

我在spring-servlet.xml中有这个

    <?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" xmlns:tx="http://www.springframework.org/schema/tx"
 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
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

 <annotation-driven />

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

 <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <beans:property name="url"
   value="jdbc:mysql://localhost:3306/CountryData" />
  <beans:property name="username" value="XXX" />
  <beans:property name="password" value="XXX" />
 </beans:bean>

 <!-- Hibernate 4 SessionFactory Bean definition -->
 <beans:bean id="hibernate4AnnotatedSessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <beans:property name="dataSource" ref="dataSource" />
  <beans:property name="annotatedClasses">
   <beans:list>
    <beans:value>shadows.bean.Country</beans:value>
   </beans:list>
  </beans:property>
  <beans:property name="hibernateProperties">
   <beans:props>
    <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
    </beans:prop>
    <beans:prop key="hibernate.show_sql">true</beans:prop>
   </beans:props>
  </beans:property>
 </beans:bean>

 <context:component-scan base-package="shadows" />

 <tx:annotation-driven transaction-manager="transactionManager"/>

 <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
 </beans:bean>


</beans:beans>

首先感觉,我说:

[...]
<beans:list>
        <beans:value>shadows.bean.Country</beans:value>
        <beans:value>shadows.bean.Provider</beans:value>
</beans:list>
[...]

但我有这个问题:

  

无法转换类型&#39; java.util.ArrayList&#39;的属性值至   必需的类型&#39; java.lang.Class []&#39;对于属性&#39; annotatedClasses&#39 ;;   嵌套异常是java.lang.IllegalArgumentException:找不到   class [shadows.bean.Provider]

我的班级很适合我的阴影。我认为我不必像那样宣布我的豆类......

编辑:

如果我更换:

[...]
<beans:list>
        <beans:value>shadows.bean.Country</beans:value>
        <beans:value>shadows.bean.Provider</beans:value>
</beans:list>
[...]

通过

[...] <annotation-driven />
       <property name="packagesToScan" value="shadows.bean" />

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

[...]

当我启动Tomcat7时,我遇到了很多问题:

  

cvc-complex-type.2.4.c:匹配的通配符是严格的,但没有   声明可以在元素&#34;属性&#34;

中找到

1 个答案:

答案 0 :(得分:1)

尝试使用此配置:

   <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="ws.crossnet.apn.persistencia.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

此致