I am using Spring Boot 1.3.5.RELEASE & Spring Cloud Brixton BR7 to create an OAuth Server that has TokenStore using a database with JPA Repository. They work perfectly!
Today I upgrade to Spring Boot 1.4.3.RELEASE & Spring Clound Camden SR3 and I cannot start the Spring boot application.
It seems that Auth Server config class is run before JPA Repository config.
Do you have any ideas? Thank you very much.
My AuthorizationServerConfig looks like:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {// @formatter:off
clients
.jdbc(dataSource()) ;
} // @formatter:on
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore())
.authenticationManager(authenticationManager)
.userDetailsService(userService);
// @formatter:on
}
@Autowired
private UserDetailsService userService;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
// JDBC token store configuration
@Bean
@ConfigurationProperties(prefix = "oauth2.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource());
}
}
UserDetailsService looks like
@Service("userService")
@Transactional
public class UserServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
logger.debug("Finding User by username: " + username);
User user = userRepository.findByUsername(username);
...
return new DefaultUserDetails(user);
}
}
The exception is
2016-12-26 17:41:10.691 INFO 1156 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-12-26 17:41:10.868 WARN 1156 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AuthorizationServerConfig': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#503c3100' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#503c3100': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
2016-12-26 17:41:10.874 INFO 1156 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service Tomcat
2016-12-26 17:41:11.140 WARN 1156 --- [ restartedMain] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]: Factory method 'transactionAdvisor' threw exception; nested exception is java.lang.NullPointerException)
2016-12-26 17:41:11.149 ERROR 1156 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
APPLICATION FAILED TO START
Description:
The dependencies of some of the beans in the application context form a cycle:
|
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
↑ ↓
| OAuth2ServerConfig (field private org.springframework.security.core.userdetails.UserDetailsService com.oauthserver.config.AuthorizationServerConfig.userService)
↑ ↓
| userService (field private com.oauthserver.repository.user.UserRepository com.oauthserver.service.impl.UserServiceImpl.userRepository)
↑ ↓
| userRepository
↑ ↓
| (inner bean)#503c3100
↑ ↓
| entityManagerFactory
└─────┘
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS