Put the file in the properties of string in spring xml configuration

These days, java configuration is normal, so I don't have much trouble. For example, suppose you are writing spring-batch in xml and want to set the sql property to the SQL string that went out to the file.

FileCopyUtils [java --How do I load a resource and use its contents as a string in Spring --Stack Overflow](https://stackoverflow.com/questions/14022839/how-do-i-load-a-resource-and-use -its-contents-as-a-string-in-spring)

	<bean id="sql" class="java.lang.String">
		<constructor-arg>
			<bean class="org.springframework.util.FileCopyUtils"
				factory-method="copyToByteArray">
				<constructor-arg value="classpath:a.txt"
					type="java.io.InputStream" />
			</bean>
		</constructor-arg>
	</bean>

Create a class that reads from a file and returns a string

The above is all completed with XML, but it seems to be a little long. So, think about making your own bean that just does that.

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.stream.Collectors;

import org.springframework.util.ResourceUtils;

public class ResourceToStringUtils {
	
	public String to(String location) throws IOException {
                ApplicationContext ctx = ...;
                BufferedReader in
                    = new BufferedReader(new InputStreamReader(resource.getInputStream(), Charset.defaultCharset()));
                return = in.lines().collect(Collectors.joining(System.lineSeparator()));

                // 2017/0/02 Addendum I used to write as below, but depending on the environment, FileNotFoundException will occur.
                //Details will be written separately. maybe.
                //File file = ResourceUtils.getFile(location);
                //return Files.lines(file.toPath(), Charset.defaultCharset()).collect(Collectors.joining(System.lineSeparator()));


	}
}

Create a class that receives the location of spring, reads all the contents of the file, converts it to a string, and returns it.

<bean id="resourceToStringUtils"
  class="kagamihoge.hogehoge.ResourceToStringUtils"></bean>

...
<property name="p" value="#{resourceToStringUtils.to('file:hoge.txt')}"></property>
...
<property name="p" value="#{resourceToStringUtils.to('classpath:foo.sql')}"></property>

Recommended Posts

Put the file in the properties of string in spring xml configuration
How to set environment variables in the properties file of Spring boot application
Overwrite bean definition in spring xml configuration file with another xml
Procedure to make the value of the property file visible in Spring Boot
The story of low-level string comparison in Java
Specify the encoding of static resources in Spring Boot
Access the war file in the root directory of Tomcat
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Java] Get the file in the jar regardless of the environment
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
Dynamically access the property specified by the string in Spring BeanWrapper
Minimum configuration sample of RESTful API in Jersey + Spring Framework
Why put a line break at the end of the file
Get a proxy instance of the component itself in Spring Boot
Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Read the file under the classpath as a character string with spring
Fix the file name of war to the one set in Maven
Spring validation was important in the order of Form and BindingResult
Get the public URL of a private Flickr file in Java
Form that receives the value of the repeating item in Spring MVC
How to get the length of an audio file in java
Replace the contents of the Jar file
Read Java properties file in C #
Unzip the zip file in Java
Various methods of the String class
Order of processing in the program
I read the source of String
Filter the result of BindingResult [Spring]
[Spring Boot] List of validation rules that can be used in the property file for error messages
`SAXReader`: Get the xml file with the default namespace set in XPath format
The story of forgetting to close a file in Java and failing
Examine the contents of the WAR file generated by the project created by Spring Initializr
Sample program that returns the hash value of a file in Java
Decrypt the entire bean in a properties file with some values encrypted
The story of encountering Spring custom annotation
Get the result of POST in Java
Put Kanban in Docker of Raspberry Pi 3
The identity of params [: id] in rails
Static file access priority in Spring boot
Local file download memorandum in Spring Boot
The story of AppClip support in Anyca
About the initial display of Spring Framework
Notation to put a variable in a string
The story of writing Java in Emacs
Java: Place the ResourceBundle properties file anywhere
Item 63: Beware the performance of string concatenation
[Java] [Spring] Test the behavior of the logger
Maven configuration problem in Spring pom.xml in Eclipse
Write the movement of Rakefile in the runbook
File output bean as JSON in spring
Character string comparison: I was caught in the skill check problem of Paiza
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
[Spring Boot] How to get properties dynamically from a string contained in a URL
Get the path defined in Controller class of Spring boot as a list
The attached file name was garbled in the Spring Boot email, so take measures
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.