通过实例深入学习Java的Struts框架中的OGNL表达式使用
Struts 2默认的表达式语言是OGNL,原因是它相对其它表达式语言具有下面几大优势:
1. 支持对象方法调用,如xxx.doSomeSpecial();
2. 支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路径)]@[方法名 | 值名],例如:@java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME;
3. 支持赋值操作和表达式串联,如price=100, discount=0.8, calculatePrice(),这个表达式会返回80;
4. 访问OGNL上下文(OGNL context)和ActionContext;
5. 操作集合对象。
下面我们来看OGNL使用的几个例子:
示例:上下文环境中使用OGNL
public class OGNL1 { public static void main(String[] args) { /* 创建一个上下文Context对象,它是用保存多个对象一个环境 对象 */ Map<String , Object> context = new HashMap<String , Object>(); Person person1 = new Person(); person1.setName("zhangsan"); Person person2 = new Person(); person2.setName("lisi"); Person person3 = new Person(); person3.setName("wangwu"); /* person4不放入到上下文环境中 */ Person person4 = new Person(); person4.setName("zhaoliu"); /* 将person1、person2、person3添加到环境中(上下文中) */ context.put("person1", person1); context.put("person2", person2); context.put("person3", person3); try { /* 获取根对象的"name"属性值 */ Object value = Ognl.getValue("name", context, person2); System.out.println("ognl expression \"name\" evaluation is : " + value); /* 获取根对象的"name"属性值 */ Object value2 = Ognl.getValue("#person2.name", context, person2); System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2); /* 获取person1对象的"name"属性值 */ Object value3 = Ognl.getValue("#person1.name", context, person2); System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3); /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */ Object value4 = Ognl.getValue("name", context, person4); System.out.println("ognl expression \"name\" evaluation is : " + value4); /* 将person4指定为root对象,获取person4对象的"name"属性,注意person4对象不在上下文中 */ Object value5 = Ognl.getValue("#person4.name", context, person4); System.out.println("ognl expression \"person4.name\" evaluation is : " + value5); /* 获取person4对象的"name"属性,注意person4对象不在上下文中 */ // Object value6 = Ognl.getValue("#person4.name", context, person2); // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6); } catch (OgnlException e) { e.printStackTrace(); } } } class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制台输出:
ognl expression "name" evaluation is : lisi ognl expression "#person2.name" evaluation is : lisi ognl expression "#person1.name" evaluation is : zhangsan ognl expression "name" evaluation is : zhaoliu ognl.OgnlException: source is null for getProperty(null, "name") at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296) at ognl.ASTProperty.getValueBody(ASTProperty.java:114) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.ASTChain.getValueBody(ASTChain.java:141) at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) at ognl.SimpleNode.getValue(SimpleNode.java:258) at ognl.Ognl.getValue(Ognl.java:494) at ognl.Ognl.getValue(Ognl.java:596) at ognl.Ognl.getValue(Ognl.java:566) at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53)
对于使用上下文的OGNL,若不指定从哪一个对象中查找"name"属性,则OGNL直接从根对象(root)查找,若指定查找对象(使用'#'号指定,如#person1),则从指定的对象中查找,若指定对象不在上下文中则会抛出异常,换句话说就是是#person1.name形式指定查找对象则必须要保证指定对象在上下文环境中。
示例:使用OGNL调用方法
public class OGNL2 { public static void main(String[] args) { /* OGNL提供的一个上下文类,它实现了Map接口 */ OgnlContext context = new OgnlContext(); People people1 = new People(); people1.setName("zhangsan"); People people2 = new People(); people2.setName("lisi"); People people3 = new People(); people3.setName("wangwu"); context.put("people1", people1); context.put("people2", people2); context.put("people3", people3); context.setRoot(people1); try { /* 调用 成员方法 */ Object value = Ognl.getValue("name.length()", context, context.getRoot()); System.out.println("people1 name length is :" + value); Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot()); System.out.println("people2 name upperCase is :" + upperCase); Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot()); System.out.println("people1 name.charAt(5) is :" + invokeWithArgs); /* 调用静态方法 */ Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot()); System.out.println("min(4,10) is :" + min); /* 调用静态变量 */ Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot()); System.out.println("E is :" + e); } catch (OgnlException e) { e.printStackTrace(); } } } class People { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制台输出:
people1 name length is :8 people2 name upperCase is :LISI people1 name.charAt(5) is :s min(4,10) is :4 E is :2.718281828459045
使用OGNL调用方法也十分简单,对于成员方法调用,只需要给出方法的名称+(),若有参数,直接写在括号内,与一般调用Java方法一致。对于静态方法的调用,需要使用如下格式:@ClassName@method,对于静态变量需要使用如下格式:@ClassName@field。
示例:使用OGNL操作集合
public class OGNL3 { public static void main(String[] args) throws Exception { OgnlContext context = new OgnlContext(); Classroom classroom = new Classroom(); classroom.getStudents().add("zhangsan"); classroom.getStudents().add("lisi"); classroom.getStudents().add("wangwu"); classroom.getStudents().add("zhaoliu"); classroom.getStudents().add("qianqi"); Student student = new Student(); student.getContactWays().put("homeNumber", "110"); student.getContactWays().put("companyNumber", "119"); student.getContactWays().put("mobilePhone", "112"); context.put("classroom", classroom); context.put("student", student); context.setRoot(classroom); /* 获得classroom的students集合 */ Object collection = Ognl.getValue("students", context, context.getRoot()); System.out.println("students collection is :" + collection); /* 获得classroom的students集合 */ Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot()); System.out.println("first student is : " + firstStudent); /* 调用集合的方法 */ Object size = Ognl.getValue("students.size()", context, context.getRoot()); System.out.println("students collection size is :" + size); System.out.println("--------------------------飘逸的分割线--------------------------"); Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot()); System.out.println("mapCollection is :" + mapCollection); Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot()); System.out.println("the first element of contactWays is :" + firstElement); System.out.println("--------------------------飘逸的分割线--------------------------"); /* 创建集合 */ Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot()); System.out.println(createCollection); /* 创建Map集合 */ Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot()); System.out.println(createMapCollection); } } class Classroom { private List<String> students = new ArrayList<String>(); public List<String> getStudents() { return students; } public void setStudents(List<String> students) { this.students = students; } } class Student { private Map<String , Object> contactWays = new HashMap<String , Object>(); public Map<String , Object> getContactWays() { return contactWays; } public void setContactWays(Map<String , Object> contactWays) { this.contactWays = contactWays; } }
控制台的输出:
students collection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi] first student is : zhangsan students collection size is :5 --------------------------飘逸的分割线-------------------------- mapCollection is :{homeNumber=110, mobilePhone=112, companyNumber=119} the first element of contactWays is :110 --------------------------飘逸的分割线-------------------------- [aa, bb, cc, dd] {key1=value1, key2=value2}
OGNL不仅可以操作集合对象,还可以创建集合对象,对集合操作与对属性的操作没什么不同,需要注意的是OGNL认为List与Array是一样的。使用OGNL创建List集合时使用{},创建Map对象时使用#{}。
示例:使用OGNL过滤集合与投影集合
public class OGNL4 { public static void main(String[] args) throws Exception { OgnlContext context = new OgnlContext(); Humen humen = new Humen(); humen.setName("qiuyi"); humen.setSex("n"); humen.setAge(22); humen.getFriends().add(new Humen("zhangsan" , "n" , 22)); humen.getFriends().add(new Humen("lisi" , "f" , 21)); humen.getFriends().add(new Humen("wangwu" , "n" , 23)); humen.getFriends().add(new Humen("zhaoliu" , "n" , 22)); humen.getFriends().add(new Humen("qianqi" , "n" , 22)); humen.getFriends().add(new Humen("sunba" , "f" , 20)); humen.getFriends().add(new Humen("yangqiu" , "f" , 25)); context.put("humen", humen); context.setRoot(humen); /* OGNL过滤集合的语法为:collection.{? expression} */ Object filterCollection = Ognl.getValue("friends.{? #this.name.length() > 7}", context, context.getRoot()); System.out.println("filterCollection is :" + filterCollection); System.out.println("--------------------------飘逸的分割线--------------------------"); /* OGNL投影集合的语法为:collection.{expression} */ Object projectionCollection = Ognl.getValue("friends.{name}", context, context.getRoot()); System.out.println("projectionCollection is :" + projectionCollection); } } class Humen { private String name; private String sex; private int age; private List<Humen> friends = new ArrayList<Humen>(); public Humen() { } public Humen(String name , String sex , int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Humen> getFriends() { return friends; } public void setFriends(List<Humen> friends) { this.friends = friends; } @Override public String toString() { return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
控制台输出:
filterCollection is :[Humen [name=zhangsan, sex=n, age=22]] --------------------------飘逸的分割线-------------------------- projectionCollection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi, sunba, yangqiu]
OGNL可以对集合进行过滤与投影操作,过滤的语法为collection.{? expression},其中使用"#this"表示集合当前对象(可以与for-each循环比较)。投影的语法为collection.{expression}。投影和过滤可以看做是数据库中对表取列和取行的操作。
一些常见问题
平时使用Struts2标签时会出现一些很奇特的问题,对于OGNL不了解的人可能对问题的出现无能为力或者就算解决了问题也不知道是如何解决的。下面总结一些使用Struts2标签容易出现的困惑:
问题一:#,%{},$符号
在Struts2标签属性中经常会出现"#"或者"%{}"的符号出现,通过上面OGNL表达式基础的介绍,知道了OGNL上下文中有且仅有一个根对象。Struts2为我们定义了许多明明对象,他们分别是"ValueStack","Parameters","Session","Request", "Appliction","Attr",其中"ValueStack"被设置为上下文的根对象。访问非根对象必须加上"#"号,这就是出现"#"的原因。Struts2中的标的处理类,并不是所有都将标签的属性作为OGNL表达式来看待,有时候我们需要设置动态地值,则必须告诉标签的处理类该字符串按照OGNL表达式来处理,%{}符号的作用就是告诉标签的处理类将它包含的字符串按照OGNL表达式处理。 "$"符号用于XML文件中用于获取动态值,与%{}作用类似。
问题二:%{}符号的影响
Struts2的标签几十几百个,要记住哪一个标签的处理类将标签的属性作为OGNL表达式是一件很困难的事情,在不清楚处理类的处理方式时怎么办,%{}对于标签处理类来说,若处理类将属性值作为普通字符串则%{}符号包含的字符串当做OGNL表达式,若处理类将属性值作为OGNL表达式来处理,则直接忽略%{}符号。换句话说,不清楚处理方式的话,可以都使用%{}符号。
问题三:标签是如何获得数据
下面是ValueStack的官方描述:
ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression, the stack will be searched down the stack, from the latest objects pushed in to the earliest, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the expression being evaluated).
大致意思:ValueStack允许保存多个bean(也就是Action),并且可以使用表达式语言获得他们。当评估一个表达式,ValueStack将会从栈顶到栈底的方向被搜索一遍,对于给定的属性名称寻找bean的getter或setter方法或寻找给定的方法。
每当一个请求到达Action时,Struts2会将Action对象推入ValueStack中。
<body> username:<s:property value="username"/><br /> -------------------诡异的分割线-------------------<br /> username:<%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %><br /> </body>
页面显示结果:
username:zhangsan -------------------诡异的分割线------------------- username:zhangsan
可以看到标签取值与用Java代码取值的结果相同,明显标签的取值方式更简练简洁。OGNL表达式"username"表示了从根对象ValueStack中取出属性username的值。它会从栈顶到栈底遍历ValueStack,直到找某一个Action中的"username"属性。
总结OGNL的使用方法:
1.访问属性
名字属性获取:
<s:property value="user.username"/><br>
地址属性获取:
<s:property value="user.address.addr"/><br>
2.访问方法
调用值栈中对象的普通方法:
<s:property value="user.get()"/><br>
3.访问静态属性和方法
调用Action中的静态方法:
<s:property value="@struts.action.LoginAction@get()"/>
调用JDK中的类的静态方法:
<s:property value="@java.lang.Math@floor(44.56)"/><br>
调用JDK中的类的静态方法(同上):
<s:property value="@@floor(44.56)"/><br>
调用JDK中的类的静态方法:
<s:property value="@java.util.Calendar@getInstance()"/><br>
调用普通类中的静态属性:
<s:property value="@struts.vo.Address@TIPS"/><br>
访问构造方法
调用普通类的构造方法:
<s:property value="new struts.vo.Student('李晓红' , '美女' , 3 , 25).username"/>
4.访问数组
获取List:
<s:property value="testList"/><br>
获取List中的某一个元素(可以使用类似于数组中的下标获取List中的内容):
<s:property value="testList[0]"/><br>
获取Set:
<s:property value="testSet"/><br>
获取Set中的某一个元素(Set由于没有顺序,所以不能使用下标获取数据):
<s:property value="testSet[0]"/><br> ×
获取Map:
<s:property value="testMap"/><br>
获取Map中所有的键:
<s:property value="testMap.keys"/><br>
获取Map中所有的值:
<s:property value="testMap.values"/><br>
获取Map中的某一个元素(可以使用类似于数组中的下标获取List中的内容):
<s:property value="testMap['m1']"/><br>
获取List的大小:
<s:property value="testSet.size"/><br>
5.访问集合 – 投影、选择(? ^ $)
利用选择获取List中成绩及格的对象:<s:property value="stus.{?#this.grade>=60}"/><br>
利用选择获取List中成绩及格的对象的username:
<s:property value="stus.{?#this.grade>=60}.{username}"/><br>
利用选择获取List中成绩及格的第一个对象的username:
<s:property value="stus.{?#this.grade>=60}.{username}[0]"/><br>
利用选择获取List中成绩及格的第一个对象的username:
<s:property value="stus.{^#this.grade>=60}.{username}"/><br>
利用选择获取List中成绩及格的最后一个对象的username:
<s:property value="stus.{$#this.grade>=60}.{username}"/><br>
利用选择获取List中成绩及格的第一个对象然后求大小:
<s:property value="stus.{^#this.grade>=600}.{username}.size"/><br>
集合的伪属性
OGNL能够引用集合的一些特殊的属性,这些属性并不是JavaBeans模式,例如size(),length()等等. 当表达式引用这些属性时,OGNL会调用相应的方法,这就是伪属性.
6.Lambda :[…]
格式::[…]
使用Lambda表达式计算阶乘:
<s:property value="#f = :[#this==1?1:#this*#f(#this-1)] , #f(4)"/><br>
7.OGNL中#的使用
#可以取出堆栈上下文中的存放的对象.
获取Paraments对象的属性:<s:property value="#parameters.username"/>
8.OGNL中%的使用
用%{}可以取出存在值堆栈中的Action对象,直接调用它的方法.
例如你的Action如果继承了ActionSupport .那么在页面标签中,用%{getText('key')}的方式可以拿出国际化信息.
9.OGNL中$的使用
“$”有两个主要的用途
- 用于在国际化资源文件中,引用OGNL表达式
- 在Struts 2配置文件中,引用OGNL表达式
推荐阅读
-
Java 类加载器的作用 - 简介:类加载器是 Java™ 中一个非常重要的概念。类加载器负责将 Java 类的字节码加载到 Java 虚拟机中。本文首先详细介绍了 Java 类加载器的基本概念,包括代理模型、加载类的具体过程和线程上下文类加载器等。然后介绍了如何开发自己的类加载器,最后介绍了类加载器在 Web 容器和 OSGi™ 中的应用。 类加载器是 Java 语言的一项创新,也是 Java 语言广受欢迎的重要原因之一。它允许将 Java 类动态加载到 Java 虚拟机中并执行。类加载器从 JDK 1.0 开始出现,最初是为了满足 Java Applets 的需求而开发的,Java Applets 需要从远程位置下载 Java 类文件并在浏览器中执行。现在,类加载器已广泛应用于网络容器和 OSGi。一般来说,Java 应用程序的开发人员不需要直接与类加载器交互;Java 虚拟机的默认行为足以应对大多数情况。但是,如果遇到需要与类加载器交互的情况,而您又不太了解类加载器的机制,就很容易花费大量时间调试异常,如 ClassNotFoundException 和 NoClassDefFoundError。本文将详细介绍 Java 的类加载器,帮助读者深入理解 Java 语言中的这一重要概念。下面先介绍一些基本概念。 类加载器的基本概念 顾名思义,类加载器用于将 Java 类加载到 Java 虚拟机中。一般来说,Java 虚拟机以如下方式使用 Java 类:Java 源程序(.java 文件)经 Java 编译器编译后转换为 Java 字节代码(.class 文件)。类加载器负责读取 Java 字节代码并将其转换为 java.lang 实例。每个实例都用来表示一个 Java 类。通过该实例的 newInstance 方法创建该类的对象。实际情况可能更加复杂,例如,Java 字节代码可能是由工具动态生成或通过网络下载的。 基本上,所有类加载器都是 java.lang.ClassLoader 类的实例。下面将详细介绍这个 Java 类。 java.lang.ClassLoader 类简介 java.lang.ClassLoader 类的基本职责是根据给定类的名称为其查找或生成相应的字节码,然后根据这些字节码定义一个 Java 类,即 java.lang.Class 类的实例。除此之外,ClassLoader 还负责加载 Java 应用程序所需的资源,如图像文件和配置文件。不过,本文只讨论它加载类的功能。为了履行加载类的职责,ClassLoader 提供了许多方法,其中比较重要的方法如表 1 所示。下文将详细介绍这些方法。 表 1.与加载类相关的 ClassLoader 方法
-
通过实例深入学习Java的Struts框架中的OGNL表达式使用
-
深入理解Struts2中OGNL表达式的实战应用实例
-
深入理解Java Struts框架中的栈操作与OGNL的运用
-
【Netty】「萌新入门」(七)ByteBuf 的性能优化-堆内存的分配和释放都是由 Java 虚拟机自动管理的,这意味着它们可以快速地被分配和释放,但是也会产生一些开销。 直接内存需要手动分配和释放,因为它由操作系统管理,这使得分配和释放的速度更快,但是也需要更多的系统资源。 另外,直接内存可以映射到本地文件中,这对于需要频繁读写文件的应用程序非常有用。 此外,直接内存还可以避免在使用 NIO 进行网络传输时发生数据拷贝的情况。在使用传统的 I/O 时,数据必须先从文件或网络中读取到堆内存中,然后再从堆内存中复制到直接缓冲区中,最后再通过 SocketChannel 发送到网络中。而使用直接缓冲区时,数据可以直接从文件或网络中读取到直接缓冲区中,并且可以直接从直接缓冲区中发送到网络中,避免了不必要的数据拷贝和内存分配。 通过 ByteBufAllocator.DEFAULT.directBuffer 方法来创建基于直接内存的 ByteBuf: ByteBuf directBuf = ByteBufAllocator.DEFAULT.directBuffer(16); 通过 ByteBufAllocator.DEFAULT.heapBuffer 方法来创建基于堆内存的 ByteBuf: ByteBuf heapBuf = ByteBufAllocator.DEFAULT.heapBuffer(16); 注意: 直接内存是一种特殊的内存分配方式,可以通过在堆外申请内存来避免 JVM 堆内存的限制,从而提高读写性能和降低 GC 压力。但是,直接内存的创建和销毁代价昂贵,因此需要慎重使用。 此外,由于直接内存不受 JVM 垃圾回收的管理,我们需要主动释放这部分内存,否则会造成内存泄漏。通常情况下,可以使用 ByteBuffer.clear 方法来释放直接内存中的数据,或者使用 ByteBuffer.cleaner 方法来手动释放直接内存空间。 测试代码: public static void testCreateByteBuf { ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(16); System.out.println(buf.getClass); ByteBuf heapBuf = ByteBufAllocator.DEFAULT.heapBuffer(16); System.out.println(heapBuf.getClass); ByteBuf directBuf = ByteBufAllocator.DEFAULT.directBuffer(16); System.out.println(directBuf.getClass); } 运行结果: class io.netty.buffer.PooledUnsafeDirectByteBuf class io.netty.buffer.PooledUnsafeHeapByteBuf class io.netty.buffer.PooledUnsafeDirectByteBuf 池化技术 在 Netty 中,池化技术指的是通过对象池来重用已经创建的对象,从而避免了频繁地创建和销毁对象,这种技术可以提高系统的性能和可伸缩性。 通过设置 VM options,来决定池化功能是否开启: -Dio.netty.allocator.type={unpooled|pooled} 在 Netty 4.1 版本以后,非 Android 平台默认启用池化实现,Android 平台启用非池化实现; 这里我们使用非池化功能进行测试,依旧使用的是上面的测试代码 testCreateByteBuf,运行结果如下所示: class io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeDirectByteBuf class io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf class io.netty.buffer.UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeDirectByteBuf 可以看到,ByteBuf 类由 PooledUnsafeDirectByteBuf 变成了 UnpooledUnsafeDirectByteBuf; 在没有池化的情况下,每次使用都需要创建新的 ByteBuf 实例,这个操作会涉及到内存的分配和初始化,如果是直接内存则代价更为昂贵,而且频繁的内存分配也可能导致内存碎片问题,增加 GC 压力。 使用池化技术可以避免频繁内存分配带来的开销,并且重用池中的 ByteBuf 实例,减少了内存占用和内存碎片问题。另外,池化技术还可以采用类似 jemalloc 的内存分配算法,进一步提升分配效率。 在高并发环境下,池化技术的优点更加明显,因为内存的分配和释放都是比较耗时的操作,频繁的内存分配和释放会导致系统性能下降,甚至可能出现内存溢出的风险。使用池化技术可以将内存分配和释放的操作集中到预先分配的池中,从而有效地降低系统的内存开销和风险。 内存释放 当在 Netty 中使用 ByteBuf 来处理数据时,需要特别注意内存回收问题。 Netty 提供了不同类型的 ByteBuf 实现,包括堆内存(JVM 内存)实现 UnpooledHeapByteBuf 和堆外内存(直接内存)实现 UnpooledDirectByteBuf,以及池化技术实现的 PooledByteBuf 及其子类。 UnpooledHeapByteBuf:通过 Java 的垃圾回收机制来自动回收内存; UnpooledDirectByteBuf:由于 JVM 的垃圾回收机制无法管理这些内存,因此需要手动调用 release 方法来释放内存; PooledByteBuf:使用了池化机制,需要更复杂的规则来回收内存; 由于池化技术的特殊性质,释放 PooledByteBuf 对象所使用的内存并不是立即被回收的,而是被放入一个内存池中,待下次分配内存时再次使用。因此,释放 PooledByteBuf 对象的内存可能会延迟到后续的某个时间点。为了避免内存泄漏和占用过多内存,我们需要根据实际情况来设置池化技术的相关参数,以便及时回收内存; Netty 采用了引用计数法来控制 ByteBuf 对象的内存回收,在博文 「源码解析」ByteBuf 的引用计数机制 中将会通过解读源码的形式对 ByteBuf 的引用计数法进行深入理解; 每个 ByteBuf 对象被创建时,都会初始化为1,表示该对象的初始计数为1。 在使用 ByteBuf 对象过程中,如果当前 handler 已经使用完该对象,需要通过调用 release 方法将计数减1,当计数为0时,底层内存会被回收,该对象也就被销毁了。此时即使 ByteBuf 对象还在,其各个方法均无法正常使用。 但是,如果当前 handler 还需要继续使用该对象,可以通过调用 retain 方法将计数加1,这样即使其他 handler 已经调用了 release 方法,该对象的内存仍然不会被回收。这种机制可以有效地避免了内存泄漏和意外访问已经释放的内存的情况。 一般来说,应该尽可能地保证 retain 和 release 方法成对出现,以确保计数正确。