如何在Spring中加载资源并将其内容用作字符串

2021/01/17 12:21 · java ·  · 0评论

如何加载Spring资源内容并将其用于设置bean属性或将其作为参数构造函数传递?

该资源包含自由文本。

在一行中,尝试读取test.xml:

String msg = StreamUtils.copyToString( new ClassPathResource("test.xml").getInputStream(), Charset.defaultCharset()  );
<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>

此解决方案需要Apache Commons IO。

@Parvez建议的另一种没有Apache Commons IO依赖性的解决方案是

<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>

只需阅读:

    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);
    }

这是一种不使用任何外部库的方式。.spring提供的默认库.. environment.properties文件包含键值对...使用$ {key}引用每个值

在我的示例中,我保留数据库道具

<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>

daoway的回答非常有帮助,并且在Adrian评论之后,我添加了一个类似于daoway的代码的配置片段

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

从您的组件

    @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);
     }
  }
本文地址:http://java.askforanswer.com/ruhezaispringzhongjiazaiziyuanbingjiangqineirongyongzuozifuchuan.html
文章标签: ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!