Is there an equivalent for Springs Resource in Micronaut?

时光怂恿深爱的人放手 提交于 2020-01-04 05:42:29

问题


I am migrating a tiny Spring Boot application to Micronaut 1.0.1 and I was wondering if there is an equivalent for org.springframework.core.io.Resource and their implementation such as ClasspathResource?

In Spring Boot I inject a resource into a service using its constructor.

@Service
public class MyService() {

    private final Resource resource;

    public MyService(Resource resource) { this.resource = resource; }
}

How can I do this in Micronaut?

@Singleton
public class MyService() {

    private final Resource resource;

    @Inject
    public MyService(Resource resource) { this.resource = resource; }
}

回答1:


In Micronaut you can use io.micronaut.core.io.ResourceLoader variants, such as io.micronaut.core.io.scan.ClassPathResourceLoader or io.micronaut.core.io.file.FileSystemResourceLoader. One option to get them is via io.micronaut.core.io.ResourceResolver:

ClassPathResourceLoader loader = new ResourceResolver().getLoader(ClassPathResourceLoader.class).get();
Optional<URL> resource = loader.getResource("classpath:foo/bar.txt");


来源:https://stackoverflow.com/questions/53370135/is-there-an-equivalent-for-springs-resource-in-micronaut

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!