mirror of
https://github.com/Estom/notes.git
synced 2026-02-11 06:15:45 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
83
Java/JavaDemo/jdk-demo/DynamicProxy/pom.xml
Normal file
83
Java/JavaDemo/jdk-demo/DynamicProxy/pom.xml
Normal file
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>JdkDemo</artifactId>
|
||||
<groupId>org.example</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>DynamicProxy</artifactId>
|
||||
|
||||
<name>DynamicProxy</name>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<url>http://www.example.com</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ykl.proxy;
|
||||
|
||||
import com.ykl.proxy.handler.LogInterceptor;
|
||||
import com.ykl.proxy.service.UserService;
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
|
||||
public class CglibDynamicProxy {
|
||||
|
||||
public static void main(String[] args) {
|
||||
LogInterceptor logInterceptor = new LogInterceptor();
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(UserService.class); // 设置超类,cglib是通过继承来实现的
|
||||
enhancer.setCallback(logInterceptor);
|
||||
UserService userService = (UserService)enhancer.create(); // 创建代理类
|
||||
|
||||
|
||||
userService.update();
|
||||
userService.select();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ykl.proxy;
|
||||
|
||||
import com.ykl.proxy.handler.LogHandler;
|
||||
import com.ykl.proxy.service.UserService;
|
||||
import com.ykl.proxy.service.UserServiceImpl;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
public class DynamicProxy {
|
||||
|
||||
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
|
||||
// 设置变量可以保存动态代理类,默认名称以 $Proxy0 格式命名
|
||||
// System.getProperties().setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
|
||||
// 1. 创建被代理的对象,UserService接口的实现类
|
||||
UserServiceImpl userServiceImpl = new UserServiceImpl();
|
||||
// 2. 获取对应的 ClassLoader
|
||||
ClassLoader classLoader = userServiceImpl.getClass().getClassLoader();
|
||||
// 3. 获取所有接口的Class,这里的UserServiceImpl只实现了一个接口UserService,
|
||||
Class[] interfaces = userServiceImpl.getClass().getInterfaces();
|
||||
// 4. 创建一个将传给代理类的调用请求处理器,处理所有的代理对象上的方法调用
|
||||
// 这里创建的是一个自定义的日志处理器,须传入实际的执行对象 userServiceImpl
|
||||
InvocationHandler logHandler = new LogHandler(userServiceImpl);
|
||||
/*
|
||||
5.根据上面提供的信息,创建代理对象 在这个过程中,
|
||||
a.JDK会通过根据传入的参数信息动态地在内存中创建和.class 文件等同的字节码
|
||||
b.然后根据相应的字节码转换成对应的class,
|
||||
c.然后调用newInstance()创建代理实例
|
||||
*/
|
||||
UserService proxy = (UserService) Proxy.newProxyInstance(classLoader, interfaces, logHandler);
|
||||
// 调用代理的方法
|
||||
proxy.select();
|
||||
proxy.update();
|
||||
|
||||
// 保存JDK动态代理生成的代理类,类名保存为 UserServiceProxy
|
||||
// ProxyUtils.generateClassFile(userServiceImpl.getClass(), "UserServiceProxy");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.ykl.proxy.handler;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
public class LogHandler implements InvocationHandler {
|
||||
Object target; // 被代理的对象,实际的方法执行者
|
||||
|
||||
public LogHandler(Object target) {
|
||||
this.target = target;
|
||||
}
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
before();
|
||||
Object result = method.invoke(target, args); // 调用 target 的 method 方法
|
||||
after();
|
||||
return result; // 返回方法的执行结果
|
||||
}
|
||||
// 调用invoke方法之前执行
|
||||
private void before() {
|
||||
System.out.println(String.format("log start time [%s] ", new Date()));
|
||||
}
|
||||
// 调用invoke方法之后执行
|
||||
private void after() {
|
||||
System.out.println(String.format("log end time [%s] ", new Date()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.ykl.proxy.handler;
|
||||
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
public class LogInterceptor implements MethodInterceptor {
|
||||
|
||||
/**
|
||||
* @param object 表示要进行增强的对象
|
||||
* @param method 表示拦截的方法
|
||||
* @param objects 数组表示参数列表,基本数据类型需要传入其包装类型,如int-->Integer、long-Long、double-->Double
|
||||
* @param methodProxy 表示对方法的代理,invokeSuper方法表示对被代理对象方法的调用
|
||||
* @return 执行结果
|
||||
* @throws Throwable
|
||||
*/
|
||||
@Override
|
||||
public Object intercept(Object object, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
|
||||
before();
|
||||
Object result = methodProxy.invokeSuper(object, objects); // 注意这里是调用 invokeSuper 而不是 invoke,否则死循环,methodProxy.invokesuper执行的是原始类的方法,method.invoke执行的是子类的方法
|
||||
after();
|
||||
return result;
|
||||
}
|
||||
private void before() {
|
||||
System.out.println(String.format("log start time [%s] ", new Date()));
|
||||
}
|
||||
private void after() {
|
||||
System.out.println(String.format("log end time [%s] ", new Date()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ykl.proxy.service;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void select();
|
||||
|
||||
void update();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ykl.proxy.service;
|
||||
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public void select() {
|
||||
System.out.println("selecting");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
System.out.println("updating");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.example;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
79
Java/JavaDemo/jdk-demo/pom.xml
Normal file
79
Java/JavaDemo/jdk-demo/pom.xml
Normal file
@@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>JdkDemo</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<modules>
|
||||
<module>DynamicProxy</module>
|
||||
</modules>
|
||||
|
||||
<name>JdkDemo</name>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<url>http://www.example.com</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
||||
13
Java/JavaDemo/jdk-demo/src/main/java/org/example/App.java
Normal file
13
Java/JavaDemo/jdk-demo/src/main/java/org/example/App.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package org.example;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.example;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
{
|
||||
/**
|
||||
* Rigorous Test :-)
|
||||
*/
|
||||
@Test
|
||||
public void shouldAnswerWithTrue()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user