4.2  内置Resource实现

4.2.1  ByteArrayResource

ByteArrayResource代表byte[]数组资源,对于“getInputStream”操作将返回一个ByteArrayInputStream。

首先让我们看下使用ByteArrayResource如何处理byte数组资源:

package lqy.springh4_2;

import java.io.IOException;
import java.io.InputStream; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource; public class ResourceTest { /**
* @param args
*/
public static void main(String[] args) { Resource resource = new ByteArrayResource("Hello World!".getBytes());
if(resource.exists()) {
dumpStream(resource);
}
} private static void dumpStream(Resource resource) {
InputStream is = null;
try {
//1.获取文件资源
is = resource.getInputStream();
//2.读取资源
byte[] descBytes = new byte[is.available()];
is.read(descBytes);
System.out.println(new String(descBytes));
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
//3.关闭资源
is.close();
} catch (IOException e) {
}
}
} }

让我们来仔细看一下代码,dumpStream方法很抽象定义了访问流的三部曲:打开资源、读取资源、关闭资源,所以dunpStrean可以再进行抽象 从而能在自己项目中使用;byteArrayResourceTest测试方法,也定义了基本步骤:定义资源、验证资源存在、访问资源。

ByteArrayResource可多次读取数组资源,即isOpen ()永远返回false。

1.2.2  InputStreamResource

InputStreamResource代表java.io.InputStream字节流,对于“getInputStream ”操作将直接返回该字节流,因此只能读取一次该字节流,即“isOpen”永远返回true。

让我们看下测试代码吧:

package lqy.springh4_2;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; public class ResourceTest { /**
* @param args
*/
public static void main(String[] args) { ByteArrayInputStream bis = new ByteArrayInputStream("Hello World!".getBytes());
Resource resource = new InputStreamResource(bis);
if(resource.exists()) {
dumpStream(resource);
} System.out.println(resource.isOpen());
} private static void dumpStream(Resource resource) {
InputStream is = null;
try {
//1.获取文件资源
is = resource.getInputStream();
//2.读取资源
byte[] descBytes = new byte[is.available()];
is.read(descBytes);
System.out.println(new String(descBytes));
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
//3.关闭资源
is.close();
} catch (IOException e) {
}
}
} }

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAH4AAABLCAIAAAA59qTBAAAFa0lEQVR4nO2az4sjRRTH65zR+Tu8zqHnENc9eTHgfY+FxGLBg7sE5qCCo7Kd3OyL4riQ5KIILcKA9GUuC85lLm2GcTwYElkW3F32sL0gmMmYLg/9q351pzrppDLmfWmYpLqq3nuffvU6XT2IJgqCIAiCfr8fJGI/U0pfPvt93G08Pv913G1MppPJdBKGdDqbTcPZyenlVTjr9B8Favnt/f22Lzci7CbfXIy4LukQvlv21cXpX3lurjXvczwbksazgQdB0Dl488mLSXS88d7XT15Mjg920yOcvfz7r5+OD3ZzAmedZ+y7GOmjp4MWHR0hhOigRQetV9NpGNKz9t6/YXhyenkdhoffLIE+cDHLIEPkt/dR2o/5kqAPXJydD1ycNhai99ttZkqOjoT+QevW4+eT9Dg+2L0OfhaOV3988eP911WB8+nFeKKL/unF+bjboIPWWXuPDlrjbuPpxXmEPgzpyS+XYUgppTn0ddDHriVKz/jtfYQxltpT9EnucqfnZ70rT6kKPAiCz+7f+vPZP+nRa9aun3eF42r00XcfvKYKPAtJWF266CPudNA6PLw77jbo6GjcbYQhHXcblNKT09/SefJz/8ZIQP/pvbc+/vD2J/duf/X5299/+Y737bu9Zq3XrPWaO73mzvX4wdUF6b2/U9ZKGfSjIzpoOR0cVZ4o3+/c7QhHx/lh0ZA3RQL6Aj3EtasL8hDXFrCiXesHrb5z56y9lx50dLSAvRuhUugX4x7oo98qrSdwQK8QoDemjUAPWp0g640J0BsToDcmQG9MgN6YNg+9b1vIsqVd4Crl4lVb0BGg15FvW8wGZEXeLY/ety1523XNKunDQujZ3epKAgb05edn3hQso4XQ+7aF0rdzwjLMWpLoIr9dHDe5GFm2G69g7GYj4v5ZmC5Glm1jcZUzq1/bB74be0KrvrHohQEKW6KDovW4sRT6ZE7OVdYtNp3S9mgU70TUK/InOsN1z978MWejMb6N7eyNkzg4zwfhJaIiBPX7KrGDELzKlmIF8gYTX7TRZ5mucCtu5xMrcVPwhXVX9VnI+iyw/Lyf64NQYfIKjkaM0jWU441a+YWhStaFsp5zjw9bEVKl6H3bUpSmeT7MQa+V9cyqKo6XnTO5FqI5y/aXrfX5bmX/HFApeqaN8WKuD2wje/UWqPXsCJUt37ZFMlLBsWy/kt/1cc4kpVi6xVRbcDIDFsbpxPN84G+H9nK/cMSLLtpS/a/DsrdZUJUC9MYE6I0J0BsToDcmLfSrejG83dJCT0ErEKA3JkBvTKtC75H40Y14cxrXpjnWh06dPTN06qjuDFfoTxn0HmGcETxVStlHZ6BCHuFHid81lW8d0OeJM05Xj35RA/qqAv3QqSPlWtZDnztcHKZGrxruEUSctJ2dtci6VB03Cj0v4gkuCqmpgZ5d14VrPEGfmPNI3RnmDfcISpt5hIJLHkFsAm0yejnruaxBqCR6mURuuNE5jxBCiBdfibzh+fPwI9iIbh56sQazqh790CHO0CPEWz36lWv5Ws+sWVFVFhzqEVQnhERFhzhOPVlz6oKj5VI2JFq8N+4XDltz4g5iGSJeXiN7CykMdejUkYqTargKvdp62ko8+aawOehBlQrQGxOgNyZAb0yA3pgAvTEBemMqi37tz3z/XwF6YyqBXnwcZJ/axR1a5smQvVh6+8NbogqyXrVDq0RfsL28jaoGvZTCKvRF28vbqDWih0TnVfrHpQwwB728yViwvbyNKv+7PqsbRZvjaa+64xDVbRZWADxSGROgNyZAb0yA3pgAvTEBemMC9MYE6I0J0BsToDcmQG9MgN6YAL0xAXpjAvTGBOiNCdAb03+iv4RNpAqFPwAAAABJRU5ErkJggg==" alt="" />

