java学习笔记-第九课

1. 模块化编程
1.1 模块化的概念
  • 模块化:将程序划分为独立的模块,每个模块负责特定的功能。Java 9 引入了模块系统,增强了代码的可维护性和可重用性。
1.2 定义模块
  • module-info.java:每个模块都有一个 module-info.java 文件,用于定义模块名称和依赖。
    module com.example.myapp {
        requires java.base; // 指定模块依赖
        exports com.example.myapp.api; // 导出包
    }
    
1.3 创建模块
  • 示例
    // module-info.java
    module com.example.myapp {
        requires java.base;
        exports com.example.myapp.api;
    }
    
    // com/example/myapp/api/HelloWorld.java
    package com.example.myapp.api;
    
    public class HelloWorld {
        public static void sayHello() {
            System.out.println("Hello, World!");
        }
    }
    
    // 使用模块
    import com.example.myapp.api.HelloWorld;
    
    public class Main {
        public static void main(String[] args) {
            HelloWorld.sayHello();
        }
    }
    

2. 国际化 (I18N) 和本地化 (L10N)
2.1 国际化的概念
  • 国际化(I18N):使应用程序能够支持多种语言和地区。
  • 本地化(L10N):针对特定语言和地区对应用程序进行定制。
2.2 使用 ResourceBundle
  • 定义资源文件

    • messages_en.properties
      greeting=Hello
      
    • messages_fr.properties
      greeting=Bonjour
      
  • 加载资源文件

    import java.util.Locale;
    import java.util.ResourceBundle;
    
    public class Main {
        public static void main(String[] args) {
            ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.FRANCE);
            System.out.println(bundle.getString("greeting")); // 输出:Bonjour
    
            bundle = ResourceBundle.getBundle("messages", Locale.US);
            System.out.println(bundle.getString("greeting")); // 输出:Hello
        }
    }
    
3. 安全编程
3.1 加密与解密
  • Java 加密架构 (JCA):提供加密、解密、签名等功能。

  • 对称加密

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String text = "Hello, World!";
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey secretKey = keyGen.generateKey();
    
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
            System.out.println("Encrypted: " + encryptedText);
    
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            String decryptedText = new String(decryptedBytes);
            System.out.println("Decrypted: " + decryptedText);
        }
    }
    
  • 非对称加密

    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import javax.crypto.Cipher;
    import java.util.Base64;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String text = "Hello, World!";
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            KeyPair keyPair = keyGen.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
    
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
            System.out.println("Encrypted: " + encryptedText);
    
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
            String decryptedText = new String(decryptedBytes);
            System.out.println("Decrypted: " + decryptedText);
        }
    }
    

3.2 数字签名
  • 生成签名
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Signature;
    import java.util.Base64;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String text = "Hello, World!";
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            KeyPair keyPair = keyGen.generateKeyPair();
            PrivateKey privateKey = keyPair.getPrivate();
            PublicKey publicKey = keyPair.getPublic();
    
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(privateKey);
            signature.update(text.getBytes());
            byte[] signatureBytes = signature.sign();
            String signatureString = Base64.getEncoder().encodeToString(signatureBytes);
            System.out.println("Signature: " + signatureString);
    
            signature.initVerify(publicKey);
            signature.update(text.getBytes());
            boolean isVerified = signature.verify(Base64.getDecoder().decode(signatureString));
            System.out.println("Signature verified: " + isVerified);
        }
    }
    

4. 性能调优
4.1 内存管理
  • JVM 垃圾回收:自动管理内存,通过垃圾回收机制释放不再使用的对象所占用的内存。
  • 内存调优工具:使用 jvisualvmjconsole 等工具进行内存分析和调优。
4.2 多线程优化
  • 线程池:使用 Executors 创建线程池,提高多线程程序的性能。
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Main {
        public static void main(String[] args) {
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            for (int i = 0; i < 100; i++) {
                executorService.submit(() -> {
                    System.out.println("Thread: " + Thread.currentThread().getName());
                });
            }
            executorService.shutdown();
        }
    }
    

小结
  • 本课学习了 Java 的模块化编程,包括模块的定义和使用。
  • 介绍了国际化和本地化,使应用程序支持多种语言和地区。
  • 探讨了安全编程技术,包括加密解密和数字签名。
  • 学习了性能调优的方法,包括内存管理和多线程优化。
  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值