File tree Expand file tree Collapse file tree 9 files changed +145
-0
lines changed
spring-core-work-with-resources
java/c/jbd/spring/resources Expand file tree Collapse file tree 9 files changed +145
-0
lines changed Original file line number Diff line number Diff line change 2929 <module >spring-aop-09-custom-annotation</module >
3030 <module >spring-aop-10-inter-type-declarations</module >
3131 <module >spring-core-null-safety</module >
32+ <module >spring-core-work-with-resources</module >
3233 </modules >
3334
3435 <properties >
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <parent >
6+ <artifactId >spring-framework-tutorial-parent</artifactId >
7+ <groupId >com.jstobigdata</groupId >
8+ <version >1.0-SNAPSHOT</version >
9+ </parent >
10+ <modelVersion >4.0.0</modelVersion >
11+
12+ <artifactId >spring-core-work-with-resources</artifactId >
13+ <description >Spring example to work with resources</description >
14+
15+ <dependencies >
16+ <dependency >
17+ <groupId >org.springframework</groupId >
18+ <artifactId >spring-core</artifactId >
19+ </dependency >
20+
21+ <dependency >
22+ <groupId >org.springframework</groupId >
23+ <artifactId >spring-context</artifactId >
24+ </dependency >
25+ </dependencies >
26+
27+ <dependencyManagement >
28+ <dependencies >
29+ <dependency >
30+ <groupId >com.jstobigdata</groupId >
31+ <artifactId >spring-tutorial-boms</artifactId >
32+ <version >1.0-SNAPSHOT</version >
33+ <type >pom</type >
34+ <scope >import</scope >
35+ </dependency >
36+ </dependencies >
37+ </dependencyManagement >
38+
39+ </project >
Original file line number Diff line number Diff line change 1+ package c .jbd .spring .resources ;
2+
3+ import org .springframework .core .io .DefaultResourceLoader ;
4+ import org .springframework .core .io .Resource ;
5+ import org .springframework .core .io .ResourceLoader ;
6+
7+ import java .io .IOException ;
8+
9+ public class DefaultResourceLoaderExample {
10+ public static void main (String [] args ) throws IOException {
11+ ResourceLoader resourceLoader = new DefaultResourceLoader ();
12+ Resource r = resourceLoader .getResource ("sample.md" );
13+ ReaderUtil .readAndPrint (r .getInputStream ());
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package c .jbd .spring .resources ;
2+
3+ import java .io .IOException ;
4+ import java .io .InputStream ;
5+
6+ public class ReaderUtil {
7+ public static void readAndPrint (InputStream stream ) throws IOException {
8+ int content = stream .read ();
9+ System .out .println ("\n ============ start ===========" );
10+ while (content != -1 ) {
11+ System .out .print ((char ) content );
12+ content = stream .read ();
13+ }
14+ System .out .println ("\n ============ End ============" );
15+ stream .close ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package c .jbd .spring .resources ;
2+
3+ import org .springframework .core .io .ClassPathResource ;
4+ import org .springframework .core .io .Resource ;
5+ import org .springframework .core .io .UrlResource ;
6+
7+ import java .io .IOException ;
8+
9+ public class ResourcesExample {
10+ public static void main (String [] args ) throws IOException {
11+ Resource urlResource
12+ = new UrlResource ("https://google.com" );
13+ ReaderUtil .readAndPrint (urlResource .getInputStream ());
14+
15+ Resource classPathResource = new ClassPathResource ("sample.md" );
16+ ReaderUtil .readAndPrint (classPathResource .getInputStream ());
17+
18+ //FileSystemResource fileResource = new FileSystemResource("PathToFile/filename");
19+ //readAndPrint(fileResource.getInputStream());
20+ }
21+
22+
23+ }
Original file line number Diff line number Diff line change 1+ package c .jbd .spring .resources .loader ;
2+
3+ import c .jbd .spring .resources .ReaderUtil ;
4+ import org .springframework .context .ResourceLoaderAware ;
5+ import org .springframework .core .io .Resource ;
6+ import org .springframework .core .io .ResourceLoader ;
7+ import org .springframework .stereotype .Component ;
8+
9+ import java .io .IOException ;
10+
11+ @ Component ("jbdResourceLoader" )
12+ public class JbdResourceLoader implements ResourceLoaderAware {
13+ private ResourceLoader resourceLoader ;
14+
15+ public void setResourceLoader (ResourceLoader resourceLoader ) {
16+ this .resourceLoader = resourceLoader ;
17+ }
18+
19+ public void loadResource (String resourcePath ) throws IOException {
20+ Resource resource = resourceLoader .getResource (resourcePath );
21+ ReaderUtil .readAndPrint (resource .getInputStream ());
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package c .jbd .spring .resources .loader ;
2+
3+ import org .springframework .context .ApplicationContext ;
4+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
5+
6+ import java .io .IOException ;
7+
8+ public class ResourceLoaderDemo {
9+ public static void main (String [] args ) throws IOException {
10+
11+ ApplicationContext ctx = new AnnotationConfigApplicationContext (JbdResourceLoader .class );
12+ JbdResourceLoader loader = (JbdResourceLoader ) ctx .getBean ("jbdResourceLoader" );
13+ loader .loadResource ("classpath:sample.md" );
14+ System .out .println ("*** Resource loader using classpath ***" );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <beans xmlns =" http://www.springframework.org/schema/beans"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://www.springframework.org/schema/beans
5+ http://www.springframework.org/schema/beans/spring-beans.xsd" >
6+
7+ <bean id =" item" >
8+ <property name =" id" value =" #{ T(java.util.UUID).randomUUID().toString() }" />
9+ </bean >
10+ </beans >
Original file line number Diff line number Diff line change 1+ ## This is a Sample Markdown file
You can’t perform that action at this time.
0 commit comments