7

カスタムコンバーターがあります:

  @Component
public class RoleConverter implements Converter<String, Role> {

    @Autowired private Roles roles;

    @Override
    public Role convert(String id) {
        return roles.findById(Long.parseLong(id));
    }
}

しかし、@Autowired は null 値を設定しています。原因Nullpointerexception

これは Roles クラスです:

@Repository
@Transactional
public class Roles extends Domain<Role>{

    public Roles() {
        super(Role.class);
    }

}

Java構成を使用しています。コンバーターが登録されています:

@Configuration
@EnableWebMvc
//other annotations...
public class WebappConfig extends WebMvcConfigurerAdapter {
//....


    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new RoleConverter());
        super.addFormatters(registry);
    }


/....

}

コントローラーで @Autowired Roles を実行すると、機能します。

@Autowired が Converter で null を設定するのはなぜですか?

4

1 に答える 1