测试代码几乎和ByteArrayResource测试完全一样,注意“isOpen”此处用于返回true。

4.2.3  FileSystemResource

FileSystemResource代表java.io.File资源,对于“getInputStream ”操作将返回底层文件的字节流,“isOpen”将永远返回false,从而表示可多次读取底层文件的字节流。

让我们看下测试代码吧:

package lqy.springh4_2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; public class ResourceTest { /**
* @param args
*/
public static void main(String[] args) { File file = new File("d:/test.txt");
Resource resource = new FileSystemResource(file);
if(resource.exists()) {
dumpStream(resource);
} System.out.println(resource.isOpen());
} private static void dumpStream(Resource resource) {
InputStream is = null;
try {
//1.获取文件资源
is = resource.getInputStream();
//2.读取资源
byte[] descBytes = new byte[is.available()];
is.read(descBytes);
System.out.println(new String(descBytes));
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
//3.关闭资源
is.close();
} catch (IOException e) {
}
}
} }

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAABFCAIAAADvgDXLAAAGZ0lEQVR4nO2cvaGrMAyFWYqGaag8CrVHuD29x2ABlmADXgHYsiWDQQkJL+erEuMfWdbBMpfcap7naZqmafr7+5s26Oef4mcnfgg8s0MFFVF+duKHwDM7ZFUEACgEexEAWqAiALRARQBogYoA0AIVAaDlm1Q0dHVVd8M7h+jbd48A7qVvq7afdMGjDjyoaJ+hq6vA9ymwb6uUtn9Zt76rd919hq5WGvwxFa0uqrtBp6Khq1+yZBpO2nBJRTSWPj7hDFswnSPrvbS7N6lo6Oq61gXRtYlPr4nezS1Q0Zn+Ly/Z23mkioaurrtB59QnqijswCSVWP0bSjaHL4b27VrUt1Xd9WuK1PahxVo/zKtvq7rr4qtTlF4V2zClWY+/ULSPU18nDYSxUgPT0WlP/HPiLnEIcYKTFEyH5nHv5bujKkonGbklfJF8wXxbd0MiUeIEwWW8nGZ0gs9JaOUjJ9M2zWh5QJ5U0WZF5G46eOpmP7M0npZai6k0eKZERfTqtiztNkJIrQ5tiLKwvhWmsLfQ0bmItpTGEu5u8YDelryK9oyVJ+gv0aELzbuwF0mrQK/6tRQWi40RbhaJRsmSis4J5VxF3G9HkZNrG43CA/KUirInQGIHO+TW3SCtdE1mwz4ne1Fwa343OrQhSUVymUnBHJkc+XyX0ni7Eu87e3tR3lR5UH8t9nSJeVczOrYKfAXFaukQUjQkJgm32LicqSizxHuRk20rXiABeXEvitwRR7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kjU/BZRmvtkqpFf6YQ71ENU5GP0ca6+5/TPQSoCAAtUBEAWr74PToAHgJUBIAWqAgALadUJD3LYfjHjjgegR/hjIrkv/eLPPFVGQAuckJFZ5QBFYEfokhFuZdu82+kMxWRLnZ/LgHA83jRXpReS79Lf0U6+m0CAA9Bp6LsG+mSqoQqFKgIPBWNinbeSM/sW4vq2PvsADwahYr23kjPZ3/7v00A4IGoMjrpjfQ0UeO/Vg19SL9NAOBx4N0FALRARQBogYoA0AIVAaAFKgJAC1QEgBaoCAAtUBEAWqAiALRARQBogYoA0AIVAaAFKgJAC1QEgBaoCAAtUBEAWqAiALRARQBo+Qd+5RymzfzxVgAAAABJRU5ErkJggg==" alt="" />

