当前位置: 首页 > news >正文

扬中seo重庆seo网站建设优化

扬中seo,重庆seo网站建设优化,重庆seo收费,建设网站怎么设置网站页面大小个人简介#xff1a;Java领域新星创作者#xff1b;阿里云技术博主、星级博主、专家博主#xff1b;正在Java学习的路上摸爬滚打#xff0c;记录学习的过程~ 个人主页#xff1a;.29.的博客 学习社区#xff1a;进去逛一逛~ 资源操作#xff1a;Spring Resources一、Res… 个人简介Java领域新星创作者阿里云技术博主、星级博主、专家博主正在Java学习的路上摸爬滚打记录学习的过程~ 个人主页.29.的博客 学习社区进去逛一逛~ 资源操作Spring Resources一、Resource接口 Resource接口实现类⚪UrlResource⚪ClassPathResource⚪FileSystemResource⚪ServletContextResource⚪InputStreamResource⚪ByteArrayResource二、ResourceLoader 接口三、ResourceLoaderAware接口四、让Spring为Bean实例依赖注入资源建议一、Resource接口 Spring 的 Resource 接口位于 org.springframework.core.io 中。 旨在成为一个更强大的接口用于抽象对低级资源的访问。以下显示了Resource接口定义的方法 public interface Resource extends InputStreamSource {boolean exists();boolean isReadable();boolean isOpen();boolean isFile();URL getURL() throws IOException;URI getURI() throws IOException;File getFile() throws IOException;ReadableByteChannel readableChannel() throws IOException;long contentLength() throws IOException;long lastModified() throws IOException;Resource createRelative(String relativePath) throws IOException;String getFilename();String getDescription(); }其中一些重要的方法 getInputStream(): 找到并打开资源返回一个InputStream以从资源中读取。预计每次调用都会返回一个新的InputStream()调用者有责任关闭每个流exists(): 返回一个布尔值表明某个资源是否以物理形式存在isOpen: 返回一个布尔值指示此资源是否具有开放流的句柄。如果为trueInputStream就不能够多次读取只能够读取一次并且及时关闭以避免内存泄漏。对于所有常规资源实现返回false但是InputStreamResource除外。getDescription(): 返回资源的描述用来输出错误的日志。这通常是完全限定的文件名或资源的实际URL。 其他方法 isReadable(): 表明资源的目录读取是否通过getInputStream()进行读取。isFile(): 表明这个资源是否代表了一个文件系统的文件。getURL(): 返回一个URL句柄如果资源不能够被解析为URL将抛出IOExceptiongetURI(): 返回一个资源的URI句柄getFile(): 返回某个文件如果资源不能够被解析称为绝对路径将会抛出FileNotFoundExceptionlastModified(): 资源最后一次修改的时间戳createRelative(): 创建此资源的相关资源getFilename(): 资源的文件名是什么 例如最后一部分的文件名 myfile.txt Resource接口实现类 ⚪UrlResource Resource的一个实现类用来访问网络资源它支持URL的绝对路径。 http:------该前缀用于访问基于HTTP协议的网络资源。 ftp:------该前缀用于访问基于FTP协议的网络资源 file: ------该前缀用于从文件系统中读取资源 案例: import org.springframework.core.io.UrlResource; import java.io.IOException; import java.net.MalformedURLException;/*** author .29.* create 2023-03-01 9:11*/ public class urlResources {public static void loadAndReadUrlResources(String path){UrlResource url null;try {url new UrlResource(path);//获取资源名System.out.println(url.getURL());System.out.println(url.getFilename());//获取资源描述System.out.println(url.getDescription());//获取资源内容System.out.println(url.getInputStream().read());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args){//访问网络资源测试UrlResource功能 // loadAndReadUrlResources(http://www.baidu.com);//方法二读取文件获取路径loadAndReadUrlResources(file:baidu.txt);} }注意file:前缀读取的是根路径下的类容http:前缀测试结果 file:前缀测试结果 ⚪ClassPathResource ClassPathResource 用来访问类加载路径下的资源相对于其他的 Resource 实现类其主要优势是方便访问类加载路径里的资源尤其对于 Web 应用ClassPathResource 可自动搜索位于 classes 下的资源文件无须使用绝对路径访问。 案例 import org.springframework.core.io.ClassPathResource; import java.io.IOException; import java.io.InputStream;/*** author .29.* create 2023-03-02 20:20*///测试ClassPathResource(); public class ClassPathResourceDemo {public static void main(String[] args){loadClassPathResource(baidu.txt);}public static void loadClassPathResource(String path){ClassPathResource resource new ClassPathResource(path);//获取文件信息System.out.println(resource.getFilename());System.out.println(resource.getDescription());//获取文件内容try {InputStream inputStream resource.getInputStream();byte[] bytes new byte[1024];if(inputStream.read(bytes) ! -1){System.out.println(new String(bytes));}} catch (IOException e) {e.printStackTrace();}} }⚪FileSystemResource Spring 提供的 FileSystemResource 类用于访问文件系统资源使用 FileSystemResource 来访问文件系统资源并没有太大的优势因为 Java 提供的 File 类也可用于访问文件系统资源。 案例 import org.springframework.core.io.FileSystemResource;import java.io.IOException; import java.io.InputStream;/*** author .29.* create 2023-03-02 20:32*/ public class FileSystemResourceDemo {public static void main(String[] args){loadFileSystemResource(d:\\haojin.txt); //path是绝对路径}public static void loadFileSystemResource(String path){FileSystemResource fileSystemResource new FileSystemResource(path);//获取系统资源的信息System.out.println(fileSystemResource.getFilename());System.out.println(fileSystemResource.getDescription());//获取资源内容try {InputStream inputStream fileSystemResource.getInputStream();byte[] bytes new byte[1024];if(inputStream.read(bytes) ! 1){System.out.println(new String(bytes));}} catch (IOException e) {e.printStackTrace();}} }⚪ServletContextResource 这是ServletContext资源的Resource实现它解释相关Web应用程序根目录中的相对路径。它始终支持流(stream)访问和URL访问但只有在扩展Web应用程序存档且资源实际位于文件系统上时才允许java.io.File访问。无论它是在文件系统上扩展还是直接从JAR或其他地方如数据库访问实际上都依赖于Servlet容器。 ⚪InputStreamResource InputStreamResource 是给定的输入流(InputStream)的Resource实现。它的使用场景在没有特定的资源实现的时候使用(感觉和Component 的适用场景很相似)。与其他Resource实现相比这是已打开资源的描述符。 因此它的isOpen()方法返回true。如果需要将资源描述符保留在某处或者需要多次读取流请不要使用它。 ⚪ByteArrayResource 字节数组的Resource实现类。通过给定的数组创建了一个ByteArrayInputStream。它对于从任何给定的字节数组加载内容非常有用而无需求助于单次使用的InputStreamResource。 二、ResourceLoader 接口 Spring将采用和ApplicationContext相同的策略来访问资源。也就是说如果ApplicationContext是FileSystemXmlApplicationContextres就是FileSystemResource实例如果ApplicationContext是ClassPathXmlApplicationContextres就是ClassPathResource实例 当Spring应用需要进行资源访问时实际上并不需要直接使用Resource实现类而是调用ResourceLoader实例的getResource()方法来获得资源ReosurceLoader将会负责选择Reosurce实现类也就是确定具体的资源访问策略从而将应用程序和具体的资源访问策略分离开来 案例 import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.Resource;/*** author .29.* create 2023-03-02 20:53*/ public class ResourceLoaderDemo {public static void main(String[] args){//示例一ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext();Resource resource context.getResource(baidu.txt);System.out.println(resource.getFilename());System.out.println(resource.getDescription());System.out.println(----------------------------------------------------------);//示例二FileSystemXmlApplicationContext context2 new FileSystemXmlApplicationContext();Resource resource2 context.getResource(baidu.txt);System.out.println(resource2.getFilename());System.out.println(resource2.getDescription());} } 三、ResourceLoaderAware接口 ResourceLoaderAware接口实现类的实例将获得一个ResourceLoader的引用ResourceLoaderAware接口也提供了一个setResourceLoader()方法该方法将由Spring容器负责调用Spring容器会将一个ResourceLoader对象作为该方法的参数传入。 如果把实现ResourceLoaderAware接口的Bean类部署在Spring容器中Spring容器会将自身当成ResourceLoader作为setResourceLoader()方法的参数传入。由于ApplicationContext的实现类都实现了ResourceLoader接口Spring容器自身完全可作为ResorceLoader使用。 案例 1.创建接口实现类 import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader;/*** author .29.* create 2023-03-02 21:08*/ public class testBean implements ResourceLoaderAware {private ResourceLoader resourceLoader;//实现ResourceLoaderAware接口必须实现的方法//如果把该Bean部署在Spring容器中该方法将会有Spring容器负责调用。//SPring容器调用该方法时Spring会将自身作为参数传给该方法。Overridepublic void setResourceLoader(ResourceLoader resourceLoader) {this.resourceLoader resourceLoader;}//返回resourceLoader对象的方法public ResourceLoader getResourceLoader(){return this.resourceLoader;} }2.配置实现类的bean ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean id testBean classcom.haojin.spring.resources.ResourceLoaderAwareDemo.testBean/bean/beans3.测试 import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ResourceLoader;/*** author .29.* create 2023-03-02 21:12*/ public class Demo {public static void main(String[] args){ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(bean.xml);testBean testBean context.getBean(testBean, testBean.class);ResourceLoader resourceLoader testBean.getResourceLoader();System.out.println(Spring容器将自身注入到ResourceLoaderAware Bean 中: (context resourceLoader));} } 四、让Spring为Bean实例依赖注入资源建议 当程序获取 Resource 实例时总需要提供 Resource 所在的位置不管通过 FileSystemResource 创建实例还是通过 ClassPathResource 创建实例或者通过 ApplicationContext 的 getResource() 方法获取实例都需要提供资源位置。这意味着资源所在的物理位置将被耦合到代码中如果资源位置发生改变则必须改写程序。因此通常建议采用依赖注入让 Spring 为 Bean 实例依赖注入资源。 案例 1.创建依赖注入类定义属性和方法 import org.springframework.core.io.Resource;/*** author .29.* create 2023-03-02 21:36*/ public class ResourceBean {//Resource实现类对象private Resource resource;//对象的Getter() 和 Setter()public Resource getResource() {return resource;}public void setResource(Resource resource) {this.resource resource;}//输出资源信息的方法public void parse(){System.out.println(resource.getDescription());System.out.println(resource.getFilename());} } 2.创建spring配置文件配置依赖注入: 利用依赖注入以后只需要在配置文件中修改资源文件位置即可?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean id resourceBean classcom.haojin.spring.resources.DI.ResourceBeanproperty nameresource valuebaidu.txt//bean/beans3.测试 import org.springframework.context.support.ClassPathXmlApplicationContext;/*** author .29.* create 2023-03-02 21:38*/ public class testDI {public static void main(String[] args){ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(beans.xml);ResourceBean resourceBean context.getBean(resourceBean, ResourceBean.class);resourceBean.parse();} }
http://www.hkea.cn/news/14502688/

