1
votes

While using spring security for Rest api, beans unable to register using autowired annotation but without spring security is working fine

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>

Error i am facing is

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userBowlingDataController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.class.services.UserBowlingDataService com.class.UserBowlingDataController.userbwldataservice; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.class.services.UserBowlingDataService] 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)}

Please help me in this

1

1 Answers

0
votes

Are there any other XML files in the project? Is there a typo somewhere?

Note the incorrect package name in the exception message (com.classservices instead of com.class.services")

"NoSuchBeanDefinitionException: No qualifying bean of type [com.classservices.UserBowlingDataService] "