注意由于“isOpen”将永远返回false,所以可以多次调用dumpStream(resource)。

4.2.4  ClassPathResource

ClassPathResource代表classpath路径的资源,将使用ClassLoader进行加载资源。classpath 资源存在于类路径中的文件系统中或jar包里,且“isOpen”永远返回false,表示可多次读取资源。

ClassPathResource加载资源替代了Class类和ClassLoader类的“getResource(String name)”和“getResourceAsStream(String name)”两个加载类路径资源方法,提供一致的访问方式。

ClassPathResource提供了三个构造器:

public ClassPathResource(String path):使用默认的ClassLoader加载“path”类路径资源;

public ClassPathResource(String path, ClassLoader classLoader)使用指定的ClassLoader加载“path”类路径资源;

比如当前类路径是 “cn.javass.spring.chapter4.ResourceTest”,而需要加载的资源路径是“cn/javass/spring /chapter4/test1.properties”,则将加载的资源在“cn/javass/spring/chapter4 /test1.properties”;

public ClassPathResource(String path, Class<?> clazz)使用指定的类加载“path”类路径资源,将加载相对于当前类的路径的资源;

比如当前类路径是 “cn.javass.spring.chapter4.ResourceTest”,而需要加载的资源路径是“cn/javass/spring /chapter4/test1.properties”,则将加载的资源在“cn/javass/spring/chapter4/cn/javass /spring/chapter4/test1.properties”;

而如果需要 加载的资源路径为“test1.properties”,将加载的资源为“cn/javass/spring/chapter4/test1.properties”。

让我们直接看测试代码吧:

1)使用默认的加载器加载资源,将加载当前ClassLoader类路径上相对于根路径的资源:

    public static void main(String[] args) throws IOException {

        Resource resource = new ClassPathResource("lqy/springh4_2/test1.properties");
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
System.out.println(resource.isOpen());
}

