Search code examples
javaspringspring-annotations

UserDetailsService implemented class is not injecting autowired field


i'm using acegi authentication for login . when i try to compare the values from databade i'm using the following class

 @Transactional(readOnly = true)
    @Service
    public class CustomUserDetailsService implements UserDetailsService {
        @Autowired
        private UserDAO userdao;
    public UserDetails loadUserByUsername(String username)
                throws UsernameNotFoundException, DataAccessException {
com.quest.model.dto.User dbUser = userdao.searchDatabase(username);
    }
    }

but the autowired field is not initializing. But it initialized in the controller class. This class is mapped through spring-security 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:security="http://www.springframework.org/schema/security"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/security 
                http://www.springframework.org/schema/security/spring-security.xsd
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- This is where we configure Spring-Security  -->
        <!-- <context:annotation-config />
        <context:component-scan base-package="com.quest" /> 
        <mvc:annotation-driven /> --> 
        <security:http auto-config="true" use-expressions="true" access-denied-page="/auth/denied" >

            <security:intercept-url pattern="/auth/home" access="permitAll"/>
            <security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')"/>
            <security:intercept-url pattern="/student/*" access="hasRole('ROLE_STUDENT')"/>
            <security:intercept-url pattern="/control/*" access="hasRole('ROLE_TEACHER')"/>

            <!-- <security:form-login
                    login-page="/auth/home" 
                    authentication-failure-url="/auth/home?error=true" 
                    default-target-url="/control/dummyLogin"/> -->
            <security:form-login
                    login-page="/auth/home" 
                    authentication-failure-url="/auth/home?error=true" 
                    default-target-url="/student/student"/>

            <security:logout 
                    invalidate-session="true" 
                    logout-success-url="/auth/home" 
                    logout-url="/student/logout"/>

        </security:http>

        <!-- Declare an authentication-manager to use a custom userDetailsService -->
        <security:authentication-manager>
                <security:authentication-provider user-service-ref="customUserDetailsService">
                        <security:password-encoder ref="passwordEncoder"/>
                </security:authentication-provider>
        </security:authentication-manager>

        <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
        <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>

        <!-- A custom service where Spring will retrieve users and their corresponding access levels  -->
        <bean id="customUserDetailsService" class="com.quest.service.CustomUserDetailsService"/>


    </beans>

this is spring-servlrt.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
    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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ws="http://jax-ws.dev.java.net/spring/core"   
    xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd
        http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd">


    <context:annotation-config />
    <context:component-scan base-package="com.quest.*" />   
    <mvc:annotation-driven /> 
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
      <property name="baseAddress" value="http://localhost:9999/"/>
    </bean>

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.quest.model.dto</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.configurationClass">org.hibernate.cfg.AnnotationConfiguration</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager" depends-on="sessionFactory">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

<!--    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> !-->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>


     <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.quest.data.converter.HibernateAwareObjectMapper" />
                </property>
            </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="200000" />
    </bean>

    <bean id="seedDataInstaller" 
        class="com.quest.model.SeedData"  init-method="populateSeed">
    </bean>

</beans>

and the userDAO looks like

package com.quest.model.dao; @Repository("userdao") public class UserDAO { ...}

Solution

  • No bean named 'userdao' is defined

    you have to define either UserDao in xml configuration as a bean or annotate UserDao class with @component

    if you are following the annotation based config then please uncomment the following portion in your config xml

    <!-- 
    <context:annotation-config />
    <context:component-scan base-package="com.quest.*>" /> 
    <mvc:annotation-driven /> 
    -->