java maven mvn

发布时间:2026/7/24 20:31:25

java maven mvn 使用命令行javac foo/bar/Main.java java foo.bar.Main反射机制反射技术可以操作私有属性私有方法Class cls Monkey.class; Monkey monkey new Monkey(); Class cls monkey.getClass(); Class cls Class.forName(foo.bar.Monkey);import java.lang.reflect.Field; public class Main { public static void main(String[] args) { try { Monkey monkey new Monkey(); Class? cls Monkey.class; Field field cls.getDeclaredField(sex); field.setAccessible(true); field.set(monkey, 女); String sex (String)field.get(monkey); System.out.println(sex); } catch(Exception e) { e.printStackTrace(); } } }import java.lang.reflect.Method; public class Main { public static void main(String[] args) { try { Monkey monkey new Monkey(); Class? cls Monkey.class; Method setMethod cls.getDeclaredMethod(setSex, String.class); setMethod.setAccessible(true); Method getMethod cls.getDeclaredMethod(getSex); getMethod.setAccessible(true); setMethod.invoke(monkey, 男); String sex (String)getMethod.invoke(monkey); System.out.println(sex); } catch(Exception e) { e.printStackTrace(); } } }设计模式单例模式当应用程序只需要构造类的一个实例懒汉模式/** * 等程序需要时再初始化实例可节省资源 */ public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if (instance null) { instance new Singleton(); } return instance; } }饿汉模式/** * 确保线程安全 */ public class Singleton { private static Singleton instance new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } }工厂模式简单工厂模式使用一个单独的工厂类来创建不同的对象public class PizzaFactory { public Pizza createPizza(String type) { Pizza pizza null; if(type.equals(cheeze)) { pizza new CheezePizza(); } return pizza; } } class PizzaStore { public Pizza orderPizza() { Pizza pizza; pizza factory.createPizza(cheeze); pizza.prepare(); pizza.bake(); return pizza; } }工厂方法模式工厂方法将对象的创建延迟到子类public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; pizza createPizza(type); pizza.prepare(); pizza.bake(); } public abstract Pizza createPizza(String type); }抽象工厂模式抽象工厂模式提供一个创建一系列相关或互相依赖对象的接口而无需指定它们具体的类public abstract class Store { public abstract Pizza createPizza(String type); public abstract Coke createCoke(String type); }maven用法pom.xmlproject properties /properties dependencies /dependencies build extensions /extensions plugins /plugins /build /projectproperties maven.compiler.source21/maven.compiler.source maven.compiler.target21/maven.compiler.target /propertiesmaven生命周期mvn clean # 清理项目删除target文件夹不会删除本地仓库mvn compile # 把.java编译成.class并输出在target文件夹中mvn test # 运行测试代码mvn package # 把项目打包成jar输出在target文件夹中mvn install # 把项目安装在本地仓库中.m2文件夹)mvn deploy # 推送到远程仓库maven命令行mvn archetype:generate # 通过模板创建新项目 mvn dependency:tree # 查看依赖 mvn dependency:resolve # 下载依赖 mvn clean clover:setup test clover:aggregate clover:clover # clover生成test覆盖率报告 # mvn plugin-prefix:goal # pitest生成代码突变报告和覆盖率报告 mvn clean test-compile org.pitest:pitest-maven:mutationCoverage # 自动生成测试代码 项目根目录/.evosuite mvn -DmemoryInMB2500 -Dcores5 evosuite:generatemaven plugin使用maven名词解释groupId是开发人员artifactId是项目名字版本号后的SNAPSHOT意思是处于开发阶段jdbcConnection conn DriverManager.getConnection(jdbc:mysql://ip:3306/db, user, password); String sql INSERT INTO user_info (password, name, email, phone, age, gender, birthday, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?); PreparedStatement stmt conn.prepareStatement(sql); Random random new Random(); for (long i 0; i 100_000; i) { int strlen random.nextInt(8, 32); String password RandomStringUtils.insecure().nextAscii(strlen); stmt.setString(1, password); strlen random.nextInt(8, 32); String name RandomStringUtils.insecure().nextAscii(strlen); stmt.setString(2, name); strlen random.nextInt(8, 32); String email RandomStringUtils.insecure().nextAscii(strlen); stmt.setString(3, email); String phone RandomStringUtils.insecure().nextAscii(11); stmt.setString(4, phone); int age random.nextInt(1, 90); stmt.setInt(5, age); int gender random.nextInt(0, 2); stmt.setInt(6, gender); long birthday random.nextLong(1_700_000_000_000L); stmt.setLong(7, birthday); strlen random.nextInt(128, 512); String address RandomStringUtils.insecure().nextAscii(strlen); stmt.setString(8, address); stmt.executeUpdate(); }EclipseEclipse 是一个 Java 应用程序依赖于 JVM 来执行。Eclipse默认使用的是jre的运行环境所以找不到jdk的包解决方法编辑Eclipse安装目录下面的eclipse.ini在”-vmargs”前添加下面代码保存、重启eclipse后Update Project即可。-vm your_jdk_path\jre\bin\server\jvm.dll-vm是 Eclipse 配置文件eclipse.ini中的一个选项它指定了 Eclipse 使用的 Java 虚拟机mvn input # 与上述图等价多线程# 自定义线程池 (常驻线程, 最大线程, 线程过期时间, 时间单位, 存储队列) ExecutorService pool new ThreadPoolExecutor(3, 10, 1, TimeUnit.SECONDS, queue); ExecutorService fixedPool Executors.newFixedThreadPool(5); # 大小无限制 ExecutorService cachedPool Executors.newCachedThreadPool(); ExecutorService singleThreadPool Executors.newSingleThreadExecutor();pool.submit(Runnable); # 有返回值 不阻塞 pool.execute(Runnable); # 无返回值 不阻塞 pool.shutdown(); # 原有任务会继续执行FutureString future pool.submit(Callable); future.get(); # 会阻塞java.utilArrays.sort(points, (o1, o2) - Integer.compare(o1[0], o2[0])); # 排序 原地javadoccd src/main/java javadoc -d 输出目录 com.alibaba.csp.sentinel # 对子包无效垃圾回收

相关新闻