0

I try to implement Oauth2 into my project using this blog

I am newbie on Spring framework, so occured an exception such as ClassNotFoundException, altough all comliant classes exist under right package though. Source (maven project) can be seen on github Thank you

Error begins with:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.HierarchicalJsr250Voter] for bean with name 'roleVoter' defined in class path resource [spring/security/security-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.HierarchicalJsr250Voter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.HierarchicalJsr250Voter] for bean with name 'roleVoter' defined in class path resource [spring/security/security-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.HierarchicalJsr250Voter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter] for bean with name 'corsFilter' defined in class path resource [spring/oauth/oauth2-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.OAuthRestEntryPoint] for bean with name 'oauthRestEntryPoint' defined in class path resource [spring/oauth/oauth2-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.OAuthRestEntryPoint
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    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)
webyildirim
  • 587
  • 3
  • 12
  • 32

2 Answers2

1

Make sure you web.xml does initialize spring context correctly: [UPDATE]

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:app-config.xml
    </param-value>
</context-param>

MUST be direct child. Then, in your app-config you can scan for all components using

<context:component-scan base-package="your.component.package.here"/>

Then you import all of your spring config files

<import resource="classpath:your-resource.xml"/>

I also noticed that the PropertiesPlaceholderConfigurer bean was not correctly configured, so I had to do something like this on one of my spring config files, It was throwing exceptions everywhere because spring did not found the file properties:

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"  value="WEB-INF/application.properties"/>`</bean>`

There are some classes that you won't actually need, but that is up to you. Hope you can figure it out with these hints.

Regards

AlbertoRuvel
  • 356
  • 4
  • 14
  • Thank you so much for your response. I guess i have tried your under the light of your informations. But nothing changes. I am importing the spring-business.xml in web xml. https://github.com/webyildirim/trafikhaberci/blob/master/src/main/webapp/WEB-INF/web.xml Then import security config files in business-config.xml . https://github.com/webyildirim/trafikhaberci/blob/master/src/main/resources/spring/business-config.xml Regards. – webyildirim Mar 26 '15 at 12:41
  • Actually, I used that example to implement it in my project, and I have noticed that some components are not correctly defined. – AlbertoRuvel Mar 27 '15 at 02:39
  • Yes, you pointed me to the right direction.But still gives: Err creating bean 'userConfiguration': Injection of autowired dependencies failed; nested exc. 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....UserConfiguration.tokenServices to com.sun.proxy.$Proxy67 – webyildirim Mar 30 '15 at 14:13
  • Well, that exception is thrown cause Spring tries to instantiate your token services class, but, remember it has some dependencies, I guess there are some repositories and the password encoder, Spring must first instantiate those dependencies in order to inject your token services. The password encoder and other services, can be registered as XML beans (The idea is to do not have a lot of XML spring config) – AlbertoRuvel Mar 30 '15 at 20:07
  • Thank you for your response. The problem is solved in this [question](http://stackoverflow.com/questions/29349692/throwing-an-exception-while-implementing-oauth2/29431032#29431032) . Thankyou – webyildirim Apr 03 '15 at 11:10
0

The essential problem regarding the exception was the build path problem. When i moved security-configuration.xml file content into business-config.xml, ide warned me about build path problem. So i checked the build path of project, maven dependecies seem to be unchecked. I had changed version of JDK previously, so i think that caused unchecked situation. Unfortunatelly, noticing took for a while...

webyildirim
  • 587
  • 3
  • 12
  • 32