wuheng преди 2 години
родител
ревизия
a1b8577695

+ 59 - 0
day04/pom.xml

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>spring</artifactId>
+        <groupId>com.lovecoding.spring</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>day04</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.2.6</version>
+        </dependency>
+
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.25</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-jdbc</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-beans</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aspects</artifactId>
+            <version>5.3.22</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 23 - 0
day04/src/main/java/com/lovecoding/i18n/I18nDemo.java

@@ -0,0 +1,23 @@
+package com.lovecoding.i18n;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+public class I18nDemo {
+
+    public static void main(String[] args) throws UnsupportedEncodingException {
+
+        ResourceBundle bundle = ResourceBundle.getBundle(
+                "i18n", Locale.CHINA
+        );
+        String username = bundle.getString("username");
+        //prioerties 文件是不允许 使用中文的, 因为编码写死在代码里了
+        //我们可以通过自己转码,或者转成 unicon 编码 解决中文乱码问题
+        String s = new String(username.getBytes("ISO-8859-1"), "UTF-8");
+        System.out.println(  s  );
+
+    }
+
+}

+ 46 - 0
day04/src/main/java/com/lovecoding/resources/BeanDemo.java

@@ -0,0 +1,46 @@
+package com.lovecoding.resources;
+
+import org.springframework.context.ResourceLoaderAware;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class BeanDemo {
+
+
+    public static void main(String[] args) {
+
+        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
+        /**
+         * 我们声明了一个 Bean 对象, 里面声明一个Set方法
+         * Spring 框架在检查Bean 对象的时候,
+         * 自动帮我们注入了 ResourceLoader 对象, 其实就是 ApplicationContext 对象
+         */
+        ResourceLoaderAwareDemo bean = context.getBean(ResourceLoaderAwareDemo.class);
+        ResourceLoader resourceLoader = bean.getResourceLoader();
+        /**
+         * resourceLoader 这个loader 是怎么获取的, 谁注入的
+         * 我想获取 系统文件目录的 文件
+         * 我想获取 ClassPath目录的文件 如何区分呢
+         * Spring 框架允许我们通过不同的协议 Spring框架内部帮我们处理了 不同的实现类
+         * Resource res = ctx.getResource("classpath:bean.xml");
+         * Resrouce res = ctx.getResource("file:bean.xml");
+         * Resource res = ctx.getResource("http://192.168.18.13:4567/beans.xml");
+         */
+        Resource resource = resourceLoader.getResource("classpath:lovecoding.txt");
+        InputStream inputStream = null;
+        try {
+            inputStream = resource.getInputStream();
+            byte[] n = new byte[1024];
+            while ( inputStream.read(n) != -1 ) {
+                System.out.println( new String(n) );
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+}

+ 30 - 0
day04/src/main/java/com/lovecoding/resources/ClassPathResourceDemo.java

@@ -0,0 +1,30 @@
+package com.lovecoding.resources;
+
+import org.springframework.core.io.ClassPathResource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ClassPathResourceDemo {
+
+    public static void main(String[] args) {
+        //ClassPathResourceDemo.class.getClassLoader()
+        getClassPathResource("classpathdemo.properties");
+    }
+
+    public static void getClassPathResource(String path){
+        ClassPathResource classPathResource = new ClassPathResource(path);
+        InputStream inputStream = null;
+        try {
+            inputStream = classPathResource.getInputStream();
+            byte[] n = new byte[1024];
+            while ( inputStream.read(n) != -1 ) {
+                System.out.println( new String(n) );
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+}

+ 30 - 0
day04/src/main/java/com/lovecoding/resources/FileSystemResourceDemo.java

@@ -0,0 +1,30 @@
+package com.lovecoding.resources;
+
+import org.springframework.core.io.FileSystemResource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class FileSystemResourceDemo {
+
+    public static void main(String[] args) {
+        getFileSystemResource("C:/Users/lc/Documents/Mysql8/docker-compose.yml");
+    }
+
+    public static void getFileSystemResource(String path){
+        FileSystemResource resource = new FileSystemResource(path);
+
+        try {
+            InputStream inputStream = resource.getInputStream();
+            byte[] n = new byte[1024];
+            while ( inputStream.read(n) != -1 ) {
+                System.out.println( new String(n) );
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+}

+ 18 - 0
day04/src/main/java/com/lovecoding/resources/ResourceLoaderAwareDemo.java

@@ -0,0 +1,18 @@
+package com.lovecoding.resources;
+
+import org.springframework.context.ResourceLoaderAware;
+import org.springframework.core.io.ResourceLoader;
+
+public class ResourceLoaderAwareDemo implements ResourceLoaderAware {
+
+    ResourceLoader resourceLoader;
+
+    @Override
+    public void setResourceLoader(ResourceLoader resourceLoader) {
+        this.resourceLoader = resourceLoader;
+    }
+
+    public ResourceLoader getResourceLoader() {
+        return resourceLoader;
+    }
+}

+ 24 - 0
day04/src/main/java/com/lovecoding/resources/ResourceLoaderDemo.java

@@ -0,0 +1,24 @@
+package com.lovecoding.resources;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.core.io.Resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ResourceLoaderDemo {
+    public static void main(String[] args) {
+        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
+        Resource resource = context.getResource("classpathdemo.properties");
+        InputStream inputStream = null;
+        try {
+            inputStream = resource.getInputStream();
+            byte[] n = new byte[1024];
+            while ( inputStream.read(n) != -1 ) {
+                System.out.println( new String(n) );
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 40 - 0
day04/src/main/java/com/lovecoding/resources/ResourcesDemo.java

@@ -0,0 +1,40 @@
+package com.lovecoding.resources;
+
+import org.springframework.core.io.UrlResource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+
+public class ResourcesDemo {
+
+    public static void main(String[] args) {
+        /**
+         * 值的是 URL 协议里的资源
+         * Http
+         * Https
+         * file
+         * ftp
+         */
+        getUrlRessourceByUrl("https://www.aliyun.com");
+
+    }
+
+    public static void getUrlRessourceByUrl(String url){
+        try {
+            UrlResource urlResource = new UrlResource(url);
+            String description = urlResource.getDescription();
+            System.out.println( description );
+            InputStream inputStream = urlResource.getInputStream();
+            byte[] n = new byte[1024];
+            while ( inputStream.read(n) != -1 ) {
+                System.out.println( new String(n) );
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+}

+ 10 - 0
day04/src/main/resources/bean.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+    <bean id="resourceLoaderAwareDemo" class="com.lovecoding.resources.ResourceLoaderAwareDemo">
+
+    </bean>
+
+</beans>

+ 2 - 0
day04/src/main/resources/classpathdemo.properties

@@ -0,0 +1,2 @@
+Hello World
+你好 世界

+ 0 - 0
day04/src/main/resources/i18n.properties


+ 1 - 0
day04/src/main/resources/i18n_en_US.properties

@@ -0,0 +1 @@
+username = Tom

+ 1 - 0
day04/src/main/resources/i18n_zh_CN.properties

@@ -0,0 +1 @@
+username = 张三

+ 1 - 0
day04/src/main/resources/lovecoding.txt

@@ -0,0 +1 @@
+Love coding