天天看点

springboot自定义类@autowire注入为null的问题

@Component
public class MyFtpPlet extends DefaultFtplet {

    private static Logger logger = LoggerFactory.getLogger(MyFtpServer.class);

    @Autowired
    private ResUploadedService resUploadedService;

    private static MyFtpPlet myFtpPlet;

    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {
        myFtpPlet = this;
        myFtpPlet.resUploadedService = this.resUploadedService;
    }      
@Override
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
        throws FtpException, IOException {

    ResUploaded resUploaded = new ResUploaded();
    resUploaded.setUuid(UUIDUtil.getUuid());
    resUploaded.setFileName(fileName);
    resUploaded.setUploadTime(new Date());
    resUploaded.setFileDir(fileDir);
    resUploaded.setFilePath(rootPath.replaceAll("\\\\", "/"));
    resUploaded.setFileRepath(filePath);
    resUploaded.setStatus(0);
    myFtpPlet.resUploadedService.recordUploadInfo(resUploaded);
    logger.info("UUID:" + resUploaded.getUuid() + "FileName:" + fileName + "记录文件信息成功!");
    return super.onUploadEnd(session, request);
}      

}

需要注意:注入类的调用方法是

myFtpPlet.resUploadedService.recordUploadInfo(resUploaded);

目前发现除了带有@Controller @Service @Configuration 可以自动注入成功 

其他的自定义普通类只能 已这种方式去调用!

继续阅读