如何加载 Spring 资源内容并使用它设置 bean 属性或将其作为参数构造函数传递?
资源包含自由文本。
<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>
这个解决方案需要 ApacheCommons 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>
这是一种不使用任何外部库的方法。.由弹簧提供的默认值。.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>
看看吧:
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); }
Daoway 的回答非常有帮助,在 艾德里安注释之后,我添加了一个类似 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); } }
在一行中尝试读取 test.xml:
String msg = StreamUtils.copyToString( new ClassPathResource("test.xml").getInputStream(), Charset.defaultCharset() );
春天
private String readResource(String fileName){ ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("resourceSubfolder/"+fileName) try{ Reader reader = new InputStreamReader(resource.getInputStream()); return FileCopyUtils.copyToString(reader); } catch (IOException e){ e.printStackTrace(); } return null; }
2021年最新情况。
您可以使用 Spring 的 Resource Interface 获取文本文件的内容,然后使用 StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset());获取文本。
Resource
StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset());
举例如下:
@Value("classpath:appVersionFilePath") private Resource resource; @GetMapping(value = "/hello") public HttpResult<String> hello() throws IOException { String appVersion = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); // ... }