title: Throwing an exception while implementing oauth2
body: <p>I am trying to implement Oauth2 into a spring hibernate project on Eclipse STS 3.6.2. During deployment faced an exception. Project source code is <a href="https://github.com/webyildirim/trafikhaberci" rel="nofollow">here</a> . What might be the problem?
  Regards</p>
  
  <p>spring-config.xml:</p>
  
  <pre><code>    &lt;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"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:security="http://www.springframework.org/schema/security"
             xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.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/tx
              http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/security
              http://www.springframework.org/schema/security/spring-security.xsd"&gt;
  
          &lt;context:component-scan base-package="com.structure"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.core.services.impl"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.rest.filter"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.rest.configuration"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.rest.resource"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.rest.api.user"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.rest.mvc"/&gt;
          &lt;context:component-scan base-package="com.trafficalarm.core.repositories.jpa"/&gt;
  
          &lt;tx:annotation-driven/&gt;
  
          &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
              &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
              &lt;property name="username" value="root" /&gt;
              &lt;property name="password" value=""/&gt;
          &lt;/bean&gt;
              &lt;!--bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"--&gt;
              &lt;!--property name="username" value="sa"/--&gt;
              &lt;!--property name="password" value="sa"/--&gt;
              &lt;!--property name="url" value="jdbc:h2:mem:test"/--&gt;
              &lt;!--property name="driverClassName" value="org.h2.Driver"/--&gt;
              &lt;!--/bean--&gt;
  
  
          &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="dataSource" ref="dataSource"/&gt;
              &lt;property name="jpaVendorAdapter"&gt;
                  &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
              &lt;/property&gt;
              &lt;property name="jpaProperties"&gt;
                  &lt;map&gt;
                      &lt;entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/&gt;&lt;!-- remove when working with mem db--&gt;
                      &lt;entry key="hibernate.hbm2ddl.auto" value="update"/&gt;
                      &lt;entry key="hibernate.show_sql" value="true"/&gt;
                  &lt;/map&gt;
              &lt;/property&gt;
              &lt;property name="packagesToScan" value="com.trafficalarm.core.model.entities"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/&gt;
  
          &lt;bean id="propertyPlaceholderConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
              &lt;property name="location"  value="WEB-INF/classes/properties/app-production.properties"/&gt;
          &lt;/bean&gt;
  
          &lt;!--mvc:resources mapping="/app/**" location="/app/build/"/&gt;
  
          &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
              &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
              &lt;property name="suffix" value=".jsp"/&gt;
          &lt;/bean&gt;
  
          &lt;security:global-method-security pre-post-annotations="enabled"&gt;
              &lt;security:protect-pointcut expression="execution(* com.trafficalarm.rest.mvc.*.*(..))"
                                         access="ROLE_DUMMY"/&gt;
          &lt;/security:global-method-security&gt; --&gt;
  
          &lt;mvc:annotation-driven /&gt;
          &lt;mvc:default-servlet-handler/&gt;
          &lt;import resource="security/security-configuration.xml"/&gt;
          &lt;import resource="security/authentication-managers.xml"/&gt;
          &lt;import resource="security/endpoint-configuration.xml"/&gt;
          &lt;import resource="oauth/oauth2-configuration.xml"/&gt;
          &lt;import resource="oauth/token-store.xml"/&gt;
          &lt;import resource="oauth/client-details.xml"/&gt;
      &lt;/beans&gt;
  
  oauth2-configuration.xml:
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
         xmlns:sec="http://www.springframework.org/schema/security"
  
         xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/security/oauth2
                        http://www.springframework.org/schema/security/spring-security-oauth2.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security.xsd"&gt;
  
  
      &lt;oauth:authorization-server client-details-service-ref="client-details-service" token-services-ref="tokenServices"&gt;
          &lt;oauth:refresh-token/&gt;
          &lt;oauth:password/&gt;
      &lt;/oauth:authorization-server&gt;
  
      &lt;oauth:resource-server id="resourceServerFilter" token-services-ref="tokenServices"/&gt;
  
      &lt;bean id="clientCredentialsTokenEndpointFilter"
            class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"&gt;
          &lt;property name="authenticationManager" ref="clientAuthenticationManager"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="clientAuthenticationEntryPoint"
            class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
          &lt;property name="typeName" value="Basic"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="oauthAuthenticationEntryPoint"
            class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
          &lt;property name="realmName" value="test"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="oauthAccessDeniedHandler"
            class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/&gt;
  
  
      &lt;bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"&gt;
          &lt;property name="tokenStore" ref="tokenStore"/&gt;
          &lt;property name="supportRefreshToken" value="true"/&gt;
          &lt;property name="clientDetailsService" ref="client-details-service"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="corsFilter" class="com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter"/&gt;
  
      &lt;bean id="oauthRestEntryPoint" class="com.trafficalarm.rest.security.OAuthRestEntryPoint"/&gt;
  
      &lt;!-- The token endpoint See org.springframework.security.oauth2.provider.endpoint.TokenEndpoint--&gt;
      &lt;http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"
            xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;anonymous enabled="false"/&gt;
          &lt;http-basic entry-point-ref="oauthRestEntryPoint" /&gt;
          &lt;access-denied-handler ref="oauthAccessDeniedHandler"/&gt;
          &lt;custom-filter ref="corsFilter" after="LAST"/&gt;
      &lt;/http&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And here is the exception that is occured:</p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.oauth2.provider.token.DefaultTokenServices com.trafficalarm.rest.configuration.UserConfiguration.tokenServices; nested exception is java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
      at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:581)
      at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1686)
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.oauth2.provider.token.DefaultTokenServices com.trafficalarm.rest.configuration.UserConfiguration.tokenServices; nested exception is java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
      ... 26 more
  Caused by: java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
      at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
      at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
      at java.lang.reflect.Field.set(Unknown Source)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
      ... 28 more
  </code></pre>