【问题标题】:Error creating bean with name创建具有名称的 bean 时出错
【发布时间】:2014-09-27 21:25:22
【问题描述】:

例外:

g.springframework.beans.factory.BeanCreationException:创建错误 名为“utenteDAO”的bean:注入自动装配的依赖项 失败的;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:私有 org.hibernate.SessionFactory com.mauro.soclib.dao.UtenteDAO.sessionFactory;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 找到类型为 [org.hibernate.SessionFactory] ​​的匹配 bean 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

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

【问题讨论】:

  • 显示你的 servlet 配置

标签: java xml spring spring-mvc


【解决方案1】:

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

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

它抛出错误是因为你没有自动装配依赖关系

编辑:

尝试定义会话工厂,

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

【讨论】:

  • 感谢并抱歉我的英语不好,但问题是:创建名为 'utenteDAO' 的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory com.mauro.soclib.dao.UtenteDAO.sessionFactory;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [org.hibernate.SessionFactory] ​​的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解
  • 我收到了您的错误..您是否尝试将这些 bean 添加到您的调度程序中
  • 我尝试进入但问题依旧);似乎会话工厂位于 spring-security.xml
  • 我在哪里将它插入提供者进行身份验证?
【解决方案2】:

更改项目文件夹的位置并相应地设置系统路径和路径属性。在我们的案例中有效。但我们无法找到确切的原因。

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2021-06-20
    • 2012-06-19
    • 2017-03-28
    • 2011-10-25
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多