0

我对 Spring 中的 Bean 定义有点困惑。要恢复,我需要一个 Keycloak Bean 来提供服务。

我正在像这样在 RootConfig.java 上创建它:

@Bean
    public Keycloak keycloak(){
        Keycloak keycloak = KeycloakBuilder
                .builder()
                .serverUrl(KEYCLOAK_AUTH_SERVER_URL)
                .realm(KEYCLOAK_REALM)
                .clientId(KEYCLOAK_RESOURCE)
                .clientSecret(KEYCLOAK_CREDENTIALS_SECRET)
                .grantType(OAuth2Constants.CLIENT_CREDENTIALS)
                .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(KEYCLOAK_REST_CLIENT_POOL_SIZE).build())
                .build();
        return keycloak;
    }

然后在我的服务上,我想以这种方式使用它:

@Autowired
    private Keycloak keycloak;

但是我在启动我的项目时遇到了这个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationManagementServiceImpl' defined in VFS resource ["/service/ApplicationManagementServiceImpl.class\"]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
    Caused by: java.lang.ExceptionInInitializerError
    Caused by: java.lang.IllegalArgumentException: Keycloak instance required"}}

如果有人能告诉我为什么我不能访问那个 bean?启动服务器时不应该声明吗?

根配置:

@Configuration
@ComponentScan(basePackages = {
        "xxx.config",
        "xxx.controller",
        "xxx.service",})
@PropertySource("classpath:application.properties")
@Import(SecurityConfig.class)
public class RootConfig {


    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public MessageSource messageSource()
    {

    }

    @Bean
    public LocaleResolver localeResolver() {

    }

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {

    }

    @Bean
    public Keycloak keycloak(){
        Keycloak keycloak = KeycloakBuilder
                .builder()
                .serverUrl(KEYCLOAK_AUTH_SERVER_URL)
                .realm(KEYCLOAK_REALM)
                .clientId(KEYCLOAK_RESOURCE)
                .clientSecret(KEYCLOAK_CREDENTIALS_SECRET)
                .grantType(OAuth2Constants.CLIENT_CREDENTIALS)
                .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(KEYCLOAK_REST_CLIENT_POOL_SIZE).build())
                .build();
        return keycloak;
    }
}

我的服务:

@Service
public class ApplicationManagementServiceImpl implements ApplicationManagementService{


    //Variable def

    @Autowired
    private Keycloak keycloak;

    private PaymentServiceProviderClientRepresentationRepository repository = new PaymentServiceProviderClientRepresentationRepositoryImpl(keycloak, KEYCLOAK_REALM_TARGET);


    @Override
    public void createApp(String appName, String registrationId, String clientRootUrl, String baseUrl, String clientRedirectUri) {

        //myStuff

    }

    public Keycloak getKeycloak() {
        return keycloak;
    }

    public void setKeycloak(Keycloak keycloak) {
        this.keycloak = keycloak;
    }
}

谢谢

4

0 回答 0