用户bean在使用spring security时不会创建

时间:2014-11-11 20:06:34

标签: spring rest spring-mvc spring-security

使用Spring api的spring安全性时,无法使用自动装配的注释注册但没有弹簧安全性的bean工作正常

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->


<!-- Spring root -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
     org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>   /WEB-INF/appServlet-servlet.xml
    com.class.spring</param-value>
</context-param>
<context:annotation-driven />

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml

            </param-value>
    </init-param> -->
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>




<!-- <context:annotation-driven/>
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
     org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param> -->
<!-- Processes application requests -->

<!-- Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

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

   <mvc:annotation-driven />
   <mvc:default-servlet-handler/>
   <mvc:resources mapping="/resources/**" location="/resources/" />
     <!-- Resolves views selected for rendering by @Controllers to .jsp resources        in      the /WEB-INF/views directory -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
 </bean>
<context:component-scan base-package="com.project" />

<bean id="dataSource"  
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
 <property name="url" value="jdbc:mysql://localhost:3307/project_db" />  
 <property name="username" value="root" />  
 <property name="password" value="mani" />  
</bean>  

 <bean id="sessionFactory"  
 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
 <property name="dataSource" ref="dataSource" />  
 <property name="mappingResources">
        <list>
            <value>UserDetails.hbm.xml</value>
            <value>UserBowlingData.hbm.xml</value>
        </list>
    </property> 
 <property name="hibernateProperties">  
 <props>  
   <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
   <prop key="hibernate.show_sql">true</prop>  
 </props>  
 </property>  
</bean>  
 <bean id="txManager"  
 class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
<property name="sessionFactory" ref="sessionFactory" />  
</bean>  

 <bean id="persistenceExceptionTranslationPostProcessor"  
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />       


<!--bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean-->
<bean id="userDetailsDaoImpl" class="com.class.dao.UserDetailsDaoImpl" />  
<bean id="userDetailsServiceImpl" class="com.class.services.UserDetailsServicImpl" />

<bean id="userBowlingDataDaoImpl" class="com.class.dao.UserBowlingDataDaoImpl" />
<bean id="userBowlingDataService" class="com.class.services.UserBowlingDataServiceImpl" /> 


<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean> 



<http use-expressions="true" entry-point-ref="restAuthenticationEntryPoint">
    <intercept-url pattern="/appServlet/**" access="isAuthenticated()" />

    <sec:form-login authentication-success-handler-ref="mySuccessHandler" authentication-failure-handler-ref="myFailureHandler" />

    <logout />
</http>

<beans:bean id="mySuccessHandler" class="com.project.security.MySavedRequestAwareAuthenticationSuccessHandler" />
<beans:bean id="myFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler" />

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="temporary" password="temporary" authorities="ROLE_ADMIN" />
            <user name="user" password="userPass" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

我面临的错误是

org.springframework.beans.factory.BeanCreationException:创建名为'userBowlingDataController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.class.services.UserBowlingDataService com.class.UserBowlingDataController.userbwldataservice;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.class.services.UserBowlingDataService]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

请帮助我

1 个答案:

答案 0 :(得分:0)

项目中是否还有其他XML文件?某处有错字吗?

请注意异常消息中的错误包名称(com.classservices而不是com.class.services&#34;)

&#34; NoSuchBeanDefinitionException:没有[com.classservices.UserBowlingDataService]类型的限定bean&#34;