我正在尝试做一个图像上传API。我有一个ImageUpload任务如下,
@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
}
}ImageUploadService看起来如下所示,
@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;
}
}以下是spring引导应用程序类,
@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);
}
}补充资料:
pom.xml为spring-aspects和spring-instrument添加了依赖项我得到了NullPointerException在ImageUploadTask。我怀疑@Autowired没有像预期的那样工作。
@Autowired时才必须使用@Conigurable,为什么不使用@Inject呢?(虽然我试过了,也得到了同样的NPE)发布于 2016-12-07 06:53:03
默认情况下,@Configurable的自动装配为off,即Autowire.NO信标,因为imageUploadService是null
因此,将代码更新为可解释性,将其启用为名字或类型,如下所示。
@Component
@Configurable(preConstruction = true, autowire = Autowire.BY_NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ImageUploadTask implements Callable<JSONObject> { .... }配置的其余部分即。启用加载时间编织似乎很好。
另外,对于@Inject注释,请看一下这里,这很大程度上解释了差异(或者相似)。
https://stackoverflow.com/questions/41008920
复制相似问题