org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[repositories.UserDAO]类型的限定bean

时间:2016-09-15 20:58:19

标签: java spring hibernate spring-mvc

我正在使用Spring MVC和Hibernate。

我有一个主控制器来处理所有请求。我编写了一个测试,以便用一个简单的例子来测试mySQL数据库连接。

@Controller

public class HomeController {

@Autowired
private ApplicationContext applicationContext;


@RequestMapping(value = "/db")
public void dbTester(){

    User tester = new User();
    tester.setName("Ytesting");
    tester.setToken("ranodm");
    tester.setNumber("123456789");

    UserDAO userRepo = applicationContext.getBean(UserDAO.class);
    userRepo.save(tester);

}

当到达第二行时,它会抛出此异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [repositories.UserDAO] is defined
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因

org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义[repositories.UserDAO]类型的限定bean     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:374)     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371)     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)     org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)     com.controllers.HomeController.dbTester(HomeController.java:89)     sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     java.lang.reflect.Method.invoke(Method.java:498)     org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)     org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)     org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)     org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)     org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     javax.servlet.http.HttpServlet.service(HttpServlet.java:622)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我无法弄清问题是什么。

我的applicationContext看起来像这样:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="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/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- This will define where the Dispatcher will look for a controller
 that has a Request mapping of "/" -->
<context:component-scan base-package="com.controllers"/>

<!-- This sets the default home page-->
<mvc:view-controller path="/" view-name="/login"/>
<!-- Sets the default path for all the Resources -->
<mvc:resources mapping="/resources/**" location="resources/"/>

和我的UserDOAImpl:

@Repository

公共类UserDAOImpl实现UserDAO {

@Autowired
private SessionFactory sessionFactory;


@Transactional
public User getByID(int id) {
    return null;
}

@Transactional
public void save(User u) {
    sessionFactory.getCurrentSession().save(u);
}

}

我似乎无法弄清楚问题是什么

修改

这是我的ApplictionContext xml:

<context:annotation-config/>
<context:component-scan base-package="models"/>
<context:component-scan base-package="repositories"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="controllers"/>

<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/kroc_db_1"/>
    <property name="username" value="root"/>
    <property name="password" value="yossi"/>
</bean>



<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>models.entities.CardInfo</value>
            <value>models.entities.Vendor</value>
            <value>models.entities.User</value>
            <value>models.entities.Card</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

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

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory"/>

0 个答案:

没有答案