使用名称创建bean时出错

时间:2014-08-05 09:10:02

标签: java xml spring spring-mvc

异常:

  

g.springframework.beans.factory.BeanCreationException:创建错误   名为'utenteDAO'的bean:注入自动连接的依赖项   失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   com.mauro.soclib.dao.UtenteDAO.sessionFactory;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   匹配找到的类型为[org.hibernate.SessionFactory]的bean   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

弹簧security.xml文件

<beans...">
   <context:annotation-config />     
   <context:component-scan base-package="com.mauro" />
    <http auto-config='true' >
        <intercept-url pattern="/admin**" access="ROLE_USER" />
        <intercept-url pattern="/paginaConGrafica**" access="ROLE_USER" />


        <form-login login-page="/login" 
            default-target-url="/paginaConGrafica"
            authentication-failure-url="/error-login.html" 
            login-processing-url="/j_spring_security_check"/>
        <logout logout-success-url="/index" />
    </http>

    <beans:bean id="restAuthenticationProvider"
        class="com.mauro.soclib.security.customAuthenticationProvider"  autowire="byType">
    </beans:bean>



    <authentication-manager alias="authenticationManager">
    <authentication-provider
        ref="restAuthenticationProvider" />
     </authentication-manager>



</beans:beans>

的servlet

<?xml  version="1.0" encoding="UTF-8"?>
    <context:annotation-config />
    <context:component-scan base-package="com.mauro.soclib" />
   <!-- 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>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/social_libreria" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>

    <bean id="UtenteValidator" class="com.mauro.soclib.validator.UtenteFormValidator" />


    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:message" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- Scan for the domain objects with the ORM annotation -->  
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="packagesToScan" value="com.mauro.soclib" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.jdbc.batch_size">0</prop>
            </props>
        </property>
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="10000000" />
    </bean>
    <tx:annotation-driven />

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

的web.xml:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- link al config file security -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>
    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
      <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>sessionFactory</param-value>         
      </init-param>      
   </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



    <!-- specifiche della servlet -->
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

2 个答案:

答案 0 :(得分:0)

将此条目添加到您的调度程序servlet,

<bean id="utenteDAO" class="path" />
<bean id="utenteDAOImplementation class" class="path" />

由于您没有自动连接依赖项

,因此抛出错误

修改

尝试定义会话工厂,

@Autowired
@Qualifier("sessionFactory")
public void seSessionFactory(SessionFactory sessionFactory) {
    this.setSessionFactory(sessionFactory);
}

答案 1 :(得分:-1)

更改项目文件夹的位置,并相应地设置系统路径和路径属性。在我们的情况下,它可以正常工作。但我们无法找到确切的原因。