Java关键字 transient

2022-11-01 15:32:49 浏览数 (1)

transient 短暂的,转瞬即逝的;暂时的

  • 在Java中,一个类只要实现了Serilizable接口,其属性和方法都会被自动序列化。
  • 但实际开发中,对象的某些属性可能比较敏感,不便于被序列化从而传输到网络或者本地文件。
  • 这时候就可以使用transient关键字修饰该属性,正如其意,transient修饰的对象只会短暂的存在于内存中。
  • transient只能修饰属性,不能用于类和方法。
Demo
代码语言:javascript复制
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person implements Serializable {
    private String username;
    private transient String password;
    private static final long serialVersionUID = -5758970364037629271L;
}

public class TransientTest {
    @Test
    public void test() {
        File file = new File("serializable.txt");
        ObjectOutputStream output = null;
        ObjectInputStream input = null;

        Person person = new Person("root", "root");

        //将对象序列化到本地文件
        try {
            output = new ObjectOutputStream(new FileOutputStream(file));
            output.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //从本地序列化文件中读取对象
        try {
            input = new ObjectInputStream(new FileInputStream("serializable.txt"));
            person = (Person) input.readObject();

            //查看效果
            System.out.println(person.getUsername());
            System.out.println(person.getPassword());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
效果
代码语言:javascript复制
D:Java_JDKJDK14binjava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2libidea_rt.jar=8550:D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2bin" -Dfile.encoding=UTF-8 -classpath "D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2libidea_rt.jar;D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2pluginsjunitlibjunit5-rt.jar;D:JetBrainsDeveloperToolsIntelliJ IDEA 2021.2pluginsjunitlibjunit-rt.jar;D:My ProjectsIDEAssmtargettest-classes;D:My ProjectsIDEAssmtargetclasses;D:Mavenrepositoryorgspringframeworkspring-context5.2.15.RELEASEspring-context-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-aop5.2.15.RELEASEspring-aop-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-beans5.2.15.RELEASEspring-beans-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-core5.2.15.RELEASEspring-core-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-jcl5.2.15.RELEASEspring-jcl-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-expression5.2.15.RELEASEspring-expression-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-webmvc5.2.15.RELEASEspring-webmvc-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-web5.2.15.RELEASEspring-web-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-jdbc5.2.15.RELEASEspring-jdbc-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-tx5.2.15.RELEASEspring-tx-5.2.15.RELEASE.jar;D:Mavenrepositoryorgspringframeworkspring-test5.2.15.RELEASEspring-test-5.2.15.RELEASE.jar;D:Mavenrepositorymysqlmysql-connector-java8.0.26mysql-connector-java-8.0.26.jar;D:Mavenrepositorycomgoogleprotobufprotobuf-java3.11.4protobuf-java-3.11.4.jar;D:Mavenrepositoryorgmybatismybatis3.5.7mybatis-3.5.7.jar;D:Mavenrepositoryorgmybatismybatis-spring2.0.6mybatis-spring-2.0.6.jar;D:Mavenrepositoryorgprojectlomboklombok1.18.20lombok-1.18.20.jar;D:Mavenrepositorycommchangec3p0.9.5.5c3p0-0.9.5.5.jar;D:Mavenrepositorycommchangemchange-commons-java.2.19mchange-commons-java-0.2.19.jar;D:Mavenrepositoryjunitjunit4.13.2junit-4.13.2.jar;D:Mavenrepositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar;D:Mavenrepositorychqoslogbacklogback-classic1.2.5logback-classic-1.2.5.jar;D:Mavenrepositorychqoslogbacklogback-core1.2.5logback-core-1.2.5.jar;D:Mavenrepositoryorgslf4jslf4j-api2.0.0-alpha1slf4j-api-2.0.0-alpha1.jar;D:Mavenrepositorycomgooglecodegsongson2.8.8gson-2.8.8.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 top.harmonytqs.ssm.TransientTest,test
root
null

Process finished with exit code 0

此处password字段被取出的时候为null,可见并未被序列化。

Q.E.D.

0 人点赞