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

  • 在Java中,一个类只要实现了Serilizable接口,其属性和方法都会被自动序列化。
  • 但实际开发中,对象的某些属性可能比较敏感,不便于被序列化从而传输到网络或者本地文件。
  • 这时候就可以使用transient关键字修饰该属性,正如其意,transient修饰的对象只会短暂的存在于内存中。
  • transient只能修饰属性,不能用于类和方法。

Demo

@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();
                }
            }
        }
    }
}

效果

D:\Java_JDK\JDK14\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\lib\idea_rt.jar=8550:D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\lib\idea_rt.jar;D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\plugins\junit\lib\junit5-rt.jar;D:\JetBrainsDeveloperTools\IntelliJ IDEA 2021.2\plugins\junit\lib\junit-rt.jar;D:\My Projects\IDEA\ssm\target\test-classes;D:\My Projects\IDEA\ssm\target\classes;D:\Maven\repository\org\springframework\spring-context\5.2.15.RELEASE\spring-context-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-aop\5.2.15.RELEASE\spring-aop-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-beans\5.2.15.RELEASE\spring-beans-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-core\5.2.15.RELEASE\spring-core-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-jcl\5.2.15.RELEASE\spring-jcl-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-expression\5.2.15.RELEASE\spring-expression-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-webmvc\5.2.15.RELEASE\spring-webmvc-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-web\5.2.15.RELEASE\spring-web-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-jdbc\5.2.15.RELEASE\spring-jdbc-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-tx\5.2.15.RELEASE\spring-tx-5.2.15.RELEASE.jar;D:\Maven\repository\org\springframework\spring-test\5.2.15.RELEASE\spring-test-5.2.15.RELEASE.jar;D:\Maven\repository\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar;D:\Maven\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;D:\Maven\repository\org\mybatis\mybatis\3.5.7\mybatis-3.5.7.jar;D:\Maven\repository\org\mybatis\mybatis-spring\2.0.6\mybatis-spring-2.0.6.jar;D:\Maven\repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;D:\Maven\repository\com\mchange\c3p0\0.9.5.5\c3p0-0.9.5.5.jar;D:\Maven\repository\com\mchange\mchange-commons-java\0.2.19\mchange-commons-java-0.2.19.jar;D:\Maven\repository\junit\junit\4.13.2\junit-4.13.2.jar;D:\Maven\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\Maven\repository\ch\qos\logback\logback-classic\1.2.5\logback-classic-1.2.5.jar;D:\Maven\repository\ch\qos\logback\logback-core\1.2.5\logback-core-1.2.5.jar;D:\Maven\repository\org\slf4j\slf4j-api\2.0.0-alpha1\slf4j-api-2.0.0-alpha1.jar;D:\Maven\repository\com\google\code\gson\gson\2.8.8\gson-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,可见并未被序列化。