J'essaie de faire une API de téléchargement d'images. J'ai une tâche ImageUpload
comme suit,@Autowired ne fonctionne pas avec @Configurable
@Component
@Configurable(preConstruction = true)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ImageUploadTask implements Callable<JSONObject> {
@Autowired
private ImageUploadService imageUploadService;
@Override
public JSONObject call() throws Exception {
....
//Upload image via `imageUploadService`
imageUploadService.getService().path('...').post('...'); // Getting null pointer here for imageUploadService which is a WebTarget
}
}
Le ImageUploadService
ressemble au-dessous,
@Component
public class ImageUploadService {
@Inject
@EndPoint(name="imageservice") //Custom annotation, battle tested and works well for all other services
private WebTarget imageservice;
public WebTarget getService() {
return imageservice;
}
}
Voici la classe d'application de démarrage de printemps,
@ComponentScan
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving=EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
@EnableAutoConfiguration
public class ImageApplication extends SpringBootServletInitializer {
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new RequestContextListener());
}
public static void main(String[] args) throws IOException {
SpringApplication.run(ImageApplication.class);
}
}
Informations complémentaires:
- version printemps dépendances sont à 4.2.5.RELEASE
pom.xml
a ajouté des dépendances pourspring-aspects
etspring-instrument
Je reçois un NullPointerException
en ImageUploadTask
. Mon soupçon est que @Autowired
ne fonctionne pas comme prévu.
- Pourquoi ne pas travailler et comment résoudre ce problème?
- Est-il obligatoire d'utiliser
@Autowired
uniquement lorsque j'utilise@Conigurable
, pourquoi ne pas utiliser@Inject
? (Bien que je l'ai essayé et obtenir même NPE)
où utilisez-vous le ImageUploadTask? – chaoluo