Spring Mvc test webapp @Autowired annotation is not working

Allah Rakha

i am creating a spring mvc web app with hibernate and connect home controller for showing list of user, but getting error to mapping Dao, i posted error and project structure below this is the controller method:-

@Autowired
private UserDao userdao;
/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );


    List<Usermaster> umlis= userdao.getUserList();
    model.addAttribute("userlist",umlis);

    return "home";
}

and dao interface is :

package com.ar.dao;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.ar.entity.Usermaster;
@Component
public interface UserDao {

    public boolean userlogin();
    public List<Usermaster> getUserList();

}

and daoimpl class is :

package com.ar.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ar.dao.UserDao;
import com.ar.entity.Usermaster;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private SessionFactory sessionFactory;


    @Override
    @Transactional
    public List<Usermaster> getUserList() {

        Session currentSession=sessionFactory.getCurrentSession();
        Query<Usermaster> theq=currentSession.createQuery("from Usermaster",Usermaster.class);
        List<Usermaster> user=theq.getResultList();
        // TODO Auto-generated method stub
        return user;
    }

}

and servlet-context-xml is:

<?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"
    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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>


<beans:bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<beans:property name="driverClass" value="com.mysql.jdbc.Driver"/>
<beans:property name="jdbcUrl" value="jdbc:mysql://localhost:3306/open_tracket?useSSL=false"/>
<beans:property name="user" value="root"/>
<beans:property name="password" value="12345"/> 
<beans:property name="initialPoolSize" value="5"/>
<beans:property name="minPoolSize" value="5"/>
<beans:property name="maxPoolSize" value="20"/>
<beans:property name="maxIdleTime" value="30000"/>
</beans:bean>
<!-- Step 2: Setup Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="myDataSource"/>
<beans:property name="packagesToScan" value="com.ar.entity"/>
<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="com.ar.webtrack" />



</beans:beans>

and error description is below:

INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5bce9b94: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,myDataSource,sessionFactory,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@21d212da
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ar.dao.UserDao com.ar.webtrack.HomeController.userdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ar.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4931)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3800)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5563)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1388)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1392)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1392)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1360)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ar.dao.UserDao com.ar.webtrack.HomeController.userdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ar.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 31 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ar.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 33 more

i used hstl for showing data in jsp and my project structure is below image

how can i solve this problem?

Andrei Sfat

Your components are not being picked up by Spring because you are limiting with your current component scan. Configure it as follows:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Transaction Hibernate @Transaction annotation not working with @Autowired

From Dev

Spring Test Transactional annotation not working

From Dev

Fine grained Spring autowiring not working (@Autowired with additional custom annotation)

From Dev

Fine grained Spring autowiring not working (@Autowired with additional custom annotation)

From Dev

Spring pure annotation with autowired

From Dev

@Size and @Pattern annotation are not working in Spring MVC

From Dev

Spring MVC annotation based configuration not working

From Dev

Spring autowired annotation with java timertask

From Dev

Spring annotation @Autowired inside methods

From Dev

Autowired annotation error in spring with java

From Dev

Spring: @Autowired not working with ApplicationContext

From Dev

TDD test for Spring MVC controller not working correctly

From Dev

NoSuchBeanDefinitionException using @Autowired in spring test

From Dev

Spring Boot Unit Test Autowired

From Dev

@Autowired Spring annotation with struts2

From Java

Spring injects dependencies in constructor without @Autowired annotation

From Dev

Nullpoint when using Spring Autowired annotation

From Dev

Spring @Configuration annotation @Autowired service is null

From Dev

Spring MVC Autowired null in Component

From Dev

Spring MVC add bean and autowired

From Dev

Autowired is null and not working with Jersey + Spring

From Dev

Spring Autowired in Web Service not working

From Dev

Spring Autowired service and controller not working

From Dev

Spring @Autowired not working in some classes

From Dev

Spring @Autowired not working on new thread

From Dev

Spring @Autowired and @Value on property not working

From Dev

why Spring basic @Autowired is not working?

From Dev

Spring @Autowired not working and returning null

From Dev

Reflection not working for Spring autowired parameters

Related Related

  1. 1

    Spring Transaction Hibernate @Transaction annotation not working with @Autowired

  2. 2

    Spring Test Transactional annotation not working

  3. 3

    Fine grained Spring autowiring not working (@Autowired with additional custom annotation)

  4. 4

    Fine grained Spring autowiring not working (@Autowired with additional custom annotation)

  5. 5

    Spring pure annotation with autowired

  6. 6

    @Size and @Pattern annotation are not working in Spring MVC

  7. 7

    Spring MVC annotation based configuration not working

  8. 8

    Spring autowired annotation with java timertask

  9. 9

    Spring annotation @Autowired inside methods

  10. 10

    Autowired annotation error in spring with java

  11. 11

    Spring: @Autowired not working with ApplicationContext

  12. 12

    TDD test for Spring MVC controller not working correctly

  13. 13

    NoSuchBeanDefinitionException using @Autowired in spring test

  14. 14

    Spring Boot Unit Test Autowired

  15. 15

    @Autowired Spring annotation with struts2

  16. 16

    Spring injects dependencies in constructor without @Autowired annotation

  17. 17

    Nullpoint when using Spring Autowired annotation

  18. 18

    Spring @Configuration annotation @Autowired service is null

  19. 19

    Spring MVC Autowired null in Component

  20. 20

    Spring MVC add bean and autowired

  21. 21

    Autowired is null and not working with Jersey + Spring

  22. 22

    Spring Autowired in Web Service not working

  23. 23

    Spring Autowired service and controller not working

  24. 24

    Spring @Autowired not working in some classes

  25. 25

    Spring @Autowired not working on new thread

  26. 26

    Spring @Autowired and @Value on property not working

  27. 27

    why Spring basic @Autowired is not working?

  28. 28

    Spring @Autowired not working and returning null

  29. 29

    Reflection not working for Spring autowired parameters

HotTag

Archive