Spring MVC不会在HTTP POST中自动装配,而是在同一控制器上的Ajax中装配

usr-local-ΕΨΗΕΛΩΝ

我有一个奇怪的情况。

我需要管理一个进度递增的上传文件,该文件需要与Ancient.ie版本9配合使用。

上载只能通过提交iframePOST来完成,我们利用Flow js来完成。在上载期间,将发出Ajax期间请求以监视上载进度并在UI上填写指标。

代码

@Scope("request")
@PreAuthorize("hasFunction('DMS')")
@Controller("/secure/admin/pages/dms/fileRepository")
@RequestMapping("/secure/admin/pages/dms/fileRepository")
public class FileRepositoryController extends BaseController
{
    private final static Logger log = LogManager.getLogger();

    @Autowired
    private FileRepositoryManager fileRepositoryManager;
    @Autowired
    private ApplicationEventMulticaster eventDispatcher;

@RequestMapping(value = "/uploadProgress",
            method =
    {
            RequestMethod.POST,
            RequestMethod.GET
    })
    public PhoenixResponse uploadProgress(HttpSession session)
    {

        UploadInfo info = (UploadInfo) session.getAttribute(UploadInfo.SESSION_KEY);

        if (info == null)
        {
            log.warn("Upload info not found in session");
            return info().withSingleton(new UploadInfo());
        }

        PhoenixResponse res;

        switch (info.getStatus())
        {
            case DONE:
            {
                session.removeAttribute(UploadInfo.SESSION_KEY);
                res = success().withAlert("Upload OK", DEFAULT_ALERT_TIMEOUT);
                break;
            }
            case PROGRESS:
            case START:
            case PROCESSING:
            {
                res = info();
                break;
            }
            case ERROR:
            {
                res = error().withAlert("Errore upload");
                break;
            }
            default:
                throw new RuntimeException();
        }

        res = res.withSingleton(info)
                 .withResponseCode(info.getStatus()
                                       .name());

        return res;

    }

    @RequestMapping(path = "/uploadIframe",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.TEXT_HTML_VALUE)
    public final @ResponseBody ResponseEntity<Void> uploadIframe(@RequestParam("file") MultipartFile file, HttpSession session)
    {
        upload(file, session); //Redirect to Ajax-ready method

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/upload",
            method = RequestMethod.POST,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public final PhoenixResponse upload(@RequestParam("file") MultipartFile file, HttpSession session) //Requires modern browser
    {
        UploadInfo info = (UploadInfo) session.getAttribute(UploadInfo.SESSION_KEY);

        log.debug("Upload request received");

        FileUploadDTO result;

        if (file != null && !file.isEmpty())
        {

            String uploadFileName = IOUtils.getFileSafeName(file.getOriginalFilename());
            log.debug("upload file: " + uploadFileName + " user: " + getUsername());
            try
            {
                FileDescriptor fd;
                try (InputStream is = file.getInputStream())
                {
                    fd = fileRepositoryManager.store(is, uploadFileName, AdminModule.ID, getUsername());
                }
                result = new FileUploadDTO(fd.getName(), fd.getLength(), fd.getMimeType(), fd.getId());
                eventDispatcher.multicastEvent(new FileUploadEvent(this, fd));
            }
            catch (IOException | RuntimeException ex)
            {
                info.setStatus(UploadState.ERROR);
                log.error("Error uploading file", ex);
                return error().withAlert(ex.getMessage());
            }
        }
        else
            return error();

        if (info != null)
            info.setStatus(UploadState.DONE);

        return info().withSingleton(result);
    }

}

说明:我们有两种上传方法。一个支持Ajax,另一个响应POST请求。实际上,该uplodaIframe方法返回经典的200空白页。但是由于代码是相同的,所以它将调用重定向到Ajax-ready方法,该方法返回未使用的响应对象

问题

调用时/uploadProgress,尽管它不使用任何@Autowiredbean,但已设置了所有自动装配。

调用时,/uploadIframe所有bean都为空,尽管required=false它们的自动装配上没有

我还在堆栈跟踪中发现了一个有趣的区别:

/ uploadProgress

FileRepositoryController.uploadProgress(HttpSession) line: 106  
FileRepositoryController$$FastClassByCGLIB$$7ccffed3.invoke(int, Object, Object[]) line: not available  
MethodProxy.invoke(Object, Object[]) line: 204  
CglibAopProxy$CglibMethodInvocation.invokeJoinpoint() line: 720 
CglibAopProxy$CglibMethodInvocation(ReflectiveMethodInvocation).proceed() line: 157 
MethodSecurityInterceptor.invoke(MethodInvocation) line: 68 
CglibAopProxy$CglibMethodInvocation(ReflectiveMethodInvocation).proceed() line: 179 
CglibAopProxy$DynamicAdvisedInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 655  
FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2.uploadProgress(HttpSession) line: not available 
GeneratedMethodAccessor1992.invoke(Object, Object[]) line: not available    
Method.invoke(Object, Object...) line: 606  

/ uploadIframe

FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2(FileRepositoryController).upload(MultipartFile, HttpSession) line: 144  
FileRepositoryController$$EnhancerBySpringCGLIB$$8768d018_2(FileRepositoryController).uploadIframe(MultipartFile, HttpSession) line: 118    
GeneratedMethodAccessor1993.invoke(Object, Object[]) line: not available    
Method.invoke(Object, Object...) line: 606  
usr-local-ΕΨΗΕΛΩΝ

我不完全理解为什么,但是问题与final两种方法修饰符有关。

卸下后final,豆子自动接线正确。@ M.Deinum的荣誉

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

CSS 不会在 Spring MVC Webapp 中更新

即使我添加了新的 MVC 类,Beans 也不会在 Spring Elements 中更新

Spring MVC 3.0.0应用程序不会在Weblogic Server 11gR1上引导-ClassCastException

在Spring控制器中自动装配HttpServletRequest

Hibernate不会在Spring Boot Project中引发LazyInitializationException

Spring Boot 不会在 Postgres 中创建表

在MVC控制器中,自动装配的Bean为空

从JSF控制器自动装配Spring Bean

视图不会在 MVC 中返回模型数据

watchTermination 不会在 akka-http 流中触发

RabbitTemplate不会在旧版Spring的旧项目中自动布线

Spring boot不会在Mysql上自动创建数据库表

Spring Boot父模块不会在子模块上更改时更新

Spring boot 不会在 CURL 结果上添加新实体

Hibernate Validation不会在Spring Boot应用程序中查找错误消息

添加依赖项后,spring-cloud-sleuth不会在日志中显示任何信息

JPA左联接查询不会在Spring Boot中实例化子集合吗?

Aspectj不会在Spring框架中捕获所有事件吗?

带有 RabbitMQ 的 Spring Cloud Zipkin 不会在 MYSQL 中持久化

JSP不会在Spring Boot 2中呈现Java列表

Spring Boot Jackson 不会在 Long 中序列化时间戳

Spring-验证程序不会在集成测试中触发

Spring Batch 不会在块流中同时调用 ItemProcessor 和 ItemWriter

Spring JPA:变化不会在 OneToOne 关系中持续存在

angularjs $ http服务不会在POST上设置cookie,但会在GET上设置cookie

Spring MVC - 我可以在 RestController 中自动装配 HttpServletRequest

同一控制器的多个Spring MVC验证器

CodeIgniter不会在'/'上加载默认控制器

Symfony不会在控制器上缓存更改