test1.properties是

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAABGCAIAAABaCg9FAAAKuUlEQVR4nO2d7W/bxh3H783Ql3u5l0E3LAhiNFjaSPO4Lsh/kGKAARtoHccFhHVDUQdIl6Y1kETxU5IWI5AhLZDOsYfaU2AjLVLMaZEFigvkQUFrI4mLGBSsVlKc6Ike9UhSTMe9oEQeyTuJkinp7NwXh4B3/P3ufsx9fOSRdzZQqagIFuh0AFRUtUQBpSJaFFAqokUBpSJaFFAqokUBpSJabQX0yKu/cGa44AO+hdbGYlOYZQDDhht1W/A140XlVK0FNOjfu/j+S3o637MDzgb9ezF+2wXQMMsA/Okwy4CK2n61W0atBXTxWFc4OLkeml4PTS/NnZsc6oWzi8e6MH6dALRJ1QA0zDIMg6V+waefCLPMlrnedqt5QIPB4MTERB2bd3evh6bl7HX+/uyjrz6eHOqFs8F3d2P8tgOgYZZh2LCzJwDNtgXRbX01CeiNGze6urp27txZ2+z6kd3roWn+/ix/f/bR159ogOrZ60ecALrgA5AYNmy5GdsQsN05tQHKqKdaNVTOsGFjGFvwAYZlfUaD1kgYltUbNRkbUVeRcwboFvqBbLeaAVSjs7e3ty6g197etTR37tFXHz/6+pM7U/7jBw/A2Wtv78L46R224ANmVLXehrrd2reI26VGbLXQqNJcbgIUNtGbNJlCgNqNLbDXAZTOs2qoYUB1Ovv6+moDmgpdis70pEKXVFWdHOo9fvCAliaHerV0vmcHZl5fpc7SdQvmcSusosYebZyD3SzM6llzORoqzEiIiMQwhuutC5/pR5DKpsYAhemsC2h0pkd99jA606OqqjYrOn7wADxJuvLmrzGudQGtsICfXWijo3U800+1ElDoEUMXBkFKZ101BujExMROSGNjYzjL1M0PpdWTr+0C0urJ1M2PV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AAO1AdQAAoB2oDgAAtAPVAQCAdqA6AADQDlQHAADa8X9yfnyT1IMxeAAAAABJRU5ErkJggg==" alt="" />

2)使用指定的ClassLoader进行加载资源,将加载指定的ClassLoader类路径上相对于根路径的资源:

    @Test
public void testClasspathResourceByClassLoader() throws IOException {
ClassLoader cl = this.getClass().getClassLoader();
Resource resource = new ClassPathResource("lqy/springh4_2/test1.properties",cl);
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
Assert.assertEquals(false, resource.isOpen());
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa0AAABnCAIAAAA8MIoGAAALfElEQVR4nO2c72scxxnHF+G/wW/9yq9tCNKLDQ0hMaVUtR29NiFHCwsh2GAjmoiYSrbLiTQirF9I1+JyxAkWMssZEcrZYIOxqJOIljPtixP1Gr+Jf8Un2cdZkSLpwvTF3u7Or92b1Um+0z3fDw/itDuzO7uj+eiZ2ZWsOgAA0MbqdgMAAKDLwIMAAOrAgwAA6sCDAADqwIMAAOrAgwAA6sCDAADqwIMAAOrAgwAA6sCDAADqwIMAAOrAgwAA6sCDABCjkh+0BvOVer3u5ayc1+3m9AIpHqzkB62Y4L69ZrycZek6qjI5FG1OKkOexf0H1VBKib3cQzfSy7WaNDTZhZ+8nmPf4VNqKKXMxmwPebBXermNB6Ob5OVe8xipTA5Z1lAuN6Q5bWVyaGiykl4GLO4/+OzkxytTMw8PHFqZmnl28uMkD/be3atMDoUj47X/5PUm+w6fOn768sjo7BHn4sjo7PHTl5M8mOVmddeDPdTLph7s0h3jE78ILyf89tCWAfXF/QdXpmYYY8/OnGWMrUzN7BkPCl2K/q3X6/V9h0+NjM4yxt47c5kxNjI6u+c92Eu9bOjBOJNWNgefvZw1mM/npHycS9NznngYM3R3R9Jgt+9gz7K4/+DDA4cCCT47c/bhgUPGHgw2erlWX0rf1rnpDD+Vlottv/fF4Sl3OEn2HT51xLkYSPC9M5ePOBdNPSh3gzRso65RB3Vav+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于当前类的路径的资源:

         Class clazz = this.getClass();
Resource resource1 = new ClassPathResource("lqy/springh4_2/test1.properties" , clazz);
if(resource1.exists()) {
dumpStream(resource1);
}
System.out.println("path:" + resource1.getFile().getAbsolutePath());
Assert.assertEquals(false, resource1.isOpen()); Resource resource2 = new ClassPathResource("test1.properties" , this.getClass());
if(resource2.exists()) {
dumpStream(resource2);
}
System.out.println("path:" + resource2.getFile().getAbsolutePath());
Assert.assertEquals(false, resource2.isOpen());

“resource1”将加载lqy/springh4_2/lqy/springh4_2/test1.properties资源;“resource2”将加载“lqy/springh4_2/test1.properties”;

4)加载jar包里的资源,首先在当前类路径下找不到,最后才到Jar包里找,而且在第一个Jar包里找到的将被返回:

