简体   繁体   中英

Inject a file resource into Spring bean

What is a good way to inject some file resource into Spring bean ? Now i autowire ServletContext and use like below. Is more elegant way to do that in Spring MVC ?

@Controller
public class SomeController {

    @Autowired
    private ServletContext servletContext;

    @RequestMapping("/texts")
    public ModelAndView texts() {
        InputStream in = servletContext.getResourceAsStream("/WEB-INF/file.txt");
        // ...
    }
}

Something like this:

@Controller
public class SomeController {

    private Resource resource;

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    @RequestMapping("/texts")
    public ModelAndView texts() {
        InputStream in = resource.getInputStream();
        // ...
        in.close();
    }
}

In your bean definition:

<bean id="..." class="x.y.SomeController">
   <property name="resource" value="/WEB-INF/file.txt"/>
</bean>

This will create a ServletContextResource using the /WEB-INF/file.txt path, and inject that into your controller.

Note you can't use component-scanning to detect your controller using this technique, you need an explicit bean definition.

Or just use the @Value annotation.

For single file:

@Value("classpath:conf/about.xml")
private Resource about;

For multiple files:

@Value("classpath*:conf/about.*")
private Resource[] abouts;

What do you intend to use the resource for? In you example you don't do anything with it.

From it's name, however, it looks like you are trying to load internationalisation / localisation messages - for which you can you a MessageSource .

If you define some beans (possibly in a separate messages-context.xml ) similar to this:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>WEB-INF/messages/messages</value>
        </list>
    </property>
</bean>

<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en_GB" />
</bean>

Spring will load your resource bundle when you application starts. You can then autowire the MessageSource into your controller and use it to get localised messages:

@Controller
public class SomeController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping("/texts")
    public ModelAndView texts(Locale locale) {
        String localisedMessage = messageSource.getMessage("my.message.key", new Object[]{}, locale)
        /* do something with localised message here */
        return new ModelAndView("texts");
    }
}

NB. adding Locale as a parameter to your controller method will cause Spring to magically wire it in - that's all you need to do.

You can also then access the messages in your resource bundle in your JSPs using:

<spring:message code="my.message.key" />

Which is my preferred way to do it - just seems cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM