Inject a file resource into Spring bean

后端 未结 3 1514
离开以前
离开以前 2021-01-12 09:56

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 ?



        
相关标签:
3条回答
  • 2021-01-12 10:14

    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.

    0 讨论(0)
  • 2021-01-12 10:14

    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;
    
    0 讨论(0)
  • 2021-01-12 10:18

    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.

    0 讨论(0)
提交回复
热议问题