Mike
Mike

Reputation: 147

Under what circumstances @Bean excute before @PostConstruct?

i met a urger issue, the anotain @Bean method execute before the @PostConstruct method in the same class,

in spring boot oauth2. there is a class named as

AuthorizationServerEndpointsConfiguration.java

There are 2 methods in it.

@PostConstruct
public void init() {
   xxxx
 }
}
@Bean
public AuthorizationServerTokenServices defaultAuthorizationServerTokenServices() {
   return endpoints.getDefaultAuthorizationServerTokenServices();
}

According to my understanding,the init shuold run before defaultAuthorizationServerTokenServices method, but when i debug it in IDEA, the defaultAuthorizationServerTokenServices before init.

As below config can to reproduce this issue.

@Configuration
@Import({ ClientDetailsServiceConfiguration.class, AuthorizationServerEndpointsConfiguration.class })
class  MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
.....
}

Could you tell me what's the reason?

Thanks,

Upvotes: 0

Views: 242

Answers (1)

Tim
Tim

Reputation: 11

We had this issue when debugging a configurer with @RefreshScope.

When we dropped the RefreshScope lazy loading it appeared to fix the issue

// @RefreshScope
public class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
}

Upvotes: 1

Related Questions