首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Autowired不适用于@Configurable

@Autowired不适用于@Configurable
EN

Stack Overflow用户
提问于 2016-12-07 03:29:12
回答 1查看 4.3K关注 0票数 0

我正在尝试做一个图像上传API。我有一个ImageUpload任务如下,

代码语言:javascript
复制
@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看起来如下所示,

代码语言:javascript
复制
@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引导应用程序类,

代码语言:javascript
复制
@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);
 }
}

补充资料:

  • 依赖项的春季版本为4.2.5。
  • pom.xmlspring-aspectsspring-instrument添加了依赖项

我得到了NullPointerExceptionImageUploadTask。我怀疑@Autowired没有像预期的那样工作。

  • 为什么不能工作,我怎么解决这个问题?
  • 只有在我使用@Autowired时才必须使用@Conigurable,为什么不使用@Inject呢?(虽然我试过了,也得到了同样的NPE)
EN

回答 1

Stack Overflow用户

发布于 2016-12-07 06:53:03

默认情况下,@Configurable的自动装配为off,即Autowire.NO信标,因为imageUploadServicenull

因此,将代码更新为可解释性,将其启用为名字类型,如下所示。

代码语言:javascript
复制
@Component
@Configurable(preConstruction = true, autowire = Autowire.BY_NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ImageUploadTask implements Callable<JSONObject> { .... }

配置的其余部分即。启用加载时间编织似乎很好。

另外,对于@Inject注释,请看一下这里,这很大程度上解释了差异(或者相似)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41008920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档