定义了一个Bean,但是无法访问它

时间:2019-03-10 10:27:14

标签: java spring spring-mvc javabeans spring-bean

我对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?启动服务器时不应该声明它吗?

RootConfig:

@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;
    }
}

谢谢

0 个答案:

没有答案