Spring Security .DaoAuthenticationProvider:无法解析对bean的引用

时间:2014-03-30 17:15:30

标签: java spring hibernate spring-mvc spring-security

我正在项目中实现Spring Security。从几小时开始,我就陷入了僵局。

我收到此错误

Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'UserDAOImpl' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailService' is defined

项目设置非常简单

弹簧security.xml文件

<authentication-manager>
        <authentication-provider user-service-ref="myUserDetailService">
      </authentication-provider>
    </authentication-manager>

调度-servlet.xml中

<context:component-scan base-package="app.com,app.com.controller,app.com.dao,app.com.service,app.com.model"/>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
            p:basename="messages"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

的applicationContext.xml

  <context:annotation-config />

    <!-- <context:property-placeholder> XML element automatically registers a new PropertyPlaceholderConfigurer 
    bean in the Spring Context. -->
    <context:property-placeholder location="classpath:database.properties" />

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> 

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

    <!-- To persist the object to database, the instance of SessionFactory interface is created. 
SessionFactory is a singleton instance which implements Factory design pattern. 
SessionFactory loads hibernate.cfg.xml and with the help of TransactionFactory and ConnectionProvider 
implements all the configuration settings on a database. -->

<!-- Configuring SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>app.com.model.User</value>
                <value>app.com.model.Roles</value>
                <value>app.com.BaseEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

<!-- Configuring Hibernate Transaction Manager -->
    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    </beans>

CustomUserDetailsS​​ervice.java

@Service("myUserDetailService")
@Transactional
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDAO userDAO;

    /*@Override
    public UserDetails loadUserByUsername(String arg0)
            throws UsernameNotFoundException, DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

}

我还尝试在dispatcher-servlet.xml和amp;中声明bean。 application-context.xml它不起作用

检查了context-component基础包。它扫描所有其他课程就好了。当我从身份验证提供程序中删除myUserDetailService时,服务器启动就没有任何错误。

我真的很累。任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

phew ......让它运转起来。

已移动

<context:component-scan base-package="app.com,app.com.controller,app.com.dao,app.com.service,app.com.model"/>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
            p:basename="messages"/>

从dispatcher-servlet到application-context.xml

有人会关心它为什么开始工作吗?

答案 1 :(得分:0)

在将定义移动到上下文文件之后将其工作的原因是因为在Spring中,调度程序servlet中的定义仅对mvc可见,并且上下文中的定义是全局的(对于servlet和安全性),这里是一个清楚解释https://weblogs.java.net/blog/sgdev-blog/archive/2014/07/05/common-mistakes-when-using-spring-mvc

的页面