Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.0 inject files as resources

In my Spring 3.0 app, I have some resources in /WEB-INF/dir. At runtime I need some of them as an InputStream (or some other type). How can I retrieve them? Is it possible to inject them as a normal Resource?

like image 228
Randomize Avatar asked Sep 20 '11 11:09

Randomize


People also ask

What is difference between @autowired and @resource in Spring?

The main difference is is that @Autowired is a spring annotation whereas @Resource is specified by the JSR-250. So the latter is part of normal java where as @Autowired is only available by spring.

What is difference between @inject and @autowired?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application. Sr.

Why field injection is not recommended?

The reasons why field injection is frowned upon are as follows: You cannot create immutable objects, as you can with constructor injection. Your classes have tight coupling with your DI container and cannot be used outside of it. Your classes cannot be instantiated (for example in unit tests) without reflection.


1 Answers

Here is an easiest way to do it via annotation:

import org.springframework.core.io.Resource;  @Value("classpath:<path to file>") private Resource cert; 
like image 137
Nikita Koksharov Avatar answered Oct 12 '22 23:10

Nikita Koksharov