eption {
Resource resource = new ClassPathResource("overview.html");
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getURL().getPath());
Assert.assertEquals(false, resource.isOpen());

如 果当前类路径包含“overview.html”,在项目的“resources”目录下,将加载该资源,否则将加载Jar包里的 “overview.html”,而且不能使用“resource.getFile()”,应该使用“resource.getURL()”,因为资源不 存在于文件系统而是存在于jar包里,URL类似于“file:/C:/.../***.jar!/overview.html”。

类路径一般都是相对路径,即相对于类路径或相对于当前类的路径,因此如果使用“/test1.properties”带前缀“/”的路径,将自动删除“/”得到“test1.properties”。

4.2.5  UrlResource

UrlResource代表URL资源,用于简化URL资源访问。“isOpen”永远返回false,表示可多次读取资源。

UrlResource一般支持如下资源访问:

http通过标准的http协议访问web资源,如new UrlResource(“http://地址”);

ftp通过ftp协议访问资源,如new UrlResource(“ftp://地址”);

file通过file协议访问本地文件系统资源,如new UrlResource(“file:d:/test.txt”);

4.2.6  ServletContextResource

ServletContextResource代表web应用资源,用于简化servlet容器的ServletContext接口的getResource操作和getResourceAsStream操作;在此就不具体演示了。

4.2.7  VfsResource

VfsResource代表Jboss 虚拟文件系统资源。

Jboss VFS(Virtual File System)框架是一个文件系统资源访问的抽象层,它能一致的访问物理文件系统、jar资源、zip资源、war资源等,VFS能把这些资源一致的映射 到一个目录上,访问它们就像访问物理文件资源一样,而其实这些资源不存在于物理文件系统。

在示例之前需要准备一些jar包,在此我们使用的是Jboss VFS3版本,可以下载最新的Jboss AS 6x,拷贝lib目录下的“jboss-logging.jar”和“jboss-vfs.jar”两个jar包拷贝到我们项目的lib目录中并添加到 “Java Build Path”中的“Libaries”中。

@Test
public void testVfsResourceForRealFileSystem() throws IOException {
//1.创建一个虚拟的文件目录
VirtualFile home = VFS.getChild("/home");
//2.将虚拟目录映射到物理的目录
VFS.mount(home, new RealFileSystem(new File("d:")));
//3.通过虚拟目录获取文件资源
VirtualFile testFile = home.getChild("test.txt");
//4.通过一致的接口访问
Resource resource = new VfsResource(testFile);
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
Assert.assertEquals(false, resource.isOpen());
}
@Test
public void testVfsResourceForJar() throws IOException {
//1.首先获取jar包路径
File realFile = new File("lib/org.springframework.beans-3.0.5.RELEASE.jar");
//2.创建一个虚拟的文件目录
VirtualFile home = VFS.getChild("/home2");
//3.将虚拟目录映射到物理的目录
VFS.mountZipExpanded(realFile, home,
TempFileProvider.create("tmp", Executors.newScheduledThreadPool(1)));
//4.通过虚拟目录获取文件资源
VirtualFile testFile = home.getChild("META-INF/spring.handlers");
Resource resource = new VfsResource(testFile);
if(resource.exists()) {
dumpStream(resource);
}
System.out.println("path:" + resource.getFile().getAbsolutePath());
Assert.assertEquals(false, resource.isOpen());
}

通过VFS,对于jar里的资源和物理文件系统访问都具有一致性,此处只是简单示例,如果需要请到Jboss官网深入学习。

开涛spring3(4.2) - 资源 之 4.2 内置Resource实现的更多相关文章

  1. 开涛spring3(4.1) - 资源 之 4.1 基础知识

    4.1.1  概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源 (JBoss AS 5.x上的VFS资源)等 ...

  2. 资源 之 4.2 内置Resource实现(拾)

    4.2  内置Resource实现 4.2.1  ByteArrayResource ByteArrayResource代表byte[]数组资源,对于"getInputStream" ...

  3. 开涛spring3(4.4) - 资源 之 4.4 Resource通配符路径

    4.4.1  使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...

  4. 开涛spring3(4.3) - 资源 之 4.3 访问Resource

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  5. spring3: 内置Resource实现

    4.2  内置Resource实现 4.2.1  ByteArrayResource ByteArrayResource代表byte[]数组资源,对于“getInputStream”操作将返回一个By ...

  6. 开涛spring3(9.2) - Spring的事务 之 9.2 数据库事务概述

    9.2.1  概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口 PlatformTransactionManager,从而能支持 ...

  7. 开涛spring3(12.3) - 零配置 之 12.3 注解实现Bean定义

    12.3  注解实现Bean定义 12.3.1  概述 前边介绍的Bean定义全是基于XML方式定义配置元数据,且在[12.2注解实现Bean依赖注入]一节中介绍了通过注解来减少配置数量,但并没有完全 ...

  8. 开涛spring3(1) - Spring概述

    1.1.1  Spring是什么 Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由 ...

  9. 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入

    12.2  注解实现Bean依赖注入 12.2.1  概述 注解实现Bean配置主要用来进行如依赖注入.生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的 ...

随机推荐

  1. Lombok : 让你写 Java代码像C#一样爽

    前言 我曾经是一名 .Net 开发,如今的我是一名 Java 开发者.在我享受着 Java 成熟的生态时,我常常怀念 c# 简洁的语法:自动属性.类型推断.自动初始化器 .... 鱼,我所欲也,熊掌亦 ...

  2. UIScrollView的布局

    一.UIScrollView的子控件布局不能跟其它的控件一样进行布局,因为UIScrollView会根据子控件计算出ContentSize的大小,那么我们应该如何进行布局呀. 遵循以下两点就行 1.参 ...

  3. quartus ii有符号数的问题(待完善)

    1.有符号数是以补码的形式存储的,在modelsim仿真时,补码自动转换为有符号的十进制数. 2.关于截位的问题: 当只取寄存器中的其中m位时,这时候取得的数是按照无符号位来处理的? 3.reg型数据 ...

  4. Entity Framework技术系列之0:开篇

    小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/shareto ...

  5. web前端概念巩固(一)

    h5: 1.web语义化 Web语义化是指在进行HTML结构.表现.行为设计时,尽量使用语义化的标签,使程序代码简介明了,易于进行Web操作和网站SEO,方便团队协作的一种标准,以图实现一种" ...

  6. oracle表空间扩容

    oracle在使用中会发现,表空间不足的情况:以下介绍了如何1.查询表空间使用率.剩余量:2.如何扩展表空间容量: 1.表空间容量指标查询 SELECT TABLESPACE_NAME "表 ...

  7. 解决Tomcat: Can't load IA 32-bit .dll on a AMD 64-bit platform 问题

    错误如下: java.lang.UnsatisfiedLinkError: E:\Program Files\MyEclipse 10\apache-tomcat-7.0.23\bin\tcnativ ...

  8. java多线程基本概述(二十)——中断

    线程中断我们已经直到可以使用 interrupt() 方法,但是你必须要持有 Thread 对象,但是新的并发库中似乎在避免直接对 Thread 对象的直接操作,尽量使用 Executor 来执行所有 ...

  9. lua metatable(元表)

    lua metatable (元表) 概述 普通 table 类型的表仅能够做一些单表操作, 无法进行一些特殊的以及两个表的操作, 比如 table1 + table2, print(table3) ...

  10. JavaScript的XMLHttpRequest对象

    ajax的核心技术是XMLHttpRequest对象,简称XHR. //支持IE7及更高的版本 function createXML() { if(typeof XMLHttpReqquest!=&q ...