相关文章:

  • 那个网站做国外售货做电子商务网站的意义
  • 网站建设是广告么做响应式网站怎么设计
  • .net和php哪个做网站好classplus wordpress
  • 做投票页面什么网站好seo怎么发外链的
  • 哪家公司做网站正规网站建设项目需求费用
  • 网站建设要多久豆瓣大连网络公司服务
  • 徐州建设工程网站论坛网站开发 go
  • 长春网站建设服务网站在线制作系统
  • 公司网站维护更新流程自己网站建设基本流程
  • 网站功能有哪些山西手机版建站系统哪家好
  • 网站开发用到的虚拟机有哪些建设门户网站申请报告
  • 北京顺义做网站站外推广6种方法
  • 网络营销网站推广方案北京市建设工程信息网力推中项网1
  • 网站域名免费申请青海哪家做网站的公司最大
  • 公司网站建设制作网站开发都需要什么工具
  • 双语公司网站系统下载成都专业app开发服务
  • 婚庆网站大全在线印章生成器
  • 企业网站建设找智恒网络网站分析流程
  • dw成品网站成品视频教学wordpress 标题相同
  • 上海企业信用信息公示系统查询入口北京网站优化实战
  • 网站出现乱码免费ppt模板下载简约
  • 用七牛做网站怎么优化整站
  • 江苏建设部官方网站免费的logo设计网站
  • 免费做企业推广的网站跨境网站有哪些
  • 江苏省常州建设高等职业技术学校网站window安装wordpress
  • 世界摄影网站logo设计公司哪家好
  • 贺州同城购物网站建设360竞价推广怎么做
  • 网站建设与制作wordpress通过tag获取文章
  • 怎么做中英文网站网页设计的流程ui
  • 索莱宝做网站在线音乐网站怎么做