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; } }
Advertisement
Answer
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");