当前位置: 代码迷 >> java >> org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException:创建bean时出错
  详细解决方案

org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException:创建bean时出错

热度:206   发布时间:2023-08-04 09:28:45.0

我的控制器是:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.news.model.User;
import com.news.service.UserService;

@Controller
@RequestMapping("/submit")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(method = RequestMethod.GET)
      public ModelAndView helloWorld() {

        return new ModelAndView("submit");
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveArticle(@ModelAttribute("user") User user,  BindingResult result) {
         userService.addUser( user);
                 return new ModelAndView("redirect:/submit");
    }}

我的道课是:

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.news.model.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;


    public void saveUser(User user) {
        sessionFactory.getCurrentSession().save(user);
    }


}

用户服务是:

import com.news.model.User;

public interface UserService {

    public void addUser(User user);

}

userimpl是:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.news.dao.UserDao;
import com.news.model.User;

@Service("userService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public UserServiceImpl() {
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addUser(User user) {
        userDao.saveUser(user);
    }


    }

我在WEB-INF / classes中jdbc.properties

spring-servlet.xml是:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://www.springframework.org/schema/jee
                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
                        http://www.springframework.org/schema/lang
                        http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-4.1.xsd">

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


         <bean id="propertyConfigurer"
                     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                 <property name="locations">         
                   <list>
               <value>/WEB-INF/classes/jdbc.properties</value>          

                 </list>    
                </property>     
                 </bean> 



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





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




    <bean id="tilesViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        p:order="0" p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView"
        p:viewNames=".*" />

    <bean id="jstlViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:order="1" p:viewClass="org.springframework.web.servlet.view.JstlView"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />




    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>


            </bean> 





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

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.news.model.User</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

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

    </beans>

但是当我运行程序时,我得到了错误:

WARN:/TestSpring:unavailable

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired
 dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 

Could not autowire field: private com.news.dao.UserDao com.news.service.UserServiceImpl.userDao;

 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao':

 Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.news.dao.UserDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)

我正在使用:

**spring version 4.1.6RELEASE**
hibernate-core-3.5.4-final,hibernate-annotations-3.5.4-final,hibernate-commons-annotations-3.3.0-final,hibernate-entitymanager-3.5.4-Final,hibernate-validator-4.0.2.GA

我还检查了用户服务字段..也没有错误..所以,问题出在哪里???

尝试升级您的hibernate版本,然后查看它是否有效。 看起来您正在使用的休眠版本没有该方法:

addAnnotatedClass
  相关解决方案