How do I load a resource and use its contents as a string in Spring

前端 未结 5 1017
春和景丽
春和景丽 2020-12-04 17:48

How can I load a Spring resource contents and use it to set a bean property or pass it as an argument constructor?

The resource contains free text.

相关标签:
5条回答
  • 2020-12-04 17:54
    <bean id="contents" class="org.apache.commons.io.IOUtils" factory-method="toString">
        <constructor-arg value="classpath:path/to/resource.txt" type="java.io.InputStream" />
    </bean>
    

    This solution requires Apache Commons IO.

    Another solution, suggested by @Parvez, without Apache Commons IO dependency is

    <bean id="contents" class="java.lang.String">
        <constructor-arg>
            <bean class="org.springframework.util.FileCopyUtils" factory-method="copyToByteArray">
                <constructor-arg value="classpath:path/to/resource.txt" type="java.io.InputStream" />
            </bean>     
        </constructor-arg>
    </bean>
    
    0 讨论(0)
  • 2020-12-04 17:56

    daoway answer was very helpful, and following Adrian remark I am adding a configuration snippet similar to daoway's code

    <bean id="contents" class="org.springframework.core.io.ClassPathResource">
        <constructor-arg value="path/to/resource.txt"/>
    </bean>
    

    From your component

        @Autowired
        private Resource contents;
    
        @PostConstruct
        public void load(){
            try {
                final InputStream inputStream = contents.getInputStream();
                    //use the stream 
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream ,1024);
                StringBuilder stringBuilder = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    stringBuilder.append(line).append('\n');
                }
                br.close();
         }catch (IOException e) {
                LOGGER.error(message);
         }
      }
    
    0 讨论(0)
  • 2020-12-04 18:11

    In one line try this to read test.xml:

    String msg = StreamUtils.copyToString( new ClassPathResource("test.xml").getInputStream(), Charset.defaultCharset()  );
    
    0 讨论(0)
  • 2020-12-04 18:12

    This is one way of doing it without using any external library.. default provided by spring.. environment.properties file contains key value pairs...reference each value with ${key}

    here in my example, I am keeping database props

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list value-type="org.springframework.core.io.Resource">
                <value>classpath:environment.properties</value>
    
            </list>
        </property>
    </bean>
    <bean id="mySQLdataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${JDBC.driver}" />
        <property name="url" value="${JDBC.URL}" />
        <property name="username" value="${JDBC.username}" />
        <property name="password" value="${JDBC.password}" />
    </bean>
    
    0 讨论(0)
  • 2020-12-04 18:17

    Just read it :

        try {
            Resource resource = new ClassPathResource(fileLocationInClasspath);
            BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()),1024);
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line).append('\n');
            }
            br.close();
            return stringBuilder.toString();
        } catch (Exception e) {
            LOGGER.error(e);
        }
    
    0 讨论(0)
提交回复
热议问题