Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent for Springs Resource in Micronaut?

Tags:

java

micronaut

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; }
}
like image 425
saw303 Avatar asked Dec 10 '22 04:12

saw303


1 Answers

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");
like image 66
Álvaro Sánchez-Mariscal Avatar answered Jan 17 '23 08:01

Álvaro Sánchez-Mariscal