如何使用依赖注入在 Spring 中注入资源 Class 实例
How to inject resource Class instance in Spring using Dependency injection
我是 Spring 的新手。有一个例子,我写了一个 Class 来实现 AutoCloseable 接口。现在我想用它作为依赖注入。
我担心的是,如果我使用 @Autowired 并稍后在函数中使用它,会 Spring 在结束范围或任何异常后自动关闭资源对象吗?
@RestController
@RequestMapping("/rest/profile")
public class ProfileController {
private Daws haws;
@Autowired
public ProfileController(Daws haws) {
this.haws = haws;
}
@RequestMapping(value = "/images/{userId}/{fileName:.+}", method = RequestMethod.GET)
public void image(@PathVariable Integer userId, @PathVariable String publicUrl, @PathVariable String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
try{
S3Object image = haws.getProfileImage(userId, fileName, request);
response.setContentType(image.getObjectMetadata().getContentType());
response.setHeader("ETag",image.getObjectMetadata().getETag());
response.setHeader("Cache-Control",image.getObjectMetadata().getCacheControl());
response.setHeader("Last-Modified",image.getObjectMetadata().getLastModified().toString());
IOUtils.copy(image.getObjectContent(), response.getOutputStream());
}catch (Exception e) {
if(e instanceof AmazonS3Exception){
//....
//....
response.setStatus(statusCode);
}
}
}
//Daws class
public class Daws implements AutoCloseable{
public S3Object getProfileImage(int userId, String fileName, HttpServletRequest request) throws IOException, ParseException, AmazonS3Exception{
S3Object image = ....;
return image;
}
@Override
public void close() throws Exception {
// TODO Auto-generated method stub
}
}
我现在就是这样做的。请告诉我它是否正常或资源泄漏。如果是,那我该怎么办?
对于 Spring 托管 bean,您可以实现 DisposableBean interface or use @PreDestroy 注释。 Spring 应用上下文被销毁时会调用destroy方法
如果您需要在每次方法调用时创建和关闭对象,您应该使用 try-with-resources
如果您不使用 try-with-resources
或明确调用 close()
,AutoCloseable 将永远不会被 Spring 关闭
void close()
throws Exception
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
来自 javadoc
我是 Spring 的新手。有一个例子,我写了一个 Class 来实现 AutoCloseable 接口。现在我想用它作为依赖注入。
我担心的是,如果我使用 @Autowired 并稍后在函数中使用它,会 Spring 在结束范围或任何异常后自动关闭资源对象吗?
@RestController
@RequestMapping("/rest/profile")
public class ProfileController {
private Daws haws;
@Autowired
public ProfileController(Daws haws) {
this.haws = haws;
}
@RequestMapping(value = "/images/{userId}/{fileName:.+}", method = RequestMethod.GET)
public void image(@PathVariable Integer userId, @PathVariable String publicUrl, @PathVariable String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
try{
S3Object image = haws.getProfileImage(userId, fileName, request);
response.setContentType(image.getObjectMetadata().getContentType());
response.setHeader("ETag",image.getObjectMetadata().getETag());
response.setHeader("Cache-Control",image.getObjectMetadata().getCacheControl());
response.setHeader("Last-Modified",image.getObjectMetadata().getLastModified().toString());
IOUtils.copy(image.getObjectContent(), response.getOutputStream());
}catch (Exception e) {
if(e instanceof AmazonS3Exception){
//....
//....
response.setStatus(statusCode);
}
}
}
//Daws class
public class Daws implements AutoCloseable{
public S3Object getProfileImage(int userId, String fileName, HttpServletRequest request) throws IOException, ParseException, AmazonS3Exception{
S3Object image = ....;
return image;
}
@Override
public void close() throws Exception {
// TODO Auto-generated method stub
}
}
我现在就是这样做的。请告诉我它是否正常或资源泄漏。如果是,那我该怎么办?
对于 Spring 托管 bean,您可以实现 DisposableBean interface or use @PreDestroy 注释。 Spring 应用上下文被销毁时会调用destroy方法
如果您需要在每次方法调用时创建和关闭对象,您应该使用 try-with-resources
如果您不使用 try-with-resources
或明确调用 close()
void close() throws Exception Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
来自 javadoc