diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4329286 --- /dev/null +++ b/.gitignore @@ -0,0 +1,57 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ +target +.svn +*.log +*/*.log +.DS_Store +*/velocity.log.* +/out +/.idea/* +/.idea/ant.xml +/.idea/misc.xml +/.idea/workspace.xml +/.idea/compiler.xml +/.idea/modules.xml +/.idea/libraries/Maven*.xml +/.idea/artifacts/*.xml +/*.iws +/ais-hardware.eml +tiangong-js +rebel.xml +.husx +/.husx/docs/bash/ +/.husx/docs/bash/*/*.sh +/.husx/docs/bash/*/*/*.sh +/web/src/test/java/com/cmcc/coc/ummp/check/Test.java diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 0000000..a45eb6b --- /dev/null +++ b/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000..2cc7d4a Binary files /dev/null and b/.mvn/wrapper/maven-wrapper.jar differ diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..ffdc10e --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/README.md b/README.md deleted file mode 100644 index dc2971f..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# javaEE -基i础 test is git diff --git a/gateway/pom.xml b/gateway/pom.xml new file mode 100644 index 0000000..7ddac45 --- /dev/null +++ b/gateway/pom.xml @@ -0,0 +1,96 @@ + + + + test + com.xgh + 0.0.1-SNAPSHOT + + 4.0.0 + + gateway + + + 8 + 8 + UTF-8 + + + + io.netty + netty-all + 4.1.77.Final + + + com.alibaba + fastjson + 2.0.7 + + + org.slf4j + slf4j-api + 1.7.36 + jar + compile + + + ch.qos.logback + logback-core + 1.2.11 + jar + + + ch.qos.logback + logback-classic + 1.2.11 + jar + + + junit + junit + 4.13.2 + test + + + org.apache.dubbo + dubbo + 2.7.5 + + + org.apache.zookeeper + zookeeper + 3.4.13 + + + org.apache.curator + curator-framework + 4.0.1 + + + org.apache.curator + curator-recipes + 4.0.1 + + + cglib + cglib + 3.3.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${maven.compiler.source} + ${maven.compiler.target} + ${project.build.sourceEncoding} + + + + + \ No newline at end of file diff --git a/gateway/src/main/java/com/xgh/gateway/day01/BaseHandler.java b/gateway/src/main/java/com/xgh/gateway/day01/BaseHandler.java new file mode 100644 index 0000000..b583402 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day01/BaseHandler.java @@ -0,0 +1,19 @@ +package com.xgh.gateway.day01; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +/** + * 数据处理器 基类 + * @param + */ +public abstract class BaseHandler extends SimpleChannelInboundHandler { + + @Override + protected void channelRead0(ChannelHandlerContext ctx, T msg) throws Exception { + session(ctx,ctx.channel(),msg); + } + + protected abstract void session(ChannelHandlerContext ctx, final Channel channel, T request); +} diff --git a/gateway/src/main/java/com/xgh/gateway/day01/SessionChannelInitializer.java b/gateway/src/main/java/com/xgh/gateway/day01/SessionChannelInitializer.java new file mode 100644 index 0000000..c8f3477 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day01/SessionChannelInitializer.java @@ -0,0 +1,24 @@ +package com.xgh.gateway.day01; + +import com.xgh.gateway.day01.handlers.SessionServerHandler; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; + + +public class SessionChannelInitializer extends ChannelInitializer { + + @Override + protected void initChannel(SocketChannel channel) throws Exception { + ChannelPipeline line = channel.pipeline(); + line.addLast(new HttpRequestDecoder()) + .addLast(new HttpResponseEncoder()) + .addLast(new HttpObjectAggregator(1024*1024)) + .addLast(new SessionServerHandler()); + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day01/SessionServer.java b/gateway/src/main/java/com/xgh/gateway/day01/SessionServer.java new file mode 100644 index 0000000..5c4e0a4 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day01/SessionServer.java @@ -0,0 +1,54 @@ +package com.xgh.gateway.day01; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +import java.util.concurrent.Callable; + + +/** + * 网关会话服务 + * @author xgh + */ +public class SessionServer implements Callable { + + private final Logger logger = LoggerFactory.getLogger(SessionServer.class); + + private final EventLoopGroup boss = new NioEventLoopGroup(1); + + private final EventLoopGroup work = new NioEventLoopGroup(); + + private Channel channel; + + + @Override + public Channel call() throws Exception { + ChannelFuture channelFuture = null; + try { + ServerBootstrap b = new ServerBootstrap(); + b.group(boss,work) + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG,128) + .childHandler(new SessionChannelInitializer()); + channelFuture = b.bind(7387).syncUninterruptibly(); + this.channel = channelFuture.channel(); + }catch (Exception e){ + logger.error("socket server start error.",e); + }finally { + if(null != channelFuture || channelFuture.isSuccess()){ + logger.error("socket server start done."); + }else { + logger.error("socket server start error."); + } + } + return channel; + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day01/handlers/SessionServerHandler.java b/gateway/src/main/java/com/xgh/gateway/day01/handlers/SessionServerHandler.java new file mode 100644 index 0000000..2ef899d --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day01/handlers/SessionServerHandler.java @@ -0,0 +1,42 @@ +package com.xgh.gateway.day01.handlers; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.xgh.gateway.day01.BaseHandler; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 会话处理器 + */ +public class SessionServerHandler extends BaseHandler { + + private final Logger logger = LoggerFactory.getLogger(SessionServerHandler.class); + + @Override + protected void session(ChannelHandlerContext ctx, Channel channel, FullHttpRequest request) { + logger.info("网关接收请求uri:{} method:{}",request.uri(),request.method()); + //返回信息处理 + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + //返回信息控制 + response.content().writeBytes(JSON.toJSONBytes("你访问的路径被网关管理了uri:"+request.uri(), SerializerFeature.PrettyFormat)); + //设置头部信息 + HttpHeaders headers = response.headers(); + //返回内容类型 + headers.add(HttpHeaderNames.CONTENT_TYPE,HttpHeaderValues.APPLICATION_JSON+";charset=UTF-8"); + //响应体长度 + headers.add(HttpHeaderNames.CONTENT_LENGTH,response.content().readableBytes()); + //配置链接持久 + headers.add(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE); + //配置跨域 + headers.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN,"*"); + headers.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,"*"); + headers.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS,"GET, POST, PUT, DELETE"); + headers.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS,"true"); + + channel.writeAndFlush(response); + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxy.java b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxy.java new file mode 100644 index 0000000..92c8abb --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxy.java @@ -0,0 +1,45 @@ +package com.xgh.gateway.day02.bind; + +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; +import org.apache.dubbo.rpc.service.GenericService; + +import java.lang.reflect.Method; + +/** + * 泛化调用静态代理:方便做一些拦截处理,给http对应rpc调用,做一层代理控制 + * 每调用到一个http对应的网关方法,就会代理的方式调用到rpc对应的泛化方法上 + */ +public class GenericReferenceProxy implements MethodInterceptor { + + /** + * rpc泛化调用服务 + */ + private final GenericService genericService; + + /** + * rpc 泛化调用方法 + */ + private final String methodName; + + public GenericReferenceProxy(GenericService genericService, String methodName) { + this.genericService = genericService; + this.methodName = methodName; + } + + + /** + * + * 做一层代理控制,后续不止是可以使用 Dubbo 泛化调用,也可以是其他服务的泛化调用 + * 泛化调用文档:https://dubbo.apache.org/zh/docsv2.7/user/examples/generic-reference/ + */ + @Override + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { + Class[] parameterTypes = method.getParameterTypes(); + String[] parameters = new String[parameterTypes.length]; + for (int i = 0; i < parameterTypes.length; i++) { + parameters[i] = parameterTypes[i].getName(); + } + return genericService.$invoke(methodName,parameters,args); + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxyFactory.java b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxyFactory.java new file mode 100644 index 0000000..62fe409 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceProxyFactory.java @@ -0,0 +1,51 @@ +package com.xgh.gateway.day02.bind; + + +import net.sf.cglib.core.Signature; +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.InterfaceMaker; +import org.apache.dubbo.rpc.service.GenericService; +import org.objectweb.asm.Type; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 泛化调用静态代理工厂 + */ +public class GenericReferenceProxyFactory { + + /** + * rpc 泛化调用服务 + */ + private final GenericService genericService; + + + private final Map genericReferenceCache = new ConcurrentHashMap<>(); + + + public GenericReferenceProxyFactory(GenericService genericService) { + this.genericService = genericService; + } + + public IGenericReference newInstance(String method){ + return genericReferenceCache.computeIfAbsent(method,k->{ + //泛化调用 + GenericReferenceProxy genericReferenceProxy = new GenericReferenceProxy(genericService, method); + //创建接口 + InterfaceMaker interfaceMaker = new InterfaceMaker(); + interfaceMaker.add(new Signature(method, Type.getType(String.class),new Type[]{ Type.getType(String.class)}),null); + Class interfaceClass = interfaceMaker.create(); + //代理对象 + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(Object.class); + //IGenericReference 统一泛化接口 + //interfaceClass 根据泛化调用注册信息创建的接口,建立http->rpc关联 + enhancer.setInterfaces(new Class[]{IGenericReference.class,interfaceClass}); + enhancer.setCallback(genericReferenceProxy); + return (IGenericReference) enhancer.create(); + }); + } + + +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceRegistry.java b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceRegistry.java new file mode 100644 index 0000000..c569d31 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/bind/GenericReferenceRegistry.java @@ -0,0 +1,52 @@ +package com.xgh.gateway.day02.bind; + +import com.xgh.gateway.day02.session.Configuration; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.utils.ReferenceConfigCache; +import org.apache.dubbo.rpc.service.GenericService; + +import java.util.HashMap; +import java.util.Map; + +/** + * 泛化调用注册器 + */ +public class GenericReferenceRegistry { + + private final Configuration configuration; + + + public GenericReferenceRegistry(Configuration configuration) { + this.configuration = configuration; + } + + private final Map knownGenericReferences = new HashMap<>(); + + public IGenericReference getGenericReference(String methodName){ + GenericReferenceProxyFactory genericReferenceProxyFactory = knownGenericReferences.get(methodName); + if(genericReferenceProxyFactory == null){ + throw new RuntimeException("Type "+methodName +"is not know to the GenericReferenceRegistry."); + } + return genericReferenceProxyFactory.newInstance(methodName); + } + + + public void addGenericReference(String application,String interfaceName,String methodName){ + //获取基础服务 + ApplicationConfig applicationConfig = configuration.getApplicationConfig(application); + RegistryConfig registryConfig = configuration.getRegistryConfig(application); + ReferenceConfig referenceConfig = configuration.getReferenceConfig(interfaceName); + //构建dubbo服务 + DubboBootstrap bootstrap = DubboBootstrap.getInstance(); + bootstrap.application(applicationConfig).registry(registryConfig).reference(referenceConfig).start(); + //调用泛化服务 + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + GenericService genericService = cache.get(referenceConfig); + //创建并保存到工厂 + knownGenericReferences.put(methodName,new GenericReferenceProxyFactory(genericService)); + } + +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/bind/IGenericReference.java b/gateway/src/main/java/com/xgh/gateway/day02/bind/IGenericReference.java new file mode 100644 index 0000000..a71538f --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/bind/IGenericReference.java @@ -0,0 +1,9 @@ +package com.xgh.gateway.day02.bind; + +/** + * 统一泛化接口 + */ +public interface IGenericReference { + + String $invoke(String args); +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/BaseHandler.java b/gateway/src/main/java/com/xgh/gateway/day02/session/BaseHandler.java new file mode 100644 index 0000000..3253e05 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/BaseHandler.java @@ -0,0 +1,20 @@ +package com.xgh.gateway.day02.session; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + +/** + * 数据处理基类 + * @param + */ +public abstract class BaseHandler extends SimpleChannelInboundHandler { + + + @Override + protected void channelRead0(ChannelHandlerContext ctx, T msg) throws Exception { + session(ctx,ctx.channel(),msg); + } + + protected abstract void session(ChannelHandlerContext ctx, Channel channel, T request); +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/Configuration.java b/gateway/src/main/java/com/xgh/gateway/day02/session/Configuration.java new file mode 100644 index 0000000..9790beb --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/Configuration.java @@ -0,0 +1,75 @@ +package com.xgh.gateway.day02.session; + + +import com.xgh.gateway.day02.bind.GenericReferenceRegistry; +import com.xgh.gateway.day02.bind.IGenericReference; +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.rpc.service.GenericService; + +import java.util.HashMap; +import java.util.Map; + +/** + * 会话生命周期配置类 + */ +public class Configuration { + + private final GenericReferenceRegistry registry = new GenericReferenceRegistry(this); + + //rpc 应用服务配置项api-gateway-test + private final Map applicationConfigMap = new HashMap<>(); + + /** + * 注册中心配置项 zookeeper:127.0.0.1:2181 + */ + private final Map registryConfigMap = new HashMap<>(); + + /** + * rpc 泛化服务配置项目 cn.gateway.rpc.IActivityBooth + */ + private final Map> referenceConfigMap = new HashMap<>(); + + + public Configuration(){ + ApplicationConfig application = new ApplicationConfig(); + application.setName("api-gateway-test"); + application.setQosEnable(false); + + RegistryConfig registryConfig = new RegistryConfig(); + registryConfig.setAddress("zookeeper://127.0.0.1:2181"); + registryConfig.setRegister(false); + + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(""); + reference.setVersion("1.0.1"); + reference.setGeneric("true"); + + applicationConfigMap.put("api-gateway-test",application); + registryConfigMap.put("api-gateway-test",registryConfig); + referenceConfigMap.put("",reference); + } + + public ApplicationConfig getApplicationConfig(String applicationName){ + return applicationConfigMap.get(applicationName); + } + + public RegistryConfig getRegistryConfig(String applicationName){ + return registryConfigMap.get(applicationName); + } + + public ReferenceConfig getReferenceConfig(String interfaceName){ + return referenceConfigMap.get(interfaceName); + } + + + public void addGenericReference(String application,String interfaceName,String methodName){ + registry.addGenericReference(application,interfaceName,methodName); + } + + public IGenericReference getGenericReference(String methodName){ + return registry.getGenericReference(methodName); + } + +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/GenericReferenceSessionFactoryBuilder.java b/gateway/src/main/java/com/xgh/gateway/day02/session/GenericReferenceSessionFactoryBuilder.java new file mode 100644 index 0000000..5f9165b --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/GenericReferenceSessionFactoryBuilder.java @@ -0,0 +1,25 @@ +package com.xgh.gateway.day02.session; + + +import com.xgh.gateway.day02.session.defaults.GenericReferenceSessionFactory; +import io.netty.channel.Channel; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +/** + * 会话工厂构建类 + */ +public class GenericReferenceSessionFactoryBuilder { + + public Future build(Configuration configuration) { + IGenericReferenceSessionFactory genericReferenceSessionFactory = new GenericReferenceSessionFactory(configuration); + try { + return genericReferenceSessionFactory.openSession(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/IGenericReferenceSessionFactory.java b/gateway/src/main/java/com/xgh/gateway/day02/session/IGenericReferenceSessionFactory.java new file mode 100644 index 0000000..d0cc1b0 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/IGenericReferenceSessionFactory.java @@ -0,0 +1,17 @@ +package com.xgh.gateway.day02.session; + +import io.netty.channel.Channel; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + + +/** + * 泛化调用工厂接口 + */ +public interface IGenericReferenceSessionFactory { + + Future openSession() throws ExecutionException, InterruptedException; + + +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/SessionChannelInitializer.java b/gateway/src/main/java/com/xgh/gateway/day02/session/SessionChannelInitializer.java new file mode 100644 index 0000000..e0e9141 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/SessionChannelInitializer.java @@ -0,0 +1,28 @@ +package com.xgh.gateway.day02.session; + +import com.xgh.gateway.day02.session.handlers.SessionServerHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; + +/** + * 会话管道初始化类 + */ +public class SessionChannelInitializer extends ChannelInitializer { + + private final Configuration configuration; + + public SessionChannelInitializer(Configuration configuration) { + this.configuration = configuration; + } + + @Override + protected void initChannel(SocketChannel channel) throws Exception { + channel.pipeline().addLast(new HttpRequestDecoder()) + .addLast(new HttpResponseEncoder()) + .addLast(new HttpObjectAggregator(1024*1024)) + .addLast(new SessionServerHandler(configuration)); + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/SessionServer.java b/gateway/src/main/java/com/xgh/gateway/day02/session/SessionServer.java new file mode 100644 index 0000000..71fda8e --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/SessionServer.java @@ -0,0 +1,59 @@ +package com.xgh.gateway.day02.session; + + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetSocketAddress; +import java.util.concurrent.Callable; + +/** + * 网关会话服务 + */ +public class SessionServer implements Callable { + + private final Logger logger = LoggerFactory.getLogger(SessionServer.class); + + private final Configuration configuration; + + private final EventLoopGroup boss = new NioEventLoopGroup(1); + + private final EventLoopGroup work = new NioEventLoopGroup(); + + private Channel channel; + + public SessionServer(Configuration configuration) { + this.configuration = configuration; + } + + + @Override + public Channel call() throws Exception { + ChannelFuture channelFuture = null; + try { + ServerBootstrap b =new ServerBootstrap(); + b.group(boss,work) + .channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG,128) + .childHandler(new SessionChannelInitializer(configuration)); + channelFuture = b.bind(new InetSocketAddress(7397)).syncUninterruptibly(); + this.channel = channelFuture.channel(); + }catch (Exception e){ + logger.error("socket server start error .",e); + }finally { + if(null != channelFuture && channelFuture.isSuccess()){ + logger.info("socket server start done."); + }else { + logger.error("socket server start error."); + } + } + return channel; + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/defaults/GenericReferenceSessionFactory.java b/gateway/src/main/java/com/xgh/gateway/day02/session/defaults/GenericReferenceSessionFactory.java new file mode 100644 index 0000000..92135d6 --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/defaults/GenericReferenceSessionFactory.java @@ -0,0 +1,39 @@ +package com.xgh.gateway.day02.session.defaults; + +import com.xgh.gateway.day02.session.Configuration; +import com.xgh.gateway.day02.session.IGenericReferenceSessionFactory; +import com.xgh.gateway.day02.session.SessionServer; +import io.netty.channel.Channel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class GenericReferenceSessionFactory implements IGenericReferenceSessionFactory { + + private final Logger logger = LoggerFactory.getLogger(GenericReferenceSessionFactory.class); + + private final Configuration configuration; + + public GenericReferenceSessionFactory(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public Future openSession() throws ExecutionException, InterruptedException { + SessionServer server = new SessionServer(configuration); + Future future = Executors.newFixedThreadPool(2).submit(server); + Channel channel = future.get(); + if(null == channel){ + throw new RuntimeException("netty server start error channel is null"); + } + while (!channel.isActive()){ + logger.info("netty server gateway start ing ..."); + Thread.sleep(500); + } + logger.info("netty server gateway start Done:{}",channel.localAddress()); + return future; + } +} diff --git a/gateway/src/main/java/com/xgh/gateway/day02/session/handlers/SessionServerHandler.java b/gateway/src/main/java/com/xgh/gateway/day02/session/handlers/SessionServerHandler.java new file mode 100644 index 0000000..b29226a --- /dev/null +++ b/gateway/src/main/java/com/xgh/gateway/day02/session/handlers/SessionServerHandler.java @@ -0,0 +1,58 @@ +package com.xgh.gateway.day02.session.handlers; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import com.xgh.gateway.day02.bind.IGenericReference; +import com.xgh.gateway.day02.session.BaseHandler; +import com.xgh.gateway.day02.session.Configuration; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 会话服务处理器 + */ +public class SessionServerHandler extends BaseHandler { + + private final Logger logger = LoggerFactory.getLogger(SessionServerHandler.class); + + private final Configuration configuration; + + public SessionServerHandler(Configuration configuration) { + this.configuration = configuration; + } + + + @Override + protected void session(ChannelHandlerContext ctx, Channel channel, FullHttpRequest request) { + logger.info("网关接收请求uri:{},method:{}",request.uri(),request.method()); + //返回信息控制 + String methodName = request.uri().substring(1); + if(methodName.equals("favicon.ico")){return;} + //返回信息处理 + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + //服务泛化调用 + IGenericReference reference = configuration.getGenericReference("sayHi"); + String result = reference.$invoke("test)" + " " + System.currentTimeMillis()); + + //设置回写数据 + response.content().writeBytes(JSON.toJSONBytes(result, SerializerFeature.PrettyFormat)); + //头部信息设置 + HttpHeaders heads = response.headers(); + //返回内容类型 + heads.add(HttpHeaderNames.CONTENT_TYPE,HttpHeaderValues.APPLICATION_JSON+";charset=UTF-8"); + //响应体长度 + heads.add(HttpHeaderNames.CONTENT_LENGTH,response.content().readableBytes()); + //配置持久化 + heads.add(HttpHeaderNames.CONNECTION,HttpHeaderValues.KEEP_ALIVE); + //配置跨域 + heads.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); + heads.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "*"); + heads.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE"); + heads.add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); + + channel.writeAndFlush(response); + } +} diff --git a/gateway/src/test/java/com/xgh/gateway/day01/ApiTest.java b/gateway/src/test/java/com/xgh/gateway/day01/ApiTest.java new file mode 100644 index 0000000..d2b54f0 --- /dev/null +++ b/gateway/src/test/java/com/xgh/gateway/day01/ApiTest.java @@ -0,0 +1,37 @@ +package com.xgh.gateway.day01; + +import io.netty.channel.Channel; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @description 测试 + */ +public class ApiTest { + + private final Logger logger = LoggerFactory.getLogger(ApiTest.class); + + @Test + public void test() throws ExecutionException, InterruptedException { + SessionServer server = new SessionServer(); + + Future future = Executors.newFixedThreadPool(2).submit(server); + Channel channel = future.get(); + + if (null == channel) throw new RuntimeException("netty server start error channel is null"); + + while (!channel.isActive()) { + logger.info("NettyServer启动服务 ..."); + Thread.sleep(500); + } + logger.info("NettyServer启动服务完成 {}", channel.localAddress()); + + Thread.sleep(Long.MAX_VALUE); + } + +} \ No newline at end of file diff --git a/gateway/src/test/java/com/xgh/gateway/day02/ApiTest.java b/gateway/src/test/java/com/xgh/gateway/day02/ApiTest.java new file mode 100644 index 0000000..aa59e98 --- /dev/null +++ b/gateway/src/test/java/com/xgh/gateway/day02/ApiTest.java @@ -0,0 +1,30 @@ +package com.xgh.gateway.day02; + +import com.xgh.gateway.day02.session.Configuration; +import com.xgh.gateway.day02.session.GenericReferenceSessionFactoryBuilder; +import io.netty.channel.Channel; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + + +public class ApiTest { + + private final Logger logger = LoggerFactory.getLogger(ApiTest.class); + + @Test + public void test_GenericReference() throws InterruptedException, ExecutionException { + Configuration configuration = new Configuration(); + configuration.addGenericReference("api-gateway-test", "cn.bugstack.gateway.rpc.IActivityBooth", "sayHi"); + + GenericReferenceSessionFactoryBuilder builder = new GenericReferenceSessionFactoryBuilder(); + Future future = builder.build(configuration); + + logger.info("服务启动完成 {}", future.get().id()); + + Thread.sleep(Long.MAX_VALUE); + } +} \ No newline at end of file diff --git a/gateway/src/test/java/com/xgh/gateway/day02/CglibTest.java b/gateway/src/test/java/com/xgh/gateway/day02/CglibTest.java new file mode 100644 index 0000000..98b4361 --- /dev/null +++ b/gateway/src/test/java/com/xgh/gateway/day02/CglibTest.java @@ -0,0 +1,43 @@ +package com.xgh.gateway.day02; + +import com.alibaba.fastjson.JSON; +import net.sf.cglib.core.Signature; +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.InterfaceMaker; +import net.sf.cglib.proxy.MethodInterceptor; +import net.sf.cglib.proxy.MethodProxy; +import org.junit.Test; +import org.objectweb.asm.Type; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class CglibTest implements MethodInterceptor { + + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + return JSON.toJSONString(objects); + } + + @Test + public void test_cglib() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { + // 定义接口 + InterfaceMaker interfaceMaker = new InterfaceMaker(); + interfaceMaker.add(new Signature("sayHi", Type.getType(String.class), new Type[]{Type.getType(String.class)}), null); + Class interfaceClass = interfaceMaker.create(); + + // 创建代理 + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(Object.class); + enhancer.setInterfaces(new Class[]{interfaceClass}); + enhancer.setCallback(this); + Object obj = enhancer.create(); + + // 调用方法 + Method method = obj.getClass().getMethod("sayHi", String.class); + Object result = method.invoke(obj,"hi xiaofuge"); + + System.out.println(result); + } + +} \ No newline at end of file diff --git a/gateway/src/test/java/com/xgh/gateway/day02/RPCTest.java b/gateway/src/test/java/com/xgh/gateway/day02/RPCTest.java new file mode 100644 index 0000000..c74b0b0 --- /dev/null +++ b/gateway/src/test/java/com/xgh/gateway/day02/RPCTest.java @@ -0,0 +1,47 @@ +package com.xgh.gateway.day02; + +import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.config.utils.ReferenceConfigCache; +import org.apache.dubbo.rpc.service.GenericService; +import org.junit.Test; + +/** + * 泛化调用测试 + * 官网案例:https://dubbo.apache.org/zh/docs/advanced/generic-reference/ + */ +public class RPCTest { + + @Test + public void test_rpc() { + + ApplicationConfig application = new ApplicationConfig(); + application.setName("api-gateway-test"); + application.setQosEnable(false); + + RegistryConfig registry = new RegistryConfig(); + registry.setAddress("zookeeper://127.0.0.1:2181"); + registry.setRegister(false); + + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface("cn.bugstack.gateway.rpc.IActivityBooth"); + reference.setVersion("1.0.0"); + reference.setGeneric("true"); + + DubboBootstrap bootstrap = DubboBootstrap.getInstance(); + bootstrap.application(application) + .registry(registry) + .reference(reference) + .start(); + + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + GenericService genericService = cache.get(reference); + + Object result = genericService.$invoke("sayHi", new String[]{"java.lang.String"}, new Object[]{"world"}); + + System.out.println(result); + } + +} diff --git a/mvnw b/mvnw new file mode 100644 index 0000000..3c8a553 --- /dev/null +++ b/mvnw @@ -0,0 +1,322 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ]; then + + if [ -f /etc/mavenrc ]; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ]; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false +darwin=false +mingw=false +case "$(uname)" in +CYGWIN*) cygwin=true ;; +MINGW*) mingw=true ;; +Darwin*) + darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="$(/usr/libexec/java_home)" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ]; then + if [ -r /etc/gentoo-release ]; then + JAVA_HOME=$(java-config --jre-home) + fi +fi + +if [ -z "$M2_HOME" ]; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ]; do + ls=$(ls -ld "$PRG") + link=$(expr "$ls" : '.*-> \(.*\)$') + if expr "$link" : '/.*' >/dev/null; then + PRG="$link" + else + PRG="$(dirname "$PRG")/$link" + fi + done + + saveddir=$(pwd) + + M2_HOME=$(dirname "$PRG")/.. + + # make it fully qualified + M2_HOME=$(cd "$M2_HOME" && pwd) + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=$(cygpath --unix "$M2_HOME") + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --unix "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --unix "$CLASSPATH") +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw; then + [ -n "$M2_HOME" ] && + M2_HOME="$( ( + cd "$M2_HOME" + pwd + ))" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="$( ( + cd "$JAVA_HOME" + pwd + ))" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="$(which javac)" + if [ -n "$javaExecutable" ] && ! [ "$(expr \"$javaExecutable\" : '\([^ ]*\)')" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=$(which readlink) + if [ ! $(expr "$readLink" : '\([^ ]*\)') = "no" ]; then + if $darwin; then + javaHome="$(dirname \"$javaExecutable\")" + javaExecutable="$(cd \"$javaHome\" && pwd -P)/javac" + else + javaExecutable="$(readlink -f \"$javaExecutable\")" + fi + javaHome="$(dirname \"$javaExecutable\")" + javaHome=$(expr "$javaHome" : '\(.*\)/bin') + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ]; then + if [ -n "$JAVA_HOME" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="$(which java)" + fi +fi + +if [ ! -x "$JAVACMD" ]; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ]; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ]; then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ]; do + if [ -d "$wdir"/.mvn ]; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=$( + cd "$wdir/.." + pwd + ) + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' <"$1")" + fi +} + +BASE_DIR=$(find_maven_basedir "$(pwd)") +if [ -z "$BASE_DIR" ]; then + exit 1 +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in wrapperUrl) + jarUrl="$value" + break + ;; + esac + done <"$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") + fi + + if command -v wget >/dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl >/dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=$(cygpath --path --windows "$javaClass") + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=$(cygpath --path --windows "$M2_HOME") + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --windows "$CLASSPATH") + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/mvnw.cmd b/mvnw.cmd new file mode 100644 index 0000000..c8d4337 --- /dev/null +++ b/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9e0ee98 --- /dev/null +++ b/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + pom + + gateway + + + org.springframework.boot + spring-boot-starter-parent + 2.4.5 + + + com.xgh + test + 0.0.1-SNAPSHOT + test + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + + org.springframework.boot + spring-boot + 2.4.5 + + + org.projectlombok + lombok + + + org.assertj + assertj-core + + + junit + junit + + + + com.google.guava + guava-testlib + 30.1.1-jre + test + + + cn.hutool + hutool-core + 5.6.3 + + + redis.clients + jedis + 2.7.1 + + + + + + + org.asciidoctor + asciidoctor-maven-plugin + 1.5.8 + + + generate-docs + prepare-package + + process-asciidoc + + + html + book + + + + + + org.springframework.restdocs + spring-restdocs-asciidoctor + ${spring-restdocs.version} + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/xgh/test/TestApplication.java b/src/main/java/com/xgh/test/TestApplication.java new file mode 100644 index 0000000..863fb1a --- /dev/null +++ b/src/main/java/com/xgh/test/TestApplication.java @@ -0,0 +1,13 @@ +package com.xgh.test; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class TestApplication { + + public static void main(String[] args) { + SpringApplication.run(TestApplication.class, args); + } + +} diff --git a/src/main/java/com/xgh/test/algorithms/array/ArrayList.java b/src/main/java/com/xgh/test/algorithms/array/ArrayList.java new file mode 100644 index 0000000..e43c2fb --- /dev/null +++ b/src/main/java/com/xgh/test/algorithms/array/ArrayList.java @@ -0,0 +1,65 @@ +package com.xgh.test.algorithms.array; + +import java.util.Arrays; + +public class ArrayList implements List { + + + /** + * 默认初始化空间 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 空元素 + */ + private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; + + /** + * 元素数组缓冲区 + */ + transient Object[] elementData; + + /** + * 元素个数 + */ + private int size; + + + @Override + public boolean add(E e) { + //确保内部容量 + int minCapacity = size + 1; + if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + //判断扩容 + if (minCapacity - elementData.length > 0) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if(newCapacity -minCapacity < 0){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData,newCapacity); + } + //添加原始 + elementData[size++] = e; + return true; + } + + @Override + public E remove(int index) { + E oldElement =(E) elementData[index]; + int numMoved = size - index - 1; + if(numMoved > 0){ + System.arraycopy(elementData,index+1,elementData,index,numMoved); + } + elementData[--size] = null; + return oldElement; + } + + @Override + public E get(int index) { + return (E) elementData[index]; + } +} diff --git a/src/main/java/com/xgh/test/algorithms/array/List.java b/src/main/java/com/xgh/test/algorithms/array/List.java new file mode 100644 index 0000000..98d4573 --- /dev/null +++ b/src/main/java/com/xgh/test/algorithms/array/List.java @@ -0,0 +1,10 @@ +package com.xgh.test.algorithms.array; + +public interface List{ + + boolean add(E e); + + E remove(int index); + + E get(int index); +} diff --git a/src/main/java/com/xgh/test/algorithms/link/LinkedList.java b/src/main/java/com/xgh/test/algorithms/link/LinkedList.java new file mode 100644 index 0000000..50b7e0f --- /dev/null +++ b/src/main/java/com/xgh/test/algorithms/link/LinkedList.java @@ -0,0 +1,164 @@ +package com.xgh.test.algorithms.link; + + +public class LinkedList implements List { + + transient int size = 0; + + transient Node first; + + transient Node last; + + public LinkedList() { + + + } + + void linkFirst(E e) { + final Node f = first; + final Node newNode = new Node<>(null, e, f); + first = newNode; + if (f == null) { + last = newNode; + } else { + f.prev = newNode; + } + size++; + } + + void linkLast(E e) { + final Node l = last; + final Node newNode = new Node<>(l, e, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + } + + @Override + public boolean add(E e) { + linkLast(e); + return true; + } + + @Override + public boolean addFirst(E e) { + linkFirst(e); + return true; + } + + @Override + public boolean addLast(E e) { + linkLast(e); + return true; + } + + @Override + public boolean remove(Object o) { + if (o == null) { + for (Node x = first; x != null; x = x.next) { + if (x.item == null) { + unLink(x); + return true; + } + } + } else { + for (Node x = first; x != null; x = x.next) { + if (o.equals(x.item)) { + unLink(x); + return true; + } + } + } + return false; + } + + @Override + public E get(int index) { + return node(index).item; + } + + + @Override + public void printLinkList() { + if (this.size == 0) { + System.out.println("链表为空"); + } else { + Node temp = first; + System.out.print("目前的列表,头节点:" + first.item + " 尾节点:" + last.item + " 整体:"); + while (temp != null) { + System.out.print(temp.item + ","); + temp = temp.next; + } + System.out.println(); + } + } + + E unLink(Node x) { + final E element = x.item; + final Node prev = x.prev; + final Node next = x.next; + if (prev == null) { + first = next; + } else { + prev.next = next; + x.prev = null; + } + if (next == null) { + last = prev; + } else { + next.prev = prev; + x.next = null; + } + x.item = null; + size--; + return element; + } + + private Node node(int index) { + Node x; + if (index < (size >> 1)) { + x = first; + for (int i = 0; i < index; i++) { + x = x.next; + } + } else { + x = last; + for (int i = size - 1; i > index; i--) { + x = x.prev; + } + } + return x; + } + + private static class Node { + E item; + Node next; + Node prev; + + public Node(Node prev, E item, Node next) { + this.item = item; + this.next = next; + this.prev = prev; + } + } + + public static void main(String[] args) { + List list = new LinkedList<>(); + // 添加元素 + list.add("a"); + list.addFirst("b"); + list.addLast("c"); + // 打印列表 + list.printLinkList(); + // 头插元素 + list.addFirst("d"); + // 删除元素 + list.remove("b"); + // 打印列表 + list.printLinkList(); + } +} diff --git a/src/main/java/com/xgh/test/algorithms/link/List.java b/src/main/java/com/xgh/test/algorithms/link/List.java new file mode 100644 index 0000000..2670fca --- /dev/null +++ b/src/main/java/com/xgh/test/algorithms/link/List.java @@ -0,0 +1,18 @@ +package com.xgh.test.algorithms.link; + +public interface List { + + boolean add(E e); + + boolean addFirst(E e); + + boolean addLast(E e); + + boolean remove(Object o); + + E get(int index); + + void printLinkList(); + + +} diff --git a/src/main/java/com/xgh/test/design/IdGenerator.java b/src/main/java/com/xgh/test/design/IdGenerator.java new file mode 100644 index 0000000..10ca5c5 --- /dev/null +++ b/src/main/java/com/xgh/test/design/IdGenerator.java @@ -0,0 +1,54 @@ +package com.xgh.test.design; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.SecureRandom; +import java.util.Random; + +/** + * com.xgh.test.design.IdGenerator + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ + +public class IdGenerator { + private static final Logger logger = LoggerFactory.getLogger(IdGenerator.class); + + public static String generate() { + String id = ""; + try { + String hostName = InetAddress.getLocalHost().getHostName(); + String[] tokens = hostName.split("\\."); + if (tokens.length > 0) { + hostName = tokens[tokens.length - 1]; + } + char[] randomChars = new char[8]; + int count = 0; + SecureRandom random = new SecureRandom(); + while (count < 8) { + int randomAscii = random.nextInt(122); + if (randomAscii >= 48 && randomAscii <= 57) { + randomChars[count] = (char)('0' + (randomAscii - 48)); + count++; + } else if (randomAscii >= 65 && randomAscii <= 90) { + randomChars[count] = (char)('A' + (randomAscii - 65)); + count++; + } else if (randomAscii >= 97 && randomAscii <= 122) { + randomChars[count] = (char)('a' + (randomAscii - 97)); + count++; + } + } + id = String.format("%s-%d-%s", hostName, + System.currentTimeMillis(), new String(randomChars)); + } catch (UnknownHostException e) { + logger.warn("Failed to get the host name.", e); + } + + return id; + } +} \ No newline at end of file diff --git a/src/main/java/com/xgh/test/design/STATUS.java b/src/main/java/com/xgh/test/design/STATUS.java new file mode 100644 index 0000000..001a54d --- /dev/null +++ b/src/main/java/com/xgh/test/design/STATUS.java @@ -0,0 +1,20 @@ +package com.xgh.test.design; + +import lombok.AllArgsConstructor; +import lombok.Data; + + +@AllArgsConstructor +public enum STATUS { + //排序字段 + TO_BE_EXECUTD(0," order_time desc"), + EXECUTED(1," write_off_time DESC"), + EXPIRED(2," update_time DESC"), + FAILED(2," update_time DESC"), + + ; + private int value; + private String name; + + +} diff --git a/src/main/java/com/xgh/test/design/Transaction.java b/src/main/java/com/xgh/test/design/Transaction.java new file mode 100644 index 0000000..e32aaee --- /dev/null +++ b/src/main/java/com/xgh/test/design/Transaction.java @@ -0,0 +1,118 @@ +package com.xgh.test.design; + + +import lombok.Data; +import org.springframework.util.IdGenerator; + +import javax.transaction.InvalidTransactionException; + +/** + * com.xgh.test.design.Transaction + * + * @author xgh
+ * @description + * @date 2021年07月07日 + */ +@Data +public class Transaction { + + private String id; + private Long buyerId; + private Long sellerId; + + private Long productId; + + private Long orderId; + + private Long createTimestamp; + + private Double amount=0.0; + + private STATUS status; + + private String walletTransactionId; + + private WalletRpcService walletRpcService; + + public void setWalletRpcService(WalletRpcService walletRpcService) { + this.walletRpcService = walletRpcService; + } + + public Transaction(String preAssignedId, Long buyerId, Long sellerId, Long productId, Long orderId) { + if (preAssignedId != null && !preAssignedId.isEmpty()) { + this.id = preAssignedId; + } else { + this.id = "12313131"; + } + if (!this.id.startsWith("t_")) { + this.id = "t_" + preAssignedId; + } + + this.buyerId = buyerId; + this.sellerId = sellerId; + this.productId = productId; + this.orderId = orderId; + this.status = STATUS.TO_BE_EXECUTD; + this.createTimestamp = System.currentTimeMillis(); + } + + public boolean execute() throws InvalidTransactionException { + if (buyerId == null || (sellerId == null || amount < 0.0)) { + throw new InvalidTransactionException("111"); + } + if (status == STATUS.EXECUTED) { + return true; + } + boolean isLocked = false; + try { + isLocked = getLock(id); + if (!isLocked) { + return false; + } + if (status == STATUS.EXECUTED) { + return true; + } + long executionInvokedTimestamp = System.currentTimeMillis(); + if (executionInvokedTimestamp - createTimestamp > 14) { + this.status = STATUS.EXPIRED; + return false; + } + String walletTransactionId = walletRpcService.moveMoney(id, buyerId, sellerId, amount); + if (walletTransactionId != null) { + this.status = STATUS.EXECUTED; + this.walletTransactionId = walletTransactionId; + return true; + } else { + this.status = STATUS.FAILED; + return false; + } + + } finally { + if (isLocked) { + unlock(id); + } + } + } + + private void unlock(String id) { + + + } + + private boolean getLock(String id) { + return true; + } + + public static void main(String[] args) { + Integer[] a = new Integer[]{1,2,3,4,5}; + + a[2] = 5; + for (Integer integer : a) { + System.out.println(integer); + } + + + } + + +} diff --git a/src/main/java/com/xgh/test/design/WalletRpcService.java b/src/main/java/com/xgh/test/design/WalletRpcService.java new file mode 100644 index 0000000..ec77e7c --- /dev/null +++ b/src/main/java/com/xgh/test/design/WalletRpcService.java @@ -0,0 +1,13 @@ +package com.xgh.test.design; + +/** + * com.xgh.test.design.WalletRpcService + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public interface WalletRpcService { + + String moveMoney(String id, Long fromUserId, Long toUserId, Double amount); +} diff --git a/src/main/java/com/xgh/test/design/WalletRpcServiceOne.java b/src/main/java/com/xgh/test/design/WalletRpcServiceOne.java new file mode 100644 index 0000000..cc9cd23 --- /dev/null +++ b/src/main/java/com/xgh/test/design/WalletRpcServiceOne.java @@ -0,0 +1,15 @@ +package com.xgh.test.design; + +/** + * com.xgh.test.design.WalletRpcServiceOne + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public class WalletRpcServiceOne implements WalletRpcService{ + @Override + public String moveMoney(String id, Long fromUserId, Long toUserId, Double amount) { + return "213112abc"; + } +} diff --git a/src/main/java/com/xgh/test/design/WalletRpcServiceTwo.java b/src/main/java/com/xgh/test/design/WalletRpcServiceTwo.java new file mode 100644 index 0000000..1fd9995 --- /dev/null +++ b/src/main/java/com/xgh/test/design/WalletRpcServiceTwo.java @@ -0,0 +1,15 @@ +package com.xgh.test.design; + +/** + * com.xgh.test.design.WalletRpcServiceTwo + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public class WalletRpcServiceTwo implements WalletRpcService{ + @Override + public String moveMoney(String id, Long fromUserId, Long toUserId, Double amount) { + return null; + } +} diff --git a/src/main/java/com/xgh/test/design/id/IdGenerator.java b/src/main/java/com/xgh/test/design/id/IdGenerator.java new file mode 100644 index 0000000..0497c88 --- /dev/null +++ b/src/main/java/com/xgh/test/design/id/IdGenerator.java @@ -0,0 +1,12 @@ +package com.xgh.test.design.id; + +/** + * com.xgh.test.design.id.IdGenerator + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public interface IdGenerator { + String generate(); +} diff --git a/src/main/java/com/xgh/test/design/id/LogTraceIdGenerator.java b/src/main/java/com/xgh/test/design/id/LogTraceIdGenerator.java new file mode 100644 index 0000000..5e78113 --- /dev/null +++ b/src/main/java/com/xgh/test/design/id/LogTraceIdGenerator.java @@ -0,0 +1,11 @@ +package com.xgh.test.design.id; + +/** + * com.xgh.test.design.id.LogTraceIdGenerator + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public interface LogTraceIdGenerator extends IdGenerator{ +} diff --git a/src/main/java/com/xgh/test/design/id/RandomIdGenerator.java b/src/main/java/com/xgh/test/design/id/RandomIdGenerator.java new file mode 100644 index 0000000..feb9b82 --- /dev/null +++ b/src/main/java/com/xgh/test/design/id/RandomIdGenerator.java @@ -0,0 +1,65 @@ +package com.xgh.test.design.id; + +import org.assertj.core.util.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.SecureRandom; +import java.util.Random; + +/** + * com.xgh.test.design.id.RandomIdGenerator + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ + +public class RandomIdGenerator implements LogTraceIdGenerator { + private static final Logger logger = LoggerFactory.getLogger(RandomIdGenerator.class); + + @Override + public String generate() { + String substrOfHostName = getLastfieldOfHostName(); + long currentTimeMillis = System.currentTimeMillis(); + String randomString = generateRandomAlphameric(8); + return String.format("%s-%d-%s", substrOfHostName, currentTimeMillis, randomString); + } + + private String getLastfieldOfHostName() { + String substrOfHostName = null; + try { + String hostName = InetAddress.getLocalHost().getHostName(); + substrOfHostName = getLastSubstrSplittedByDot(hostName); + } catch (UnknownHostException e) { + logger.warn("Failed to get the host name.", e); + } + return substrOfHostName; + } + + @VisibleForTesting + protected String getLastSubstrSplittedByDot(String hostName) { + String[] tokens = hostName.split("\\."); + return tokens[tokens.length - 1]; + } + + private String generateRandomAlphameric(int length) { + char[] randomChars = new char[length]; + int count = 0; + SecureRandom random = new SecureRandom(); + while (count < length) { + int maxAscii = 'z'; + int randomAscii = random.nextInt(maxAscii); + boolean isDigit = randomAscii >= '0' && randomAscii <= '9'; + boolean isUppercase = randomAscii >= 'A' && randomAscii <= 'Z'; + boolean isLowercase = randomAscii >= 'a' && randomAscii <= 'z'; + if (isDigit || isUppercase || isLowercase) { + randomChars[count] = (char) (randomAscii); + ++count; + } + } + return new String(randomChars); + } +} \ No newline at end of file diff --git a/src/main/java/com/xgh/test/design/id/RandomIdGeneratorTest.java b/src/main/java/com/xgh/test/design/id/RandomIdGeneratorTest.java new file mode 100644 index 0000000..af72ac7 --- /dev/null +++ b/src/main/java/com/xgh/test/design/id/RandomIdGeneratorTest.java @@ -0,0 +1,57 @@ +package com.xgh.test.design.id; + +import org.junit.Assert; +import org.junit.Test; + +/** + * com.xgh.test.design.id.RandomIdGeneratorTest + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public class RandomIdGeneratorTest { + @Test + public void testGetLastSubstrSplittedByDot() { + RandomIdGenerator idGenerator = new RandomIdGenerator(); + String actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1.field2.field3"); + Assert.assertEquals("field3", actualSubstr); + actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1"); + Assert.assertEquals("field1", actualSubstr); + actualSubstr = idGenerator.getLastSubstrSplittedByDot("field1#field2$field3"); + Assert.assertEquals("field1#field2#field3", actualSubstr); + } + + // 此单元测试会失败,因为我们在代码中没有处理hostName为null或空字符串的情况 + // 这部分优化留在第36、37节课中讲解 + @Test + public void testGetLastSubstrSplittedByDot_nullOrEmpty() { + RandomIdGenerator idGenerator = new RandomIdGenerator(); + String actualSubstr = idGenerator.getLastSubstrSplittedByDot(null); + Assert.assertNull(actualSubstr); + actualSubstr = idGenerator.getLastSubstrSplittedByDot(""); + Assert.assertEquals("", actualSubstr); + } + + @Test + public void testGenerateRandomAlphameric() { + RandomIdGenerator idGenerator = new RandomIdGenerator(); + //String actualRandomString = idGenerator.generateRandomAlphameric(6); + // Assert.assertNotNull(actualRandomString); + // Assert.assertEquals(6, actualRandomString.length()); + /* for (char c : actualRandomString.toCharArray()) { + Assert.assertTrue(('0' < c && c < '9') || ('a' < c && c < 'z') || ('A' < c && c < 'Z')); + }*/ + } + + // 此单元测试会失败,因为我们在代码中没有处理length<=0的情况 +// 这部分优化留在第36、37节课中讲解 + @Test + public void testGenerateRandomAlphameric_lengthEqualsOrLessThanZero() { + RandomIdGenerator idGenerator = new RandomIdGenerator(); + // String actualRandomString = idGenerator.generateRandomAlphameric(0); + // Assert.assertEquals("", actualRandomString); + // actualRandomString = idGenerator.generateRandomAlphameric(-1); + // Assert.assertNull(actualRandomString); + } +} diff --git a/src/main/java/com/xgh/test/redis/lock/RedisWithReentrantLock.java b/src/main/java/com/xgh/test/redis/lock/RedisWithReentrantLock.java new file mode 100644 index 0000000..765a50e --- /dev/null +++ b/src/main/java/com/xgh/test/redis/lock/RedisWithReentrantLock.java @@ -0,0 +1,83 @@ +package com.xgh.test.redis.lock; + +import redis.clients.jedis.*; + +import java.util.HashMap; +import java.util.Map; + +public class RedisWithReentrantLock { + + private ThreadLocal> lockers = new ThreadLocal<>(); + + private Jedis jedis; + + public RedisWithReentrantLock(Jedis jedis) { + this.jedis = jedis; + } + + private boolean _lock(String key) { + return jedis.set(key, "", "nx", "ex", 5L) != null; + } + + private void _unlock(String key) { + jedis.del(key); + } + + + private Map currentLockers() { + Map refs = lockers.get(); + if (refs != null) { + return refs; + } + lockers.set(new HashMap<>()); + return lockers.get(); + } + + public boolean lock(String key) { + Map refs = currentLockers(); + Integer refCnt = refs.get(key); + if (refCnt != null) { + refs.put(key, refCnt + 1); + return true; + } + boolean ok = this._lock(key); + if (!ok) { + return false; + } + refs.put(key, 1); + return true; + } + + + public boolean unlock(String key) { + Map refs = currentLockers(); + + Integer refCnt = refs.get(key); + if (refCnt == null) { + return false; + } + refCnt -= 1; + if (refCnt > 0) { + refs.put(key, refCnt); + } else { + refs.remove(key); + this._unlock(key); + } + return true; + } + + public static void main(String[] args) { + Jedis jedis = new Jedis("192.168.220.131", 6379); + jedis.auth("123456"); + RedisWithReentrantLock redisWithReentrantLock = new RedisWithReentrantLock(jedis); + + System.out.println(redisWithReentrantLock.lock("books1")); + System.out.println(redisWithReentrantLock.lock("books1")); + System.out.println(redisWithReentrantLock.unlock("books1")); + System.out.println(redisWithReentrantLock.unlock("books1")); + + + } + + +} diff --git a/src/main/java/com/xgh/test/spring/step02/BeanFactory.java b/src/main/java/com/xgh/test/spring/step02/BeanFactory.java new file mode 100644 index 0000000..4dca71d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/BeanFactory.java @@ -0,0 +1,17 @@ +package com.xgh.test.spring.step02; + + +/** + * com.xgh.test.spring.step02.BeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月12日 + */ +public interface BeanFactory { + + public Object getBean(String beanName) throws BeansException; + + Object getBean(String beanName,Object... args )throws BeansException; + +} diff --git a/src/main/java/com/xgh/test/spring/step02/BeansException.java b/src/main/java/com/xgh/test/spring/step02/BeansException.java new file mode 100644 index 0000000..437c2e9 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/BeansException.java @@ -0,0 +1,19 @@ +package com.xgh.test.spring.step02; + +/** + * com.xgh.test.spring.step02.BeansException + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeansException extends RuntimeException{ + + public BeansException(String message) { + super(message); + } + + public BeansException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/xgh/test/spring/step02/config/BeanDefinition.java b/src/main/java/com/xgh/test/spring/step02/config/BeanDefinition.java new file mode 100644 index 0000000..f43863d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/config/BeanDefinition.java @@ -0,0 +1,25 @@ +package com.xgh.test.spring.step02.config; + +/** + * com.xgh.test.spring.step02.config.BeanDefinition + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeanDefinition { + + private Class beanClass; + + public BeanDefinition(Class beanClass) { + this.beanClass = beanClass; + } + + public Class getBeanClass() { + return beanClass; + } + + public void setBeanClass(Class beanClass) { + this.beanClass = beanClass; + } +} diff --git a/src/main/java/com/xgh/test/spring/step02/config/SingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step02/config/SingletonBeanRegistry.java new file mode 100644 index 0000000..983259e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/config/SingletonBeanRegistry.java @@ -0,0 +1,13 @@ +package com.xgh.test.spring.step02.config; + +/** + * com.xgh.test.spring.step02.config.SingletonBeanRegistry + * + * @author xgh
+ * @description 单例bean注册 + * @date 2021年08月17日 + */ +public interface SingletonBeanRegistry { + + Object getSingleton(String beanName); +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/AbstractAutowireCapableBeanFactory.java b/src/main/java/com/xgh/test/spring/step02/support/AbstractAutowireCapableBeanFactory.java new file mode 100644 index 0000000..b854250 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/AbstractAutowireCapableBeanFactory.java @@ -0,0 +1,54 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.BeansException; +import com.xgh.test.spring.step02.config.BeanDefinition; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.AbstractAutowireCapableBeanFactory + * + * @author xgh
+ * @description 实例化bean类 + * @date 2021年08月17日 + */ +public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{ + + private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); + + @Override + protected Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException { + Object bean = null; + try { + //todo 有参实现 + // bean = beanDefinition.getBeanClass().newInstance(); + bean = creatBeanInstance(beanDefinition,beanName,args); + + } catch (Exception e) { + throw new BeansException("Instantiation of bean fail",e); + } + addSingleton(beanName,bean); + return bean; + } + + protected Object creatBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args){ + Constructor constructorToUser = null; + Class beanClass = beanDefinition.getBeanClass(); + Constructor[] declaredConstructors = beanClass.getDeclaredConstructors(); + for (Constructor ctor : declaredConstructors) { + if(null != args && ctor.getParameterTypes().length == args.length){ + constructorToUser = ctor; + break; + } + } + return getInstantiationStrategy().instantiate(beanDefinition,beanName,constructorToUser,args); + }; + + public InstantiationStrategy getInstantiationStrategy() { + return instantiationStrategy; + } + + public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) { + this.instantiationStrategy = instantiationStrategy; + } +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/AbstractBeanFactory.java b/src/main/java/com/xgh/test/spring/step02/support/AbstractBeanFactory.java new file mode 100644 index 0000000..1a2a7b7 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/AbstractBeanFactory.java @@ -0,0 +1,40 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.BeanFactory; +import com.xgh.test.spring.step02.BeansException; +import com.xgh.test.spring.step02.config.BeanDefinition; + +/** + * com.xgh.test.spring.step02.support.AbstractBeanFactory + * + * @author xgh
+ * @description 抽象类定义模板方法 + * @date 2021年08月17日 + */ +public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory { + + @Override + public Object getBean(String beanName) throws BeansException { + return doGetBean(beanName,null); + } + + @Override + public Object getBean(String beanName, Object... args) throws BeansException { + return doGetBean(beanName,args); + } + + protected T doGetBean(String beanName, Object[] args){ + Object bean = getSingleton(beanName); + if(bean != null){ + return (T) bean; + } + BeanDefinition beanDefinition = getBeanDefinition(beanName); + return (T) creatBean(beanName,beanDefinition,args); + + } + + protected abstract Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException; + + protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException ; + +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/BeanDefinitionRegistry.java b/src/main/java/com/xgh/test/spring/step02/support/BeanDefinitionRegistry.java new file mode 100644 index 0000000..15bdb2d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/BeanDefinitionRegistry.java @@ -0,0 +1,16 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.config.BeanDefinition; + +/** + * com.xgh.test.spring.step02.support.BeanDefinitionRegistry + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public interface BeanDefinitionRegistry { + + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition); + +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/CglibSubclassingInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step02/support/CglibSubclassingInstantiationStrategy.java new file mode 100644 index 0000000..2dce42e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/CglibSubclassingInstantiationStrategy.java @@ -0,0 +1,35 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.config.BeanDefinition; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.NoOp; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.CglibSubclassingInstantiationStrategy + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy{ + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(beanDefinition.getBeanClass()); + enhancer.setCallback(new NoOp() { + @Override + public int hashCode() { + return super.hashCode(); + } + }); + if(null == ctor){ + return enhancer.create(); + } + return enhancer.create(ctor.getParameterTypes(),args); + } + +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/DefaultListableBeanFactory.java b/src/main/java/com/xgh/test/spring/step02/support/DefaultListableBeanFactory.java new file mode 100644 index 0000000..4addf89 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/DefaultListableBeanFactory.java @@ -0,0 +1,33 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.BeansException; +import com.xgh.test.spring.step02.config.BeanDefinition; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.step02.support.DefaultListableBeanFactory + * + * @author xgh
+ * @description 核心实现 + * @date 2021年08月17日 + */ +public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{ + + private Map beanDefinitionMap = new HashMap<>(); + + @Override + protected BeanDefinition getBeanDefinition(String beanName) throws BeansException { + BeanDefinition beanDefinition = beanDefinitionMap.get(beanName); + if(null == beanDefinition){ + throw new BeansException("No bean named '"+ beanName+"' is definde"); + } + return beanDefinition; + } + + @Override + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) { + beanDefinitionMap.put(beanName,beanDefinition); + } +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/DefaultSingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step02/support/DefaultSingletonBeanRegistry.java new file mode 100644 index 0000000..e6ab2c6 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/DefaultSingletonBeanRegistry.java @@ -0,0 +1,27 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.config.SingletonBeanRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.step02.support.DefaultSingletinBeanRegistry + * + * @author xgh
+ * @description 单例实例的获取接口 + * @date 2021年08月17日 + */ +public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry { + + private Map singletonObjects = new HashMap<>(); + + @Override + public Object getSingleton(String beanName) { + return singletonObjects.get(beanName); + } + + protected void addSingleton(String beanName,Object singletonObject){ + singletonObjects.put(beanName,singletonObject); + } +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/InstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step02/support/InstantiationStrategy.java new file mode 100644 index 0000000..e3c32ff --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/InstantiationStrategy.java @@ -0,0 +1,17 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.config.BeanDefinition; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.InstantionStrategy + * + * @author xgh
+ * @description 实例化策略 + * @date 2021年08月17日 + */ +public interface InstantiationStrategy { + + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor,Object[] args); +} diff --git a/src/main/java/com/xgh/test/spring/step02/support/SimpleInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step02/support/SimpleInstantiationStrategy.java new file mode 100644 index 0000000..c926502 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step02/support/SimpleInstantiationStrategy.java @@ -0,0 +1,34 @@ +package com.xgh.test.spring.step02.support; + +import com.xgh.test.spring.step02.BeansException; +import com.xgh.test.spring.step02.config.BeanDefinition; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +/** + * com.xgh.test.spring.step02.support.SimpleInstantiationStrategy + * + * @author xgh
+ * @description jdk + * @date 2021年08月17日 + */ +public class SimpleInstantiationStrategy implements InstantiationStrategy { + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Class clazz = beanDefinition.getBeanClass(); + try { + if (null != ctor) { + return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args); + }else { + return clazz.getDeclaredConstructor().newInstance(); + } + }catch(InstantiationException |IllegalAccessException|InvocationTargetException | NoSuchMethodException e) { + throw new BeansException("Failed to instantiate["+clazz.getName()+"]",e); + } + } + + +} diff --git a/src/main/java/com/xgh/test/spring/step03/BeanFactory.java b/src/main/java/com/xgh/test/spring/step03/BeanFactory.java new file mode 100644 index 0000000..29493dc --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/BeanFactory.java @@ -0,0 +1,17 @@ +package com.xgh.test.spring.step03; + + +/** + * com.xgh.test.spring.step02.BeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月12日 + */ +public interface BeanFactory { + + public Object getBean(String beanName) throws BeansException; + + Object getBean(String beanName,Object... args )throws BeansException; + +} diff --git a/src/main/java/com/xgh/test/spring/step03/BeansException.java b/src/main/java/com/xgh/test/spring/step03/BeansException.java new file mode 100644 index 0000000..328bb9b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/BeansException.java @@ -0,0 +1,19 @@ +package com.xgh.test.spring.step03; + +/** + * com.xgh.test.spring.step02.BeansException + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeansException extends RuntimeException{ + + public BeansException(String message) { + super(message); + } + + public BeansException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/PropertyValue.java b/src/main/java/com/xgh/test/spring/step03/PropertyValue.java new file mode 100644 index 0000000..c23c05b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/PropertyValue.java @@ -0,0 +1,36 @@ +package com.xgh.test.spring.step03; + +/** + * com.xgh.test.spring.step03.PropertyValue + * + * @author xgh
+ * @description 属性 + * @date 2021年08月23日 + */ +public class PropertyValue { + + private String name; + + private Object value; + + public PropertyValue(String name, Object value) { + this.name = name; + this.value = value; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/PropertyValues.java b/src/main/java/com/xgh/test/spring/step03/PropertyValues.java new file mode 100644 index 0000000..8b1f353 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/PropertyValues.java @@ -0,0 +1,36 @@ +package com.xgh.test.spring.step03; + +import java.util.ArrayList; +import java.util.List; + +/** + * com.xgh.test.spring.step03.PropertyValues + * + * @author xgh
+ * @description + * @date 2021年08月23日 + */ +public class PropertyValues { + + private final List propertyValues = new ArrayList<>(); + + + public void addPropertyValue(PropertyValue propertyValue){ + this.propertyValues.add(propertyValue); + } + + + public PropertyValue[] getPropertyValues(){ + return this.propertyValues.toArray(new PropertyValue[0]); + } + + + public PropertyValue getPropertyValue(String name){ + for (PropertyValue propertyValue : propertyValues) { + if(propertyValue.getName().equals(name)){ + return propertyValue; + } + } + return null; + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/config/BeanDefinition.java b/src/main/java/com/xgh/test/spring/step03/config/BeanDefinition.java new file mode 100644 index 0000000..12d724d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/config/BeanDefinition.java @@ -0,0 +1,44 @@ +package com.xgh.test.spring.step03.config; + +import com.xgh.test.spring.step03.PropertyValues; + +/** + * com.xgh.test.spring.step02.config.BeanDefinition + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeanDefinition { + + private Class beanClass; + + private PropertyValues propertyValues; + + + public BeanDefinition(Class beanClass) { + this.beanClass = beanClass; + this.propertyValues = new PropertyValues(); + } + + public BeanDefinition(Class beanClass, PropertyValues propertyValues) { + this.beanClass = beanClass; + this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues(); + } + + public Class getBeanClass() { + return beanClass; + } + + public void setBeanClass(Class beanClass) { + this.beanClass = beanClass; + } + + public PropertyValues getPropertyValues() { + return propertyValues; + } + + public void setPropertyValues(PropertyValues propertyValues) { + this.propertyValues = propertyValues; + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/config/BeanReference.java b/src/main/java/com/xgh/test/spring/step03/config/BeanReference.java new file mode 100644 index 0000000..0b12e8f --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/config/BeanReference.java @@ -0,0 +1,25 @@ +package com.xgh.test.spring.step03.config; + +/** + * com.xgh.test.spring.step03.config.BeanReference + * + * @author xgh
+ * @description + * @date 2021年08月23日 + */ +public class BeanReference { + + private String beanName; + + public BeanReference(String beanName) { + this.beanName = beanName; + } + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/config/SingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step03/config/SingletonBeanRegistry.java new file mode 100644 index 0000000..c53f58f --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/config/SingletonBeanRegistry.java @@ -0,0 +1,13 @@ +package com.xgh.test.spring.step03.config; + +/** + * com.xgh.test.spring.step02.config.SingletonBeanRegistry + * + * @author xgh
+ * @description 单例bean注册 + * @date 2021年08月17日 + */ +public interface SingletonBeanRegistry { + + Object getSingleton(String beanName); +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/AbstractAutowireCapableBeanFactory.java b/src/main/java/com/xgh/test/spring/step03/support/AbstractAutowireCapableBeanFactory.java new file mode 100644 index 0000000..18135c4 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/AbstractAutowireCapableBeanFactory.java @@ -0,0 +1,78 @@ +package com.xgh.test.spring.step03.support; + +import cn.hutool.core.bean.BeanUtil; +import com.xgh.test.spring.step03.BeansException; +import com.xgh.test.spring.step03.PropertyValue; +import com.xgh.test.spring.step03.PropertyValues; +import com.xgh.test.spring.step03.config.BeanDefinition; +import com.xgh.test.spring.step03.config.BeanReference; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.AbstractAutowireCapableBeanFactory + * + * @author xgh
+ * @description 实例化bean类 + * @date 2021年08月17日 + */ +public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory { + + private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); + + @Override + protected Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException { + Object bean = null; + try { + + bean = creatBeanInstance(beanDefinition,beanName,args); + //设置参数 + applyPropertyValues(beanName,bean,beanDefinition); + } catch (Exception e) { + throw new BeansException("Instantiation of bean fail",e); + } + addSingleton(beanName,bean); + return bean; + } + + + protected Object creatBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args){ + Constructor constructorToUser = null; + Class beanClass = beanDefinition.getBeanClass(); + Constructor[] declaredConstructors = beanClass.getDeclaredConstructors(); + for (Constructor ctor : declaredConstructors) { + if(null != args && ctor.getParameterTypes().length == args.length){ + constructorToUser = ctor; + break; + } + } + return getInstantiationStrategy().instantiate(beanDefinition,beanName,constructorToUser,args); + }; + + protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition){ + try { + PropertyValues propertyValues = beanDefinition.getPropertyValues(); + for (PropertyValue propertyValue : propertyValues.getPropertyValues()) { + String name = propertyValue.getName(); + Object value = propertyValue.getValue(); + if(value instanceof BeanReference){ + //a依赖b + BeanReference beanReference = (BeanReference)value; + value = getBean(beanReference.getBeanName()); + } + BeanUtil.setFieldValue(bean,name,value); + } + }catch (Exception e){ + throw new BeansException("Error setting property values" + beanName); + } + + + }; + public InstantiationStrategy getInstantiationStrategy() { + return instantiationStrategy; + } + + public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) { + this.instantiationStrategy = instantiationStrategy; + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/AbstractBeanFactory.java b/src/main/java/com/xgh/test/spring/step03/support/AbstractBeanFactory.java new file mode 100644 index 0000000..d9a124b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/AbstractBeanFactory.java @@ -0,0 +1,40 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.BeanFactory; +import com.xgh.test.spring.step03.BeansException; +import com.xgh.test.spring.step03.config.BeanDefinition; + +/** + * com.xgh.test.spring.step02.support.AbstractBeanFactory + * + * @author xgh
+ * @description 抽象类定义模板方法 + * @date 2021年08月17日 + */ +public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory { + + @Override + public Object getBean(String beanName) throws BeansException { + return doGetBean(beanName,null); + } + + @Override + public Object getBean(String beanName, Object... args) throws BeansException { + return doGetBean(beanName,args); + } + + protected T doGetBean(String beanName, Object[] args){ + Object bean = getSingleton(beanName); + if(bean != null){ + return (T) bean; + } + BeanDefinition beanDefinition = getBeanDefinition(beanName); + return (T) creatBean(beanName,beanDefinition,args); + + } + + protected abstract Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException; + + protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException; + +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/BeanDefinitionRegistry.java b/src/main/java/com/xgh/test/spring/step03/support/BeanDefinitionRegistry.java new file mode 100644 index 0000000..df32d54 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/BeanDefinitionRegistry.java @@ -0,0 +1,16 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.config.BeanDefinition; + +/** + * com.xgh.test.spring.step02.support.BeanDefinitionRegistry + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public interface BeanDefinitionRegistry { + + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition); + +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/CglibSubclassingInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step03/support/CglibSubclassingInstantiationStrategy.java new file mode 100644 index 0000000..9539c0e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/CglibSubclassingInstantiationStrategy.java @@ -0,0 +1,35 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.config.BeanDefinition; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.NoOp; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.CglibSubclassingInstantiationStrategy + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy { + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(beanDefinition.getBeanClass()); + enhancer.setCallback(new NoOp() { + @Override + public int hashCode() { + return super.hashCode(); + } + }); + if(null == ctor){ + return enhancer.create(); + } + return enhancer.create(ctor.getParameterTypes(),args); + } + +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/DefaultListableBeanFactory.java b/src/main/java/com/xgh/test/spring/step03/support/DefaultListableBeanFactory.java new file mode 100644 index 0000000..ec92193 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/DefaultListableBeanFactory.java @@ -0,0 +1,33 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.BeansException; +import com.xgh.test.spring.step03.config.BeanDefinition; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.step02.support.DefaultListableBeanFactory + * + * @author xgh
+ * @description 核心实现 + * @date 2021年08月17日 + */ +public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry { + + private Map beanDefinitionMap = new HashMap<>(); + + @Override + protected BeanDefinition getBeanDefinition(String beanName) throws BeansException { + BeanDefinition beanDefinition = beanDefinitionMap.get(beanName); + if(null == beanDefinition){ + throw new BeansException("No bean named '"+ beanName+"' is definde"); + } + return beanDefinition; + } + + @Override + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) { + beanDefinitionMap.put(beanName,beanDefinition); + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/DefaultSingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step03/support/DefaultSingletonBeanRegistry.java new file mode 100644 index 0000000..8093308 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/DefaultSingletonBeanRegistry.java @@ -0,0 +1,27 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.config.SingletonBeanRegistry; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.step02.support.DefaultSingletinBeanRegistry + * + * @author xgh
+ * @description 单例实例的获取接口 + * @date 2021年08月17日 + */ +public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry { + + private Map singletonObjects = new HashMap<>(); + + @Override + public Object getSingleton(String beanName) { + return singletonObjects.get(beanName); + } + + protected void addSingleton(String beanName,Object singletonObject){ + singletonObjects.put(beanName,singletonObject); + } +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/InstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step03/support/InstantiationStrategy.java new file mode 100644 index 0000000..238508a --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/InstantiationStrategy.java @@ -0,0 +1,17 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.config.BeanDefinition; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.InstantionStrategy + * + * @author xgh
+ * @description 实例化策略 + * @date 2021年08月17日 + */ +public interface InstantiationStrategy { + + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args); +} diff --git a/src/main/java/com/xgh/test/spring/step03/support/SimpleInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step03/support/SimpleInstantiationStrategy.java new file mode 100644 index 0000000..bf664f0 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step03/support/SimpleInstantiationStrategy.java @@ -0,0 +1,34 @@ +package com.xgh.test.spring.step03.support; + +import com.xgh.test.spring.step03.BeansException; +import com.xgh.test.spring.step03.config.BeanDefinition; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +/** + * com.xgh.test.spring.step02.support.SimpleInstantiationStrategy + * + * @author xgh
+ * @description jdk + * @date 2021年08月17日 + */ +public class SimpleInstantiationStrategy implements InstantiationStrategy { + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Class clazz = beanDefinition.getBeanClass(); + try { + if (null != ctor) { + return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args); + }else { + return clazz.getDeclaredConstructor().newInstance(); + } + }catch(InstantiationException |IllegalAccessException|InvocationTargetException | NoSuchMethodException e) { + throw new BeansException("Failed to instantiate["+clazz.getName()+"]",e); + } + } + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/BeansException.java b/src/main/java/com/xgh/test/spring/step04/beans/BeansException.java new file mode 100644 index 0000000..f8f3c30 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/BeansException.java @@ -0,0 +1,19 @@ +package com.xgh.test.spring.step04.beans; + +/** + * com.xgh.test.spring.step02.BeansException + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeansException extends RuntimeException{ + + public BeansException(String message) { + super(message); + } + + public BeansException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/PropertyValue.java b/src/main/java/com/xgh/test/spring/step04/beans/PropertyValue.java new file mode 100644 index 0000000..3b0f144 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/PropertyValue.java @@ -0,0 +1,36 @@ +package com.xgh.test.spring.step04.beans; + +/** + * com.xgh.test.spring.step03.PropertyValue + * + * @author xgh
+ * @description 属性 + * @date 2021年08月23日 + */ +public class PropertyValue { + + private String name; + + private Object value; + + public PropertyValue(String name, Object value) { + this.name = name; + this.value = value; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/PropertyValues.java b/src/main/java/com/xgh/test/spring/step04/beans/PropertyValues.java new file mode 100644 index 0000000..0f64508 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/PropertyValues.java @@ -0,0 +1,36 @@ +package com.xgh.test.spring.step04.beans; + +import java.util.ArrayList; +import java.util.List; + +/** + * com.xgh.test.spring.step03.PropertyValues + * + * @author xgh
+ * @description + * @date 2021年08月23日 + */ +public class PropertyValues { + + private final List propertyValues = new ArrayList<>(); + + + public void addPropertyValue(PropertyValue propertyValue){ + this.propertyValues.add(propertyValue); + } + + + public PropertyValue[] getPropertyValues(){ + return this.propertyValues.toArray(new PropertyValue[0]); + } + + + public PropertyValue getPropertyValue(String name){ + for (PropertyValue propertyValue : propertyValues) { + if(propertyValue.getName().equals(name)){ + return propertyValue; + } + } + return null; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/BeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/BeanFactory.java new file mode 100644 index 0000000..56f4e4a --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/BeanFactory.java @@ -0,0 +1,23 @@ +package com.xgh.test.spring.step04.beans.factory; + +import com.xgh.test.spring.step04.beans.BeansException; + +/** + * com.xgh.test.spring.step04.beans.factory.BeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface BeanFactory { + + + public Object getBean(String beanName) throws BeansException; + + T getBean(String beanName,Class requiredType); + + Object getBean(String beanName,Object... args )throws BeansException; + + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/ConfigurableListableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/ConfigurableListableBeanFactory.java new file mode 100644 index 0000000..c42875d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/ConfigurableListableBeanFactory.java @@ -0,0 +1,24 @@ +package com.xgh.test.spring.step04.beans.factory; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.config.AutowireCapableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.BeanPostProcessor; +import com.xgh.test.spring.step04.beans.factory.config.ConfigurableBeanFactory; + +/** + * com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory { + + BeanDefinition getBeanDefinition(String beanName) throws BeansException; + + void preInstantiateSingletons() throws BeansException; + + + void addBeanPostProcessor(BeanPostProcessor beanPostProcessor); +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/DisposableBean.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/DisposableBean.java new file mode 100644 index 0000000..65a5047 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/DisposableBean.java @@ -0,0 +1,8 @@ +package com.xgh.test.spring.step04.beans.factory; + +public interface DisposableBean { + + void destroy()throws Exception; + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/HierarchicalBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/HierarchicalBeanFactory.java new file mode 100644 index 0000000..e11f4da --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/HierarchicalBeanFactory.java @@ -0,0 +1,11 @@ +package com.xgh.test.spring.step04.beans.factory; + +/** + * com.xgh.test.spring.step04.beans.factory.HierarchicalBeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface HierarchicalBeanFactory extends BeanFactory{ +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/InitializingBean.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/InitializingBean.java new file mode 100644 index 0000000..b59ab20 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/InitializingBean.java @@ -0,0 +1,14 @@ +package com.xgh.test.spring.step04.beans.factory; + + +/** + * 实现此接口的bean对象,会在beanFactory设置属性后做出相应的处理,儒执行自定义初始化,或者仅仅校验是否设置了所有强制属性 + */ +public interface InitializingBean { + + /** + * 处理了属性填充后调用 + * @throws Exception + */ + void afterPropertiesSet() throws Exception; +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/ListableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/ListableBeanFactory.java new file mode 100644 index 0000000..9907fdd --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/ListableBeanFactory.java @@ -0,0 +1,34 @@ +package com.xgh.test.spring.step04.beans.factory; + +import com.xgh.test.spring.step04.beans.BeansException; + +import java.util.Map; + +/** + * com.xgh.test.spring.step04.beans.factory.ListableBeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface ListableBeanFactory extends BeanFactory { + + + + /* + * @description 按类型返回实例 + * @date 2021/8/27 0027 + * @param type + * @return java.util.Map + */ + Map getBeansOfType(Class type) throws BeansException; + + + /* + * @description 返回所以bean的name + * @date 2021/8/27 0027 + * @return java.lang.String[] + */ + String[] getBeanDefinitionNames(); + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/AutowireCapableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/AutowireCapableBeanFactory.java new file mode 100644 index 0000000..05d8c4e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/AutowireCapableBeanFactory.java @@ -0,0 +1,15 @@ +package com.xgh.test.spring.step04.beans.factory.config; + + +import com.xgh.test.spring.step04.beans.factory.BeanFactory; + +/** + * com.xgh.test.spring.step04.beans.factory.config.AutowireCapableBeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface AutowireCapableBeanFactory extends BeanFactory { + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanDefinition.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanDefinition.java new file mode 100644 index 0000000..bb67517 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanDefinition.java @@ -0,0 +1,63 @@ +package com.xgh.test.spring.step04.beans.factory.config; + +import com.xgh.test.spring.step04.beans.PropertyValues; + +/** + * com.xgh.test.spring.step02.config.BeanDefinition + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class BeanDefinition { + + private Class beanClass; + + private PropertyValues propertyValues; + + private String initMethodName; + + private String destroyMethodName; + + public BeanDefinition(Class beanClass) { + this.beanClass = beanClass; + this.propertyValues = new PropertyValues(); + } + + public BeanDefinition(Class beanClass, PropertyValues propertyValues) { + this.beanClass = beanClass; + this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues(); + } + + public Class getBeanClass() { + return beanClass; + } + + public void setBeanClass(Class beanClass) { + this.beanClass = beanClass; + } + + public PropertyValues getPropertyValues() { + return propertyValues; + } + + public void setPropertyValues(PropertyValues propertyValues) { + this.propertyValues = propertyValues; + } + + public String getInitMethodName() { + return initMethodName; + } + + public void setInitMethodName(String initMethodName) { + this.initMethodName = initMethodName; + } + + public String getDestroyMethodName() { + return destroyMethodName; + } + + public void setDestroyMethodName(String destroyMethodName) { + this.destroyMethodName = destroyMethodName; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanFactoryPostProcessor.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanFactoryPostProcessor.java new file mode 100644 index 0000000..55172a4 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanFactoryPostProcessor.java @@ -0,0 +1,14 @@ +package com.xgh.test.spring.step04.beans.factory.config; + + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory; + +public interface BeanFactoryPostProcessor { + + + /** + * 在所有的beanDefinition加载完成后,实例化bean对象之前,提供修改BeanDefinition属性的机制 + */ + void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanPostProcessor.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanPostProcessor.java new file mode 100644 index 0000000..e1e1763 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanPostProcessor.java @@ -0,0 +1,25 @@ +package com.xgh.test.spring.step04.beans.factory.config; + +import com.xgh.test.spring.step04.beans.BeansException; + +public interface BeanPostProcessor { + + /** + * 在bean对象执行初始化方法之前,执行此方法 + * @param bean + * @param beanName + * @return + * @throws BeansException + */ + Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; + + + /** + * 在bean对象执行初始化完成之后执行此方法 + * @param bean + * @param beanName + * @return + * @throws BeansException + */ + Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException; +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanReference.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanReference.java new file mode 100644 index 0000000..39acb41 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/BeanReference.java @@ -0,0 +1,25 @@ +package com.xgh.test.spring.step04.beans.factory.config; + +/** + * com.xgh.test.spring.step03.config.BeanReference + * + * @author xgh
+ * @description + * @date 2021年08月23日 + */ +public class BeanReference { + + private String beanName; + + public BeanReference(String beanName) { + this.beanName = beanName; + } + + public String getBeanName() { + return beanName; + } + + public void setBeanName(String beanName) { + this.beanName = beanName; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/ConfigurableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/ConfigurableBeanFactory.java new file mode 100644 index 0000000..cf9b26b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/ConfigurableBeanFactory.java @@ -0,0 +1,24 @@ +package com.xgh.test.spring.step04.beans.factory.config; + +import com.xgh.test.spring.step04.beans.factory.HierarchicalBeanFactory; + +/** + * com.xgh.test.spring.step04.beans.factory.config.ConfigurableBeanFactory + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public interface ConfigurableBeanFactory extends HierarchicalBeanFactory,SingletonBeanRegistry { + + String SCOPE_SINGLETON = "singleton"; + + String SCOPE_PROTOTYPE= "prototype"; + + void addBeanPostProcessor(BeanPostProcessor beanPostProcessor); + + /** + * 销毁单例bean + */ + void destroySingletons(); +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/config/SingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/SingletonBeanRegistry.java new file mode 100644 index 0000000..3a5e07d --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/config/SingletonBeanRegistry.java @@ -0,0 +1,13 @@ +package com.xgh.test.spring.step04.beans.factory.config; + +/** + * com.xgh.test.spring.step02.config.SingletonBeanRegistry + * + * @author xgh
+ * @description 单例bean注册 + * @date 2021年08月17日 + */ +public interface SingletonBeanRegistry { + + Object getSingleton(String beanName); +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractAutowireCapableBeanFactory.java new file mode 100644 index 0000000..e1738c4 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -0,0 +1,151 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.StrUtil; +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.PropertyValue; +import com.xgh.test.spring.step04.beans.PropertyValues; +import com.xgh.test.spring.step04.beans.factory.DisposableBean; +import com.xgh.test.spring.step04.beans.factory.InitializingBean; +import com.xgh.test.spring.step04.beans.factory.config.AutowireCapableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.BeanPostProcessor; +import com.xgh.test.spring.step04.beans.factory.config.BeanReference; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/** + * com.xgh.test.spring.step02.support.AbstractAutowireCapableBeanFactory + * + * @author xgh
+ * @description 实例化bean类 + * @date 2021年08月17日 + */ +public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory implements AutowireCapableBeanFactory { + + private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); + + @Override + protected Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException { + Object bean = null; + try { + bean = creatBeanInstance(beanDefinition,beanName,args); + //给bean填充属性 + applyPropertyValues(beanName,bean,beanDefinition); + + //执行bean的初始化方法和beanPostProcessor的前置后置方法 + bean = initializeBean(beanName,bean,beanDefinition); + + }catch (Exception e){ + throw new BeansException("instantiation of bean failed",e); + } + //注册实现了disposableBean接口的bean + registerDisposableBeanIfNecessary(beanName,bean,beanDefinition); + addSingleton(beanName,bean); + return bean; + } + + protected void registerDisposableBeanIfNecessary(String beanName, Object bean, BeanDefinition beanDefinition){ + if(bean instanceof DisposableBean || StrUtil.isNotEmpty(beanDefinition.getDestroyMethodName())){ + registerDisposableBean(beanName,new DisposableBeanAdapter(bean,beanName,beanDefinition)); + } + } + + + protected Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition){ + //执行beanPostProcessor before处理 + Object wrappedBean = applyBeanPostProcessorsBeforeInitialization(bean,beanName); + try { + //待完成内容invokeInitMethods + invokeInitMethods(beanName,wrappedBean,beanDefinition); + }catch (Exception e){ + throw new BeansException("Invocation of init method of bean[" + beanName + "] failed", e); + } + + //执行beanPostProcessor after + wrappedBean = applyBeanPostProcessorAfterInitialization(bean,beanName); + return wrappedBean; + } + + private Object applyBeanPostProcessorAfterInitialization(Object existingBean, String beanName) { + Object result =existingBean; + for (BeanPostProcessor processor : getBeanPostProcessors()) { + Object current = processor.postProcessAfterInitialization(result,beanName); + if(null == current){ + return result; + } + result = current; + } + return result; + } + + private void invokeInitMethods(String beanName, Object bean, BeanDefinition beanDefinition) throws Exception { + //1实现接口initializingBean + if(bean instanceof InitializingBean){ + ((InitializingBean) bean).afterPropertiesSet(); + } + //注解配置init-method(判断是否为了避免二次执行) + String initMethodName = beanDefinition.getInitMethodName(); + if(StrUtil.isNotEmpty(initMethodName)){ + Method initMethod = beanDefinition.getBeanClass().getMethod(initMethodName); + if(null == initMethod){ + throw new BeansException("Could not find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'"); + } + initMethod.invoke(bean); + } + + } + + public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName){ + Object result =existingBean; + for (BeanPostProcessor processor : getBeanPostProcessors()) { + Object current = processor.postProcessBeforeInitialization(result,beanName); + if(null == current){ + return result; + } + result = current; + } + return result; + }; + + protected Object creatBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args){ + Constructor constructorToUser = null; + Class beanClass = beanDefinition.getBeanClass(); + Constructor[] declaredConstructors = beanClass.getDeclaredConstructors(); + for (Constructor ctor : declaredConstructors) { + if(null != args && ctor.getParameterTypes().length == args.length){ + constructorToUser = ctor; + break; + } + } + return getInstantiationStrategy().instantiate(beanDefinition,beanName,constructorToUser,args); + }; + + protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition){ + try { + PropertyValues propertyValues = beanDefinition.getPropertyValues(); + for (PropertyValue propertyValue : propertyValues.getPropertyValues()) { + String name = propertyValue.getName(); + Object value = propertyValue.getValue(); + if(value instanceof BeanReference){ + //a依赖b + BeanReference beanReference = (BeanReference)value; + value = getBean(beanReference.getBeanName()); + } + BeanUtil.setFieldValue(bean,name,value); + } + }catch (Exception e){ + throw new BeansException("Error setting property values" + beanName); + } + + + }; + public InstantiationStrategy getInstantiationStrategy() { + return instantiationStrategy; + } + + public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) { + this.instantiationStrategy = instantiationStrategy; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanDefinitionReader.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanDefinitionReader.java new file mode 100644 index 0000000..64c1c0f --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanDefinitionReader.java @@ -0,0 +1,37 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.core.io.DefaultResourceLoader; +import com.xgh.test.spring.step04.core.io.ResourceLoader; + +/** + * com.xgh.test.spring.step04.beans.factory.support.AbstractBeanDefinitionReader + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader{ + + private final BeanDefinitionRegistry registry; + + private ResourceLoader resourceLoader; + + public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) { + this(registry,new DefaultResourceLoader()); + } + + public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) { + this.registry = registry; + this.resourceLoader = resourceLoader; + } + + @Override + public BeanDefinitionRegistry getRegistry() { + return registry; + } + + @Override + public ResourceLoader getResourceLoader() { + return resourceLoader; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanFactory.java new file mode 100644 index 0000000..5f96fec --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/AbstractBeanFactory.java @@ -0,0 +1,60 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.BeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.BeanPostProcessor; +import com.xgh.test.spring.step04.beans.factory.config.ConfigurableBeanFactory; + +import java.util.ArrayList; +import java.util.List; + +/** + * com.xgh.test.spring.step02.support.AbstractBeanFactory + * + * @author xgh
+ * @description 抽象类定义模板方法 + * @date 2021年08月17日 + */ +public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements ConfigurableBeanFactory { + + private final List beanPostProcessors = new ArrayList<>(); + + @Override + public Object getBean(String beanName) throws BeansException { + return doGetBean(beanName,null); + } + + @Override + public Object getBean(String beanName, Object... args) throws BeansException { + return doGetBean(beanName,args); + } + + protected T doGetBean(String beanName, Object[] args){ + Object bean = getSingleton(beanName); + if(bean != null){ + return (T) bean; + } + BeanDefinition beanDefinition = getBeanDefinition(beanName); + return (T) creatBean(beanName,beanDefinition,args); + + } + @Override + public T getBean(String beanName, Class requiredType) { + return (T)getBean(beanName); + } + + protected abstract Object creatBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException; + + protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException; + + @Override + public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) { + this.beanPostProcessors.remove(beanPostProcessor); + this.beanPostProcessors.add(beanPostProcessor); + } + + public List getBeanPostProcessors(){ + return this.beanPostProcessors; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionReader.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionReader.java new file mode 100644 index 0000000..762f83e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionReader.java @@ -0,0 +1,28 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.core.io.Resource; +import com.xgh.test.spring.step04.core.io.ResourceLoader; + +/** + * com.xgh.test.spring.step04.beans.factory.support.BeanDefinitionReader + * + * @author xgh
+ * @description bean定义读取接口 + * @date 2021年08月27日 + */ +public interface BeanDefinitionReader { + + BeanDefinitionRegistry getRegistry(); + + ResourceLoader getResourceLoader(); + + void loadBeanDefinitions(Resource resource)throws BeansException; + + void loadBeanDefinitions(Resource... resources) throws BeansException; + + void loadBeanDefinitions(String location) throws BeansException; + + void loadBeanDefinitions(String... locations)throws BeansException; + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionRegistry.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionRegistry.java new file mode 100644 index 0000000..cb3f63e --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/BeanDefinitionRegistry.java @@ -0,0 +1,41 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; + +/** + * com.xgh.test.spring.step02.support.BeanDefinitionRegistry + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public interface BeanDefinitionRegistry { + + /** + * @description 向注册表中注册 + * @date 2021/8/27 0027 + * @param beanName + * @param beanDefinition + */ + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition); + + + BeanDefinition getBeanDefinition(String beanName) throws BeansException; + + /** + * @description 判断是否包含指定的beanDefinition + * @date 2021/8/27 0027 + * @param beanName + * @return boolean + */ + boolean containsBeanDefinition(String beanName); + + /** + * @description 返回注册表所有beanName + * @date 2021/8/27 0027 + * @return java.lang.String[] + */ + String[] getBeanDefinitionNames(); + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/CglibSubclassingInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/CglibSubclassingInstantiationStrategy.java new file mode 100644 index 0000000..8cdaa45 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/CglibSubclassingInstantiationStrategy.java @@ -0,0 +1,35 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.NoOp; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.CglibSubclassingInstantiationStrategy + * + * @author xgh
+ * @description + * @date 2021年08月17日 + */ +public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy { + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(beanDefinition.getBeanClass()); + enhancer.setCallback(new NoOp() { + @Override + public int hashCode() { + return super.hashCode(); + } + }); + if(null == ctor){ + return enhancer.create(); + } + return enhancer.create(ctor.getParameterTypes(),args); + } + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultListableBeanFactory.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultListableBeanFactory.java new file mode 100644 index 0000000..5360b2a --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultListableBeanFactory.java @@ -0,0 +1,65 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.ConfigurableBeanFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.step02.support.DefaultListableBeanFactory + * + * @author xgh
+ * @description 核心实现 + * @date 2021年08月17日 + */ +public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory { + + private Map beanDefinitionMap = new HashMap<>(); + + + @Override + public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) { + beanDefinitionMap.put(beanName,beanDefinition); + } + + @Override + public boolean containsBeanDefinition(String beanName) { + return beanDefinitionMap.containsKey(beanName); + } + + @Override + public Map getBeansOfType(Class type) throws BeansException { + Map result = new HashMap<>(); + beanDefinitionMap.forEach((beanName,beanDefinition)->{ + Class beanClass = beanDefinition.getBeanClass(); + if(type.isAssignableFrom(beanClass)){ + result.put(beanName,(T) getBean(beanName)); + } + }); + return result; + } + + @Override + public String[] getBeanDefinitionNames() { + return beanDefinitionMap.keySet().toArray(new String[0]); + } + + @Override + public BeanDefinition getBeanDefinition(String beanName) throws BeansException { + BeanDefinition beanDefinition = beanDefinitionMap.get(beanName); + if(null == beanDefinition){ + throw new BeansException("No bean named '"+ beanName+"' is definded"); + } + return beanDefinition; + } + + @Override + public void preInstantiateSingletons() throws BeansException { + beanDefinitionMap.keySet().forEach(this::getBean); + } + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultSingletonBeanRegistry.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultSingletonBeanRegistry.java new file mode 100644 index 0000000..bdafd82 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DefaultSingletonBeanRegistry.java @@ -0,0 +1,51 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.DisposableBean; +import com.xgh.test.spring.step04.beans.factory.config.SingletonBeanRegistry; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * com.xgh.test.spring.step02.support.DefaultSingletinBeanRegistry + * + * @author xgh
+ * @description 单例实例的获取接口 + * @date 2021年08月17日 + */ +public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry { + + private Map singletonObjects = new HashMap<>(); + + private final Map disposableBeans = new HashMap<>(); + + @Override + public Object getSingleton(String beanName) { + return singletonObjects.get(beanName); + } + + protected void addSingleton(String beanName,Object singletonObject){ + singletonObjects.put(beanName,singletonObject); + } + + public void registerDisposableBean(String beanName,DisposableBean bean){ + disposableBeans.put(beanName,bean); + } + + + public void destroySingletons(){ + Set keySet= this.disposableBeans.keySet(); + Object[] disposableBeanNames = keySet.toArray(); + for (int i = disposableBeanNames.length-1 ; i >= 0; i--){ + Object beanName = disposableBeanNames[i]; + DisposableBean disposableBean = disposableBeans.remove(beanName); + try { + disposableBean.destroy(); + }catch (Exception e){ + throw new BeansException("Destroy method on bean with name '" + beanName + "' threw an exception", e); + } + } + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DisposableBeanAdapter.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DisposableBeanAdapter.java new file mode 100644 index 0000000..3d719bb --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/DisposableBeanAdapter.java @@ -0,0 +1,43 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import cn.hutool.core.util.StrUtil; +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.DisposableBean; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; + +import java.lang.reflect.Method; + + +/** + * bean 实例销毁步鄹 + */ +public class DisposableBeanAdapter implements DisposableBean { + + private final Object bean; + + private final String beanName; + + private String destroyMethodName; + + public DisposableBeanAdapter(Object bean, String beanName, BeanDefinition beanDefinition) { + this.bean = bean; + this.beanName = beanName; + this.destroyMethodName = beanDefinition.getDestroyMethodName(); + } + + @Override + public void destroy() throws Exception { + if(bean instanceof DisposableBean){ + ((DisposableBean) bean).destroy(); + } + //注解配置destroy-method(判断是为了必维二次执行销毁) + if(StrUtil.isNotEmpty(destroyMethodName) && !(bean instanceof DisposableBean && "destroy".equals(this.destroyMethodName))){ + Method destroyMethod = bean.getClass().getMethod(destroyMethodName); + if(null == destroyMethod){ + throw new BeansException("Couldn't find a destroy method named '" + destroyMethodName + "' on bean with name '" + beanName + "'"); + } + destroyMethod.invoke(bean); + } + + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/InstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/InstantiationStrategy.java new file mode 100644 index 0000000..abc3e52 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/InstantiationStrategy.java @@ -0,0 +1,17 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.step02.support.InstantionStrategy + * + * @author xgh
+ * @description 实例化策略 + * @date 2021年08月17日 + */ +public interface InstantiationStrategy { + + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args); +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/support/SimpleInstantiationStrategy.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/SimpleInstantiationStrategy.java new file mode 100644 index 0000000..29c9d6a --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/support/SimpleInstantiationStrategy.java @@ -0,0 +1,34 @@ +package com.xgh.test.spring.step04.beans.factory.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +/** + * com.xgh.test.spring.step02.support.SimpleInstantiationStrategy + * + * @author xgh
+ * @description jdk + * @date 2021年08月17日 + */ +public class SimpleInstantiationStrategy implements InstantiationStrategy { + + + @Override + public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) { + Class clazz = beanDefinition.getBeanClass(); + try { + if (null != ctor) { + return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args); + }else { + return clazz.getDeclaredConstructor().newInstance(); + } + }catch(InstantiationException |IllegalAccessException|InvocationTargetException | NoSuchMethodException e) { + throw new BeansException("Failed to instantiate["+clazz.getName()+"]",e); + } + } + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/beans/factory/xml/XmlBeanDefinitionReader.java b/src/main/java/com/xgh/test/spring/step04/beans/factory/xml/XmlBeanDefinitionReader.java new file mode 100644 index 0000000..e845d5b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/beans/factory/xml/XmlBeanDefinitionReader.java @@ -0,0 +1,121 @@ +package com.xgh.test.spring.step04.beans.factory.xml; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.XmlUtil; +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.PropertyValue; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.BeanReference; +import com.xgh.test.spring.step04.beans.factory.support.AbstractBeanDefinitionReader; +import com.xgh.test.spring.step04.beans.factory.support.BeanDefinitionRegistry; +import com.xgh.test.spring.step04.core.io.Resource; +import com.xgh.test.spring.step04.core.io.ResourceLoader; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.io.IOException; +import java.io.InputStream; + +/** + * com.xgh.test.spring.step04.beans.factory.xml.XmlBeanDefinitionReader + * + * @author xgh
+ * @description 解析xml处理bean注册 + * @date 2021年08月27日 + */ +public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { + + + public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) { + super(registry); + } + + public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) { + super(registry, resourceLoader); + } + + @Override + public void loadBeanDefinitions(Resource resource) throws BeansException { + + try { + try (InputStream inputStream = resource.getInputStream()) { + doLoadBeanDefinitions(inputStream); + } + } catch (IOException | ClassNotFoundException e) { + throw new BeansException("IOException parsing XML document form" + resource, e); + } + } + + protected void doLoadBeanDefinitions(InputStream inputStream) throws ClassNotFoundException { + Document doc = XmlUtil.readXML(inputStream); + Element root = doc.getDocumentElement(); + NodeList childNodes = root.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + if (!(childNodes.item(i) instanceof Element)) { + continue; + } + if (!"bean".equals(childNodes.item(i).getNodeName())) { + continue; + } + + Element bean = (Element) childNodes.item(i); + + String id = bean.getAttribute("id"); + String name = bean.getAttribute("name"); + String className = bean.getAttribute("class"); + Class clazz = Class.forName(className); + String beanName = StrUtil.isNotEmpty(id) ? id : name; + if (StrUtil.isEmpty(beanName)) { + beanName = StrUtil.lowerFirst(clazz.getSimpleName()); + } + //定义bean + BeanDefinition beanDefinition = new BeanDefinition(clazz); + //读取属性填充 + for (int j = 0; j < bean.getChildNodes().getLength(); j++) { + if (!(bean.getChildNodes().item(j) instanceof Element)) { + continue; + } + if (!"property".equals(bean.getChildNodes().item(j).getNodeName())) { + continue; + } + + Element property = (Element) bean.getChildNodes().item(j); + String attrName = property.getAttribute("name"); + String attrValue = property.getAttribute("value"); + String attrRef = property.getAttribute("ref"); + //获取属性值:引入对象值对象 + Object value = StrUtil.isNotEmpty(attrRef) ? new BeanReference(attrRef) : attrValue; + PropertyValue propertyValue = new PropertyValue(attrName, value); + beanDefinition.getPropertyValues().addPropertyValue(propertyValue); + } + if (getRegistry().containsBeanDefinition(beanName)) { + throw new BeansException("Duplicate beanName[ " + beanName + "] is not allowed"); + } + //注册bd + getRegistry().registerBeanDefinition(beanName, beanDefinition); + } + } + + @Override + public void loadBeanDefinitions(Resource... resources) throws BeansException { + for (Resource resource : resources) { + loadBeanDefinitions(resource); + } + } + + @Override + public void loadBeanDefinitions(String location) throws BeansException { + ResourceLoader resourceLoader = getResourceLoader(); + Resource resource = resourceLoader.getResource(location); + loadBeanDefinitions(resource); + } + + @Override + public void loadBeanDefinitions(String... locations) throws BeansException { + for (String location : locations) { + loadBeanDefinitions(location); + } + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/ApplicationContext.java b/src/main/java/com/xgh/test/spring/step04/context/ApplicationContext.java new file mode 100644 index 0000000..098fead --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/ApplicationContext.java @@ -0,0 +1,6 @@ +package com.xgh.test.spring.step04.context; + +import com.xgh.test.spring.step04.beans.factory.ListableBeanFactory; + +public interface ApplicationContext extends ListableBeanFactory { +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/ConfigurableApplicationContext.java b/src/main/java/com/xgh/test/spring/step04/context/ConfigurableApplicationContext.java new file mode 100644 index 0000000..e01d382 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/ConfigurableApplicationContext.java @@ -0,0 +1,18 @@ +package com.xgh.test.spring.step04.context; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.ListableBeanFactory; + +public interface ConfigurableApplicationContext extends ListableBeanFactory { + + + /** + * 刷新容器 + * @throws BeansException + */ + void refresh() throws BeansException; + + void registerShutdownHook(); + + void close(); +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/support/AbstractApplicationContext.java b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractApplicationContext.java new file mode 100644 index 0000000..eda5539 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractApplicationContext.java @@ -0,0 +1,94 @@ +package com.xgh.test.spring.step04.context.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanFactoryPostProcessor; +import com.xgh.test.spring.step04.beans.factory.config.BeanPostProcessor; +import com.xgh.test.spring.step04.context.ConfigurableApplicationContext; +import com.xgh.test.spring.step04.core.io.DefaultResourceLoader; + +import java.util.Map; + + +/** + * 应用上下文抽象类实现 + */ +public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { + + + @Override + public void refresh() throws BeansException { + //1创建beanFactory,加载BeanDefinition + refreshFactory(); + + //2.获取beanFactory + ConfigurableListableBeanFactory beanFactory = getBeanFactory(); + + //3在bean实例化之前执行beanFactoryPostProcess + invokeBeanFactoryPostProcessors(beanFactory); + //4.beanPostProcessor需要提前雨其他bean对象实例化之前注册操作 + registerBeanPostProcessors(beanFactory); + //5提前实例化单例bean对象 + beanFactory.preInstantiateSingletons(); + + + } + protected abstract ConfigurableListableBeanFactory getBeanFactory() throws BeansException; + + + protected abstract void refreshFactory() ; + + private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory){ + Map beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class); + for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) { + beanFactory.addBeanPostProcessor(beanPostProcessor); + } + //... getBean getBeansOfType getBeanDefinitionNames; + } + + @Override + public Object getBean(String beanName) throws BeansException { + return getBeanFactory().getBean(beanName); + } + + @Override + public T getBean(String beanName, Class requiredType) { + return getBeanFactory().getBean(beanName,requiredType); + } + + @Override + public Object getBean(String beanName, Object... args) throws BeansException { + return getBeanFactory().getBean(beanName,args); + } + + @Override + public Map getBeansOfType(Class type) throws BeansException { + return getBeanFactory().getBeansOfType(type); + } + + @Override + public String[] getBeanDefinitionNames() { + return getBeanFactory().getBeanDefinitionNames(); + } + + private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){ + Map beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class); + for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) { + beanFactoryPostProcessor.postProcessBeanFactory(beanFactory); + } + } + + public void registerShutdownHook(){ + Runtime.getRuntime().addShutdownHook(new Thread(this::close)); + } + + public void close(){ + getBeanFactory().destroySingletons(); + } + + + + + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/support/AbstractRefreshableApplication.java b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractRefreshableApplication.java new file mode 100644 index 0000000..b313493 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractRefreshableApplication.java @@ -0,0 +1,33 @@ +package com.xgh.test.spring.step04.context.support; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.support.DefaultListableBeanFactory; + +/** + * 获取bean工厂和加载资源 + */ +public abstract class AbstractRefreshableApplication extends AbstractApplicationContext { + + private DefaultListableBeanFactory beanFactory; + + + @Override + protected void refreshFactory() throws BeansException { + DefaultListableBeanFactory beanFactory = creatBeanFactory(); + //加载spring.xml配置文件 + loadBeanDefinitions(beanFactory); + this.beanFactory = beanFactory; + } + + protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory); + + private DefaultListableBeanFactory creatBeanFactory() { + return new DefaultListableBeanFactory(); + } + + @Override + protected ConfigurableListableBeanFactory getBeanFactory() throws BeansException { + return beanFactory; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/support/AbstractXmlApplicationContext.java b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractXmlApplicationContext.java new file mode 100644 index 0000000..7137aea --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/support/AbstractXmlApplicationContext.java @@ -0,0 +1,25 @@ +package com.xgh.test.spring.step04.context.support; + +import com.xgh.test.spring.step04.beans.factory.support.DefaultListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.xml.XmlBeanDefinitionReader; + +/** + * 上下文对配置信息的加载 + */ +public abstract class AbstractXmlApplicationContext extends AbstractRefreshableApplication { + + + @Override + protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) { + XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory, this); + + String[] configLocations = getConfigLocations(); + if( null != configLocations){ + beanDefinitionReader.loadBeanDefinitions(configLocations); + } + } + + protected abstract String[] getConfigLocations(); + + +} diff --git a/src/main/java/com/xgh/test/spring/step04/context/support/ClassPathXmlApplicationContext.java b/src/main/java/com/xgh/test/spring/step04/context/support/ClassPathXmlApplicationContext.java new file mode 100644 index 0000000..55c4c46 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/context/support/ClassPathXmlApplicationContext.java @@ -0,0 +1,27 @@ +package com.xgh.test.spring.step04.context.support; + +/** + * 应用上下文实现类 + */ + +public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext{ + + private String[] configLocations; + + public ClassPathXmlApplicationContext() { + } + + public ClassPathXmlApplicationContext(String configLocation){ + this(new String[]{configLocation}); + } + + public ClassPathXmlApplicationContext(String[] configLocations) { + this.configLocations = configLocations; + refresh(); + } + + @Override + protected String[] getConfigLocations() { + return configLocations; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/ClassPathResource.java b/src/main/java/com/xgh/test/spring/step04/core/io/ClassPathResource.java new file mode 100644 index 0000000..d4dcad9 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/ClassPathResource.java @@ -0,0 +1,41 @@ +package com.xgh.test.spring.step04.core.io; + +import cn.hutool.core.lang.Assert; +import com.xgh.test.spring.step04.util.ClassUtils; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; + +/** + * com.xgh.test.spring.step04.core.io.ClassPathResource + * + * @author xgh
+ * @description classPath的资源加载器 + * @date 2021年08月27日 + */ +public class ClassPathResource implements Resource{ + + private final String path; + + private ClassLoader classLoader; + + public ClassPathResource(String path) { + this(path,null); + } + + public ClassPathResource(String path, ClassLoader classLoader) { + Assert.notNull(path,"path mast not be null"); + this.path = path; + this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); + } + + @Override + public InputStream getInputStream() throws IOException { + InputStream is = classLoader.getResourceAsStream(path); + if(is == null){ + throw new FileNotFoundException(this.path + " cannot be opened because ist does not exist"); + } + return is; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/DefaultResourceLoader.java b/src/main/java/com/xgh/test/spring/step04/core/io/DefaultResourceLoader.java new file mode 100644 index 0000000..dd66fab --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/DefaultResourceLoader.java @@ -0,0 +1,35 @@ +package com.xgh.test.spring.step04.core.io; + +import cn.hutool.core.lang.Assert; + +import java.net.MalformedURLException; +import java.net.URL; + +/** + * com.xgh.test.spring.step04.core.io.DefaultResourceLoader + * + * @author xgh
+ * @description 获取资源 + * @date 2021年08月27日 + */ +public class DefaultResourceLoader implements ResourceLoader{ + + + + @Override + public Resource getResource(String location) { + Assert.notNull(location,"location mast not be null"); + if(location.startsWith(CLASSPATH_URL_PREFIX)){ + return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length())); + } + else + { + try { + URL url = new URL(location); + return new UrlResource(url); + }catch (MalformedURLException e){ + return new FileSystemResource(location); + } + } + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/FileSystemResource.java b/src/main/java/com/xgh/test/spring/step04/core/io/FileSystemResource.java new file mode 100644 index 0000000..f8831d7 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/FileSystemResource.java @@ -0,0 +1,40 @@ +package com.xgh.test.spring.step04.core.io; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * com.xgh.test.spring.step04.core.io.FileSystemResource + * + * @author xgh
+ * @description 文件资源加载 + * @date 2021年08月27日 + */ +public class FileSystemResource implements Resource{ + + private final File file; + + private final String path; + + public FileSystemResource(File file) { + this.file = file; + this.path = file.getPath(); + } + + + public FileSystemResource(String path) { + this.file = new File(path); + this.path = path; + } + + @Override + public InputStream getInputStream() throws IOException { + return new FileInputStream(this.file); + } + + public String getPath() { + return path; + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/Resource.java b/src/main/java/com/xgh/test/spring/step04/core/io/Resource.java new file mode 100644 index 0000000..305f04b --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/Resource.java @@ -0,0 +1,16 @@ +package com.xgh.test.spring.step04.core.io; + +import java.io.IOException; +import java.io.InputStream; + +/** + * com.xgh.test.spring.step04.core.io.Resource + * + * @author xgh
+ * @description 资源加载接口的定义和实现 + * @date 2021年08月27日 + */ +public interface Resource { + + InputStream getInputStream() throws IOException; +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/ResourceLoader.java b/src/main/java/com/xgh/test/spring/step04/core/io/ResourceLoader.java new file mode 100644 index 0000000..2268713 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/ResourceLoader.java @@ -0,0 +1,15 @@ +package com.xgh.test.spring.step04.core.io; + +/** + * com.xgh.test.spring.step04.core.io.ResourceLoader + * + * @author xgh
+ * @description 资源加载器 + * @date 2021年08月27日 + */ +public interface ResourceLoader { + + String CLASSPATH_URL_PREFIX= "classpath:"; + + Resource getResource(String location); +} diff --git a/src/main/java/com/xgh/test/spring/step04/core/io/UrlResource.java b/src/main/java/com/xgh/test/spring/step04/core/io/UrlResource.java new file mode 100644 index 0000000..b5c3cf3 --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/core/io/UrlResource.java @@ -0,0 +1,39 @@ +package com.xgh.test.spring.step04.core.io; + +import cn.hutool.core.lang.Assert; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +/** + * com.xgh.test.spring.step04.core.io.UrlResource + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public class UrlResource implements Resource{ + + private final URL url; + + public UrlResource(URL url) { + Assert.notNull(url,"url mast not be null"); + this.url = url; + } + + @Override + public InputStream getInputStream() throws IOException { + URLConnection con = this.url.openConnection(); + try { + return con.getInputStream(); + }catch (IOException e){ + if(con instanceof HttpURLConnection){ + ((HttpURLConnection) con).disconnect(); + } + throw e; + } + } +} diff --git a/src/main/java/com/xgh/test/spring/step04/util/ClassUtils.java b/src/main/java/com/xgh/test/spring/step04/util/ClassUtils.java new file mode 100644 index 0000000..d102e6a --- /dev/null +++ b/src/main/java/com/xgh/test/spring/step04/util/ClassUtils.java @@ -0,0 +1,24 @@ +package com.xgh.test.spring.step04.util; + +/** + * com.xgh.test.spring.step04.util.ClassUtils + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public class ClassUtils { + + public static ClassLoader getDefaultClassLoader(){ + ClassLoader cl = null; + try{ + cl = Thread.currentThread().getContextClassLoader(); + }catch (Throwable ex){ + System.out.println(ex); + } + if(cl == null){ + cl = ClassUtils.class.getClassLoader(); + } + return cl; + } +} diff --git a/src/main/java/com/xgh/test/thread/week02/AlarmAgent.java b/src/main/java/com/xgh/test/thread/week02/AlarmAgent.java new file mode 100644 index 0000000..a90dfb3 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/AlarmAgent.java @@ -0,0 +1,173 @@ +package com.xgh.test.thread.week02; + +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * com.xgh.test.thread.week2.AlarmAgent + * + * @author xgh
+ * @description + * @date 2021年07月27日 + */ +public class AlarmAgent { + + //报警系统是否连接上了报警服务器 + private volatile boolean connectedToServer = false; + + //保护性条件 + Predicate agentConnected = new Predicate() { + @Override + public boolean evaluate() { + return connectedToServer; + } + }; + + //blocker对象 + private Blocker blocker = new ConditionVarBlocker(false); + + //上报报警信息给报警服务 + public void sendAlarm(AlarmInfo alarmInfo) throws Exception { + //构建guardedAction(要执行的方法) + GuardedAction guardedAction = new GuardedAction(agentConnected) { + @Override + public Void call() throws Exception { + //执行目标函数 + doSendAlarm(alarmInfo); + return null; + } + }; + //通过blocker执行目标(判断是否连接成功,如果连接成功就调用目标方法doSendAlarm发送报警通知) + blocker.callWithGuard(guardedAction); + + } + + //发送报警信息给报警服务器 + private void doSendAlarm(AlarmInfo alarmInfo) { + //建立socket连接 + System.out.println("执行发送报警方法start send alarm " + alarmInfo); + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + //虚拟上报 + System.out.println("end send alarm"); + } + + + //init: 初始化报警服务,和报警服务器建立连接,并定时发送心跳信息 + //sendAlarm:发送报警信息给服务器 + //onConnected: 和报警中心建立连接 + //reConnected: 重新和报警中心建立连接 + //onDisconnected: 断开和报警中心的连接 + public void init() { + //出发连接 + Thread connectingThread = new Thread(new ConnectingTask()); + connectingThread.start(); + //定时任务 + ScheduledThreadPoolExecutor heardThreadPoolExecutor = new ScheduledThreadPoolExecutor(5, new ThreadFactory() { + private AtomicInteger index = new AtomicInteger(); + + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(); + thread.setName("heard-thread-" + index.incrementAndGet()); + thread.setDaemon(true); + System.out.println("heard-thread-" + index); + return thread; + } + }); + //5000表示首次执行任务的延迟时间,2000表示每次执行任务的间隔时间,TimeUnit.MILLISECONDS执行的时间间隔数值单位 + //每5s执行一次 + heardThreadPoolExecutor.scheduleAtFixedRate(new HeartbeatTask(), 5000, 2000, TimeUnit.MILLISECONDS); + } + + + //确定报警器和服务建立连接 + private void onConnected() { + try { + System.out.println("连接成功后将标识位connectedServer 修改成true,同时唤醒其他的等待线程"); + blocker.signalAfter(() -> { + System.out.println("update connectedServer = true "); + connectedToServer = true; + return Boolean.TRUE; + }); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + //重新连接 + private void reconnected() { + //重新连接一次 + ConnectingTask connectingTask = new ConnectingTask(); + //直接通过心跳线程执行一次重连, + connectingTask.run(); + } + + //断开连接 + private void onDisconnected() { + connectedToServer = false; + } + + /* + * @description 测试连接是否正常 + * @date 2021/7/27 0027 + * @return boolean + */ + private boolean tesConnection() { + //通过socket给报警服务器发送一次连接 + //模拟发送一次 + try { + Thread.sleep(50); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("test connection normal"); + return true; + } + + //与报警服务建立连接 + class ConnectingTask implements Runnable { + + @Override + public void run() { + //与服务器连接 + System.out.println("与服务连接执行"); + try { + //休息10s + System.out.println("睡十秒假装连接"); + Thread.sleep(10 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + //连接建立完成1 + System.out.println("alarm connected 假装连接成功"); + + onConnected(); + } + + + } + + class HeartbeatTask implements Runnable { + + @Override + public void run() { + if (!tesConnection()) { + //连接断开 + onDisconnected(); + //重新连接 + reconnected(); + } + } + + + } + + +} diff --git a/src/main/java/com/xgh/test/thread/week02/AlarmAgentTest.java b/src/main/java/com/xgh/test/thread/week02/AlarmAgentTest.java new file mode 100644 index 0000000..95cc2ed --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/AlarmAgentTest.java @@ -0,0 +1,23 @@ +package com.xgh.test.thread.week02; + +/** + * com.xgh.test.thread.week2.AlarmAgentTest + * + * @author xgh
+ * @description + * @date 2021年07月27日 + */ +public class AlarmAgentTest { + + public static void main(String[] args) { + AlarmAgent alarmAgent = new AlarmAgent(); + alarmAgent.init(); + + AlarmInfo alarmInfo = new AlarmInfo(6, 2, "1101", 1); + try { + alarmAgent.sendAlarm(alarmInfo); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/xgh/test/thread/week02/AlarmInfo.java b/src/main/java/com/xgh/test/thread/week02/AlarmInfo.java new file mode 100644 index 0000000..0211c67 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/AlarmInfo.java @@ -0,0 +1,34 @@ +package com.xgh.test.thread.week02; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * com.xgh.test.thread.week2.AlarmInfo + * + * @author xgh
+ * @description + * @date 2021年07月27日 + */ +@Data +@AllArgsConstructor +public class AlarmInfo { + + + //楼号 + private Integer no; + + //几单元 + private Integer unit; + + //几零几 + private String roomNumber; + + + //报警类型 + private Integer alarmType; + + + + +} diff --git a/src/main/java/com/xgh/test/thread/week02/Blocker.java b/src/main/java/com/xgh/test/thread/week02/Blocker.java new file mode 100644 index 0000000..9b8c134 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/Blocker.java @@ -0,0 +1,27 @@ +package com.xgh.test.thread.week02; + +import java.util.concurrent.Callable; + +/** + * com.xgh.test.thread.week2.Blocker + * + * @author xgh
+ * @description 负责对guardAction进行阻塞和唤醒 + * @date 2021年07月27日 + */ +public interface Blocker { + + + //在保护条件成立时执行目标动作,否则阻塞当前线程,直到保护条件成立 + V callWithGuard(GuardedAction guardedAction)throws Exception; + + + //先执行stateOperator,如果返回true则确定唤醒该blocker上阻塞的一共线程 + void signalAfter(Callable stateOperation)throws Exception; + + //直接欢迎blocker上阻塞的一个线程 + void signal()throws Exception; + + //根据stateOperator的是否满足唤醒所有blocker上线程 + void broadcastAfter(Callable stateOperator )throws Exception; +} diff --git a/src/main/java/com/xgh/test/thread/week02/ConditionVarBlocker.java b/src/main/java/com/xgh/test/thread/week02/ConditionVarBlocker.java new file mode 100644 index 0000000..5f844c4 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/ConditionVarBlocker.java @@ -0,0 +1,109 @@ +package com.xgh.test.thread.week02; + +import java.util.concurrent.Callable; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * com.xgh.test.thread.week2.ConditionVarBlocker + * + * @author xgh
+ * @description 延时等待 + * @date 2021年07月27日 + */ +public class ConditionVarBlocker implements Blocker{ + + private final Lock lock; + + private final Condition condition; + + private final boolean allowAccess2Lock; + + public ConditionVarBlocker(Lock lock, Condition condition, boolean allowAccess2Lock) { + this.lock = lock; + this.condition = condition; + this.allowAccess2Lock = allowAccess2Lock; + } + + public ConditionVarBlocker(boolean allowAccess2Lock) { + this(new ReentrantLock(),allowAccess2Lock); + } + + public ConditionVarBlocker(Lock lock, boolean allowAccess2Lock) { + this.lock = lock; + this.condition = lock.newCondition(); + this.allowAccess2Lock = allowAccess2Lock; + } + + //判断是否与中心连接成功,连接成功就发送消息 + //如果没有连接成功就等待 + @Override + public V callWithGuard(GuardedAction guardedAction) throws Exception { + lock.lockInterruptibly(); + try { + //判断条件是否满足满足则执行目标动作,不满足则进入条件等待队列中 + final Predicate predicate = guardedAction.predicate; + //对应connectedToServer的值的改变 + while (!predicate.evaluate()){ + + System.out.println("判断是否能够执行,如果不行,线程进入等待 alarm connecting alarm system,thread wait"); + //条件不满足 + condition.await(); + //当线程从条件等待队列欢迎后,获取锁成功,然后再次尝试去判断条件是否满足 + } + //条件满足,执行目标内容 + System.out.println("条件满足,执行线程,alarm connected execute call"); + return guardedAction.call(); + }finally { + lock.unlock(); + } + } + + @Override + public void signalAfter(Callable stateOperation) throws Exception { + //获取锁 + lock.lockInterruptibly(); + try { + //执行传入的方法,看是否能唤醒线程 + if(stateOperation.call()){ + //条件满足唤醒 + System.out.println("唤醒等待线程,alarm connected,signal thread"); + condition.signal(); + } + }finally { + lock.unlock(); + } + } + + @Override + public void signal() throws Exception { + lock.lockInterruptibly(); + try{ + condition.signal(); + }finally { + lock.unlock(); + } + } + + @Override + public void broadcastAfter(Callable stateOperator) throws Exception { + lock.lockInterruptibly(); + try { + if(stateOperator.call()){ + //条件满足唤醒所有 + condition.signalAll(); + } + }finally { + lock.unlock(); + } + } + + public Lock getLock(){ + if(allowAccess2Lock){ + return this.lock; + } + throw new IllegalStateException("access to the lock disallowed"); + } + +} diff --git a/src/main/java/com/xgh/test/thread/week02/GuardedAction.java b/src/main/java/com/xgh/test/thread/week02/GuardedAction.java new file mode 100644 index 0000000..1028382 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/GuardedAction.java @@ -0,0 +1,21 @@ +package com.xgh.test.thread.week02; + +import java.util.concurrent.Callable; + +/** + * com.xgh.test.thread.week2.GuardedAction + * + * @author xgh
+ * @description 抽象目标动作,内部包含目标动作所需的保护条件 + * @date 2021年07月27日 + */ +public abstract class GuardedAction implements Callable { + + protected final Predicate predicate; + + public GuardedAction(Predicate predicate) { + this.predicate = predicate; + } + + +} diff --git a/src/main/java/com/xgh/test/thread/week02/Predicate.java b/src/main/java/com/xgh/test/thread/week02/Predicate.java new file mode 100644 index 0000000..a475b6a --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/Predicate.java @@ -0,0 +1,18 @@ +package com.xgh.test.thread.week02; + +/** + * com.xgh.test.thread.week2.Predicate + * + * @author xgh
+ * @description + * @date 2021年07月27日 + */ +public interface Predicate { + + /* + * @description 判断条件是否满足,满足返回true 否则false + * @date 2021/7/27 0027 + * @return boolean + */ + boolean evaluate(); +} diff --git a/src/main/java/com/xgh/test/thread/week02/RequestQueue.java b/src/main/java/com/xgh/test/thread/week02/RequestQueue.java new file mode 100644 index 0000000..dcc721a --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week02/RequestQueue.java @@ -0,0 +1,60 @@ +package com.xgh.test.thread.week02; + +import java.util.Queue; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * com.xgh.test.thread.week2.RequestQueue + * + * @author xgh
+ * @description + * @date 2021年07月28日 + */ +public class RequestQueue { + + + private Queue queue = new ArrayBlockingQueue<>(Integer.MAX_VALUE); + + ReentrantLock lock = new ReentrantLock(); + + Condition condition = lock.newCondition(); + + public String get() { + String result = null; + lock.lock(); + try { + while (queue.isEmpty()) { + condition.await(); + } + result = queue.poll(); + condition.signalAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + condition.signalAll(); + } finally { + lock.unlock(); + } + + return result; + } + + public void put(String request){ + lock.lock(); + try { + while (queue.size() >= Integer.MAX_VALUE){ + condition.await(); + } + queue.offer(request); + condition.signalAll(); + }catch (InterruptedException e){ + condition.signalAll(); + }finally { + lock.unlock(); + } + + } + + +} diff --git a/src/main/java/com/xgh/test/thread/week03/AbstractTerminationThread.java b/src/main/java/com/xgh/test/thread/week03/AbstractTerminationThread.java new file mode 100644 index 0000000..591f0fb --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AbstractTerminationThread.java @@ -0,0 +1,89 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.AbstractTerminationThread + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public abstract class AbstractTerminationThread extends Thread implements Termination{ + + + /** + * + * 线程共享停止的标志实例对象 + */ + public final TerminationToken terminationToken; + + + public AbstractTerminationThread(){ + this(new TerminationToken()); + } + + public AbstractTerminationThread(TerminationToken terminationToken){ + this.terminationToken = terminationToken; + System.out.println("注册告警线程到线程停的标志实现对象队列中"); + terminationToken.register(this); + } + + @Override + public void terminate() { + System.out.println("设置中断标志对象为中断状态"); + this.terminationToken.setToShutdown(true); + try { + doTerminate(); + }finally { + //如果没有等待的任务,则强制停止线程 + if(terminationToken.reservations.get()<=0){ + super.interrupt(); + } + } + } + + //执行终止线程的逻辑,留给子类实现 + protected void doTerminate(){ + + }; + + @Override + public void run() { + Exception ex = null; + try { + for (;;){ + System.out.println("告警线程执行,此时中断标志位:"+terminationToken.isToShutdown() +",未完成的任务数量:"+terminationToken.reservations.get()); + if(terminationToken.isToShutdown() && terminationToken.reservations.get() <= 0){ + //线程已经终止了,中断退出 + System.out.println("中断标志位true,未完成任务为0,告警线程退出"); + break; + } + //执行具体逻辑 + doRun(); + } + }catch (Exception e){ + ex = e; + if(ex instanceof InterruptedException){ + System.out.println("中断响应:"+e); + } + }finally { + try { + System.out.println("告警线程停止,回调终止后的清理工作开始"); + doCleanup(ex); + }finally { + //通知terminationToken管理的所有线程退出 + System.out.println("标志实例对象中一个线程终止,通知其他线程终止"); + terminationToken.notifyThreadTermination(this); + } + } + + + + } + + //留给子类实现,完成线程终止后的一些清理工作 + protected void doCleanup(Exception ex){ + + }; + + protected abstract void doRun()throws InterruptedException; +} diff --git a/src/main/java/com/xgh/test/thread/week03/AlarmInfo.java b/src/main/java/com/xgh/test/thread/week03/AlarmInfo.java new file mode 100644 index 0000000..ae93292 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AlarmInfo.java @@ -0,0 +1,44 @@ +package com.xgh.test.thread.week03; + +import lombok.Data; + +/** + * com.xgh.test.thread.week03.AlarmInfo + * + * @author xgh
+ * @description 机器告警信息 + * @date 2021年08月02日 + */ +@Data +public class AlarmInfo { + + //机器告警类型 + private AlarmType alarmType; + + //机器告警编号 + private String id; + + //机器告警参数 + private String extraInfo; + + + public AlarmInfo(AlarmType alarmType, String id, String extraInfo) { + this.alarmType = alarmType; + this.id = id; + this.extraInfo = extraInfo; + } + + //获取机器告警信息的唯一id + public String getUniqueId(){ + return this.alarmType.getDesc() +":"+this.id +":"+this.extraInfo; + } + + public String getUniqueByAlarmType(AlarmType alarmType){ + return alarmType.getDesc() +":"+this.id +":"+this.extraInfo; + } + + + + + +} diff --git a/src/main/java/com/xgh/test/thread/week03/AlarmManager.java b/src/main/java/com/xgh/test/thread/week03/AlarmManager.java new file mode 100644 index 0000000..a5eb6ee --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AlarmManager.java @@ -0,0 +1,57 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.AlarmManager + * + * @author xgh
+ * @description 自动对机器进行监控,机器的报警信息会上报到监控系统中 + * @date 2021年08月02日 + */ +public class AlarmManager { + //是否关闭 + private volatile boolean shutdownRequested = false; + + private static final AlarmManager instance = new AlarmManager(); + + //上报机器的报警信息工作现场 + private final AlarmSendingThread alarmSendingThread; + + private AlarmManager(){ + System.out.println("创建机器上报告警信息的后台线程"); + alarmSendingThread = new AlarmSendingThread(); + } + + public static AlarmManager getInstance(){ + return instance; + } + + + public void init(){ + alarmSendingThread.start(); + } + + public int sendAlarm(AlarmType alarmType,String id,String extraInfo){ + AlarmInfo alarmInfo = new AlarmInfo(alarmType,id,extraInfo); + //重复提交次数 + int duplicateSubmissionCount = 0; + try { + duplicateSubmissionCount = alarmSendingThread.sendAlarm(alarmInfo); + }catch (Exception e){ + e.printStackTrace(); + } + return duplicateSubmissionCount; + } + //关闭机器上报功能 + public synchronized void shutdown(){ + if(shutdownRequested){ + throw new IllegalStateException("停止请求已经发送"); + } + //关闭后台警告线程 + alarmSendingThread.terminate(); + shutdownRequested = true; + } + + + + +} diff --git a/src/main/java/com/xgh/test/thread/week03/AlarmManagerTest.java b/src/main/java/com/xgh/test/thread/week03/AlarmManagerTest.java new file mode 100644 index 0000000..5a50c72 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AlarmManagerTest.java @@ -0,0 +1,44 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.AlarmManagerTest + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public class AlarmManagerTest { + + public static void main(String[] args) { + // 告警管理组件 + AlarmManager alarmManager = AlarmManager.getInstance(); + // 初始化告警管理组件 启动一个告警后台线程来上报请求到告警服务器中 + + alarmManager.init(); + + // 发送告警任务 + new Thread(() -> { + int duplicateAlarmNumber = alarmManager.sendAlarm(AlarmType.FAULT, "001", "001告警信息"); + System.out.println("发送机器报警001完成,001重复提交次数:" + duplicateAlarmNumber); + }).start(); + + new Thread(() -> { + int duplicateAlarmNumber = alarmManager.sendAlarm(AlarmType.FAULT, "002", "002告警信息"); + System.out.println("发送机器报警002完成,002重复提交次数:" + duplicateAlarmNumber); + }).start(); + + new Thread(() -> { + int duplicateAlarmNumber = alarmManager.sendAlarm(AlarmType.FAULT, "002", "002告警信息"); + System.out.println("发送机器报警002完成,002重复提交次数:" + duplicateAlarmNumber); + }).start(); + + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // 终止机器的告警 + new Thread(alarmManager::shutdown).start(); + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/AlarmSendingThread.java b/src/main/java/com/xgh/test/thread/week03/AlarmSendingThread.java new file mode 100644 index 0000000..443201c --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AlarmSendingThread.java @@ -0,0 +1,88 @@ +package com.xgh.test.thread.week03; + +import java.util.Objects; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * com.xgh.test.thread.week03.AlarmSendingThread + * + * @author xgh
+ * @description 上报机器报警信息的工作线程 + * @date 2021年08月02日 + */ +public class AlarmSendingThread extends AbstractTerminationThread { + + //机器告警队列 + private final BlockingQueue alarmQueue; + + //已经提交的机器告警信息 + private final ConcurrentHashMap submittedAlarmRegistry; + + public AlarmSendingThread() { + this.alarmQueue = new ArrayBlockingQueue<>(100); + this.submittedAlarmRegistry = new ConcurrentHashMap<>(); + } + + public int sendAlarm(AlarmInfo alarmInfo) { + if (terminationToken.isToShutdown()) { + //已经终止 + System.err.println("已经终止了:" + alarmInfo); + return -1; + } + try { + //放入机器的告警队列中 + AtomicInteger prevSubmittedCounter; + prevSubmittedCounter = submittedAlarmRegistry.putIfAbsent(alarmInfo.getUniqueId(), new AtomicInteger()); + if (prevSubmittedCounter == null) { + //代表之前该类型的机器告警为空 ;未完成任务+1 + terminationToken.reservations.incrementAndGet(); + alarmQueue.put(alarmInfo); + } else { + //当前的故障还没有回复,不需要重复上报机器高中,只是增加机器告警的次数 + return prevSubmittedCounter.incrementAndGet(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + return 0; + } + + + @Override + protected void doRun() throws InterruptedException { + AlarmInfo alarmInfo; + + alarmInfo = alarmQueue.take(); + System.out.println("告警线程队列中拉取到告警信息:"+alarmInfo); + //机器告警数量-1 + terminationToken.reservations.decrementAndGet(); + //发送机器告警信息到智慧监控系统中 + try { + doSendAlarm(); + System.out.println("机器告警信息上报完成"); + }catch (Exception e){ + e.printStackTrace(); + } + //如果当前是回复警告,则需要清空当前告警的统计信息,重置为空 + if(Objects.equals(alarmInfo.getAlarmType(),AlarmType.RESUME)){ + System.out.println("机器告警:"+alarmInfo +"已恢复,清空告警次数"); + submittedAlarmRegistry.remove(alarmInfo.getUniqueByAlarmType(AlarmType.FAULT)); + submittedAlarmRegistry.remove(alarmInfo.getUniqueByAlarmType(AlarmType.RESUME)); + } + } + + + + private void doSendAlarm() { + try { + System.out.println("假装上报到监控系统"); + Thread.sleep(50); + }catch (InterruptedException e){ + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/AlarmType.java b/src/main/java/com/xgh/test/thread/week03/AlarmType.java new file mode 100644 index 0000000..d262dc7 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/AlarmType.java @@ -0,0 +1,38 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.AlarmType + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public enum AlarmType { + + /** + * 故障 + */ + FAULT(1, "故障"), + + /** + * 恢复告警 + */ + RESUME(2, "刷新"); + + private Integer alarmType; + + private String desc; + + AlarmType(Integer alarmType, String desc) { + this.alarmType = alarmType; + this.desc = desc; + } + + public Integer getAlarmType() { + return alarmType; + } + + public String getDesc() { + return desc; + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/Termination.java b/src/main/java/com/xgh/test/thread/week03/Termination.java new file mode 100644 index 0000000..c677f01 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/Termination.java @@ -0,0 +1,13 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.Termination + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public interface Termination { + + void terminate(); +} diff --git a/src/main/java/com/xgh/test/thread/week03/TerminationToken.java b/src/main/java/com/xgh/test/thread/week03/TerminationToken.java new file mode 100644 index 0000000..564f2fe --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/TerminationToken.java @@ -0,0 +1,62 @@ +package com.xgh.test.thread.week03; + +import java.lang.ref.WeakReference; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * com.xgh.test.thread.week03.TerminationToken + * + * @author xgh
+ * @description 线程停止标识 + * @date 2021年08月02日 + */ +public class TerminationToken { + + + // 是否停止标识 + protected volatile boolean toShutdown = false; + + //未执行任务的数量 + public final AtomicInteger reservations = new AtomicInteger(0); + + //当多个线程共享一个terminationToken实例时,通过队列来记录所有的停止线程,从而减少锁的方式实现 + private final Queue> coordinatedThreads; + + public TerminationToken() { + this.coordinatedThreads = new ConcurrentLinkedDeque<>(); + } + + //是否终止 + public boolean isToShutdown(){ + return toShutdown; + } + + public void setToShutdown(boolean toShutdown){ + this.toShutdown = toShutdown; + } + //注册一个线程到terminationToken上 + public void register(Termination thread){ + coordinatedThreads.add(new WeakReference<>(thread)); + } + + //通知terminationThread中所有实例,有一个线程停止了,通知其他线程停止 + public void notifyThreadTermination(Termination thread){ + WeakReference wrThread; + Termination otherTermination; + while ((wrThread = coordinatedThreads.poll()) != null){ + otherTermination = wrThread.get(); + if(otherTermination != null && otherTermination != thread){ + otherTermination.terminate(); + } + } + + + } + + + + + +} diff --git a/src/main/java/com/xgh/test/thread/week03/ThreadExecutor.java b/src/main/java/com/xgh/test/thread/week03/ThreadExecutor.java new file mode 100644 index 0000000..df76004 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/ThreadExecutor.java @@ -0,0 +1,74 @@ +package com.xgh.test.thread.week03; + +/** + * com.xgh.test.thread.week03.ThreadExecutor + * + * @author xgh
+ * @description 两阶段终止模式 + * 1.发出信号,告知正在运行的线程将被终止 + * 2.接收到此信号的线程,做完善后工作,停止运行 + * @date 2021年07月28日 + */ +public class ThreadExecutor { + + /** + * 执行线程 + */ + private Thread executeThread; + + /** + * 运行状态 + */ + private volatile boolean isRunning = false; + + /** + * task 为发生阻塞的线程 + */ + public void execute(Runnable task) { + executeThread = new Thread(() -> { + Thread childThread = new Thread(task); + //子线程设置为守护线程 + childThread.setDaemon(true); + childThread.start(); + + try { + childThread.join(); + isRunning = true; + } catch (InterruptedException e) { + System.out.println("是我打了异常"); + e.printStackTrace(); + } + + }); + executeThread.start(); + } + + + public void shutdown(long mills) { + long currentTime = System.currentTimeMillis(); + while (!isRunning) { + if (System.currentTimeMillis() - currentTime >= mills) { + System.out.println("任务超时,需要结束他"); + executeThread.interrupt(); + break; + } + } + + } + + public static void main(String[] args) { + ThreadExecutor executor = new ThreadExecutor(); + long start = System.currentTimeMillis(); + executor.execute(()->{ + try { + Thread.sleep(5000); + }catch (InterruptedException e){ + e.printStackTrace(); + } + }); + executor.shutdown(1000); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + +} diff --git a/src/main/java/com/xgh/test/thread/week03/mytest/MyServer.java b/src/main/java/com/xgh/test/thread/week03/mytest/MyServer.java new file mode 100644 index 0000000..e9d5523 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/mytest/MyServer.java @@ -0,0 +1,86 @@ +package com.xgh.test.thread.week03.mytest; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +/** + * com.xgh.test.thread.week03.mytest.MyServer + * + * @author xgh
+ * @description + * @date 2021年08月03日 + */ +public class MyServer { + + private final BlockingQueue queue = new ArrayBlockingQueue<>(100); + + private final C c = new C(); + private final P p = new P(); + + public static void main(String[] args) throws InterruptedException { + MyServer myServer = new MyServer(); + myServer.init(); + Thread.sleep(10); + myServer.shutdown(); + + } + + // 停止生产者和消费者的执行 + public void shutdown() { + p.terminable(true); // 先停止生产者,只有在生产者完全停止之后才会停止消费者 + c.terminable(); // 停止消费者 + } + + // 启动生产者和消费者 + public void init() { + p.start(); + c.start(); + } + + + class C extends TerminableSupport { + + @Override + protected void doClean(Exception ex) { + + } + + @Override + protected void doRun() throws Exception { + String poll = queue.poll(); + System.out.println("消费"+poll); + try { + Thread.sleep(10); + }finally { + terminableToken.taskCount.decrementAndGet(); + } + + + } + + @Override + protected void doTerminable() { + + } + } + + class P extends TerminableSupport { + + @Override + protected void doClean(Exception ex) { + + } + + @Override + protected void doRun() throws Exception { + int i = 0; + queue.put(++i +""); + terminableToken.taskCount.incrementAndGet(); + } + + @Override + protected void doTerminable() { + + } + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/mytest/Terminable.java b/src/main/java/com/xgh/test/thread/week03/mytest/Terminable.java new file mode 100644 index 0000000..1e19e86 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/mytest/Terminable.java @@ -0,0 +1,13 @@ +package com.xgh.test.thread.week03.mytest; + +/** + * com.xgh.test.thread.week03.mytest.Terminable + * + * @author xgh
+ * @description 中断线程开关 + * @date 2021年08月03日 + */ +public interface Terminable { + + void terminable(); +} diff --git a/src/main/java/com/xgh/test/thread/week03/mytest/TerminableSupport.java b/src/main/java/com/xgh/test/thread/week03/mytest/TerminableSupport.java new file mode 100644 index 0000000..793134a --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/mytest/TerminableSupport.java @@ -0,0 +1,81 @@ +package com.xgh.test.thread.week03.mytest; + +import java.util.concurrent.ExecutorService; + +/** + * com.xgh.test.thread.week03.mytest.TerminableSupport + * + * @author xgh
+ * @description 控制中断主要逻辑,具体实现留给子类继承 + * @date 2021年08月03日 + */ +public abstract class TerminableSupport extends Thread implements Terminable { + + //标识 + public final TerminableToken terminableToken; + + + public TerminableSupport() { + this(new TerminableToken()); + } + + public TerminableSupport(TerminableToken terminableToken) { + this.terminableToken = terminableToken; + terminableToken.register(this); + } + + + @Override + public void run() { + Exception ex = null; + try { + if(!terminableToken.isShutdown || terminableToken.taskCount.get() > 0){ + doRun(); + } + }catch (Exception e){ + ex = e; + }finally { + try { + doClean(ex); + }finally { + terminableToken.notifyOther(this); + } + } + } + + @Override + public void interrupt() { + terminable(); + } + + protected abstract void doClean(Exception ex); + + protected abstract void doRun()throws Exception; + + @Override + public void terminable() { + terminableToken.isShutdown = true; + try { + doTerminable(); + }finally { + if(terminableToken.taskCount.get() < 0){ + super.interrupt(); + } + } + } + + public void terminable(Boolean is){ + terminable(); + if(is){ + try { + this.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + + protected abstract void doTerminable(); + +} diff --git a/src/main/java/com/xgh/test/thread/week03/mytest/TerminableToken.java b/src/main/java/com/xgh/test/thread/week03/mytest/TerminableToken.java new file mode 100644 index 0000000..f0b5923 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/mytest/TerminableToken.java @@ -0,0 +1,48 @@ +package com.xgh.test.thread.week03.mytest; + +import java.lang.ref.WeakReference; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * com.xgh.test.thread.week03.mytest.TerminableToken + * + * @author xgh
+ * @description 中断标识 + * @date 2021年08月03日 + */ +public class TerminableToken { + + //中断标识 + protected volatile boolean isShutdown = false; + + //任务数 + public AtomicInteger taskCount = new AtomicInteger(0); + + + private final Queue> queue; + + + public TerminableToken() { + this.queue = new ConcurrentLinkedDeque<>(); + } + + + protected void register(Terminable thread){ + queue.add(new WeakReference<>(thread)); + } + + + protected void notifyOther(Terminable thread){ + WeakReference weakReference; + Terminable other; + //如果队列任务不为空,就通知其他线程发送终止信号 + while (null != (weakReference = queue.poll())){ + other = weakReference.get(); + if(null != other && thread != other){ + other.terminable(); + } + } + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/test/SomeService.java b/src/main/java/com/xgh/test/thread/week03/test/SomeService.java new file mode 100644 index 0000000..4c253c5 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/test/SomeService.java @@ -0,0 +1,68 @@ +package com.xgh.test.thread.week03.test; + +import java.util.Random; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.TimeUnit; + +/** + * com.xgh.test.thread.week03.test.SomeService + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public class SomeService { + + private final BlockingQueue queue = new ArrayBlockingQueue<>(100); + + private final Producer producer = new Producer(); + private final Consumer consumer = new Consumer(); + + public static void main(String[] args) throws InterruptedException { + SomeService ss = new SomeService(); + ss.init(); + TimeUnit.SECONDS.sleep(500); + ss.shutdown(); + } + + // 停止生产者和消费者的执行 + public void shutdown() { + producer.terminate(true); // 先停止生产者,只有在生产者完全停止之后才会停止消费者 + consumer.terminate(); // 停止消费者 + } + + // 启动生产者和消费者 + public void init() { + producer.start(); + consumer.start(); + } + + // 生产者 + private class Producer extends TerminatableSupport { + private int i = 0; + + @Override + protected void doRun() throws Exception { + queue.put(String.valueOf(i++)); // 将任务添加到任务队列中 + consumer.terminationToken.reservations.incrementAndGet(); // 更新需要执行的任务数量 + System.out.println("待消费个数"+ consumer.terminationToken.reservations.get()); + } + } + + // 消费者 + private class Consumer extends TerminatableSupport { + @Override + protected void doRun() throws Exception { + String product = queue.take(); // 获取任务 + System.out.println("Processing product: " + product); + try { + TimeUnit.SECONDS.sleep(10); // 模拟消费者对任务的执行 + } catch (InterruptedException e) { + // ignore + } finally { + terminationToken.reservations.decrementAndGet(); // 更新需要执行的任务数量 + } + } + } +} diff --git a/src/main/java/com/xgh/test/thread/week03/test/Terminatable.java b/src/main/java/com/xgh/test/thread/week03/test/Terminatable.java new file mode 100644 index 0000000..662ff6f --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/test/Terminatable.java @@ -0,0 +1,13 @@ +package com.xgh.test.thread.week03.test; + +/** + * com.xgh.test.thread.week03.test.Terminatable + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public interface Terminatable { + + void terminate(); +} diff --git a/src/main/java/com/xgh/test/thread/week03/test/TerminatableSupport.java b/src/main/java/com/xgh/test/thread/week03/test/TerminatableSupport.java new file mode 100644 index 0000000..cd7a3e6 --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/test/TerminatableSupport.java @@ -0,0 +1,78 @@ +package com.xgh.test.thread.week03.test; + +/** + * com.xgh.test.thread.week03.test.TerminatableSupport + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public abstract class TerminatableSupport extends Thread implements Terminatable { + public final TerminationToken terminationToken; // 记录当前的标志位 + + public TerminatableSupport() { + this(new TerminationToken()); // 初始化当前标志位 + } + + public TerminatableSupport(TerminationToken terminationToken) { + super(); + this.terminationToken = terminationToken; // 初始化标志位 + terminationToken.register(this); // 注册当前对象的标志位 + } + + protected abstract void doRun() throws Exception; // 供子类实现具体任务的方法 + + // 钩子方法,用于子类进行一些清理工作 + protected void doCleanup(Exception cause) {} + + // 钩子方法,用于子类进行终止时的一些定制化操作 + protected void doTerminate() {} + + @Override + public void run() { + Exception ex = null; + try { + // 在当前线程中执行任务时,会判断是否标识为终止,并且剩余任务数小于等于0,是才会真正终止当前线程 + while (!terminationToken.isToShutdown() || terminationToken.reservations.get() > 0) { + doRun(); + } + } catch (Exception e) { + ex = e; + } finally { + try { + doCleanup(ex); // 当前线程终止后需要执行的操作 + } finally { + terminationToken.notifyThreadTermination(this); + } + } + } + + @Override + public void interrupt() { + terminate(); + } + + @Override + public void terminate() { + terminationToken.setToShutdown(true); // 设置终止状态 + try { + doTerminate(); // 执行客户端定制的终止操作 + } finally { + if (terminationToken.reservations.get() <= 0) { + super.interrupt(); // 如果当前线程处于终止状态,则强制终止当前线程 + } + } + } + + // 提供给客户端调用的,即客户端线程必须等待终止完成之后才会继续往下执行 + public void terminate(boolean waitUntilThreadTerminated) { + terminate(); + if (waitUntilThreadTerminated) { + try { + this.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/xgh/test/thread/week03/test/TerminationToken.java b/src/main/java/com/xgh/test/thread/week03/test/TerminationToken.java new file mode 100644 index 0000000..bd75c1a --- /dev/null +++ b/src/main/java/com/xgh/test/thread/week03/test/TerminationToken.java @@ -0,0 +1,53 @@ +package com.xgh.test.thread.week03.test; + +import java.lang.ref.WeakReference; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * com.xgh.test.thread.week03.test.TerminationToken + * + * @author xgh
+ * @description + * @date 2021年08月02日 + */ +public class TerminationToken { + protected volatile boolean toShutdown = false; // 终止状态的标志位 + public final AtomicInteger reservations = new AtomicInteger(0); // 记录当前剩余任务数 + + // 记录了所有注册了TerminationToken的实例,这里使用Queue是因为可能会有多个 + // Terminatable实例共享同一个TeraminationToken,如果是共享的,那么reservations + // 实例就保存了所有共享当前TerminationToken实例的线程所需要执行的任务总数 + private final Queue> coordinatedThreads; + + public TerminationToken() { + coordinatedThreads = new ConcurrentLinkedQueue<>(); + } + + public boolean isToShutdown() { + return toShutdown; + } + + public void setToShutdown(boolean toShutdown) { + this.toShutdown = toShutdown; + } + + // 将当前Terminatable实例注册到当前TerminationToken中 + protected void register(Terminatable thread) { + coordinatedThreads.add(new WeakReference<>(thread)); + } + + // 如果是多个Terminatable实例注册到当前TerminationToken中, + // 则广播当前的终止状态,使得这些实例都会终止 + protected void notifyThreadTermination(Terminatable thread) { + WeakReference wrThread; + Terminatable otherThread; + while (null != (wrThread = coordinatedThreads.poll())) { + otherThread = wrThread.get(); + if (null != otherThread && otherThread != thread) { + otherThread.terminate(); + } + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/test/java/com/xgh/test/TestApplicationTests.java b/src/test/java/com/xgh/test/TestApplicationTests.java new file mode 100644 index 0000000..1eccb4b --- /dev/null +++ b/src/test/java/com/xgh/test/TestApplicationTests.java @@ -0,0 +1,13 @@ +package com.xgh.test; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TestApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/src/test/java/com/xgh/test/desigin/DesiginTest.java b/src/test/java/com/xgh/test/desigin/DesiginTest.java new file mode 100644 index 0000000..d4496b8 --- /dev/null +++ b/src/test/java/com/xgh/test/desigin/DesiginTest.java @@ -0,0 +1,34 @@ +package com.xgh.test.desigin; + +import com.xgh.test.design.STATUS; +import com.xgh.test.design.Transaction; +import com.xgh.test.design.WalletRpcServiceOne; +import org.junit.jupiter.api.Test; + +import javax.transaction.InvalidTransactionException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * com.xgh.test.desigin.DesiginTest + * + * @author xgh
+ * @description + * @date 2021年07月08日 + */ +public class DesiginTest { + + @Test + public void testExecute() throws InvalidTransactionException { + Long buyerId = 123L; + Long sellerId = 234L; + Long productId = 345L; + Long orderId = 456L; + Transaction transaction = new Transaction(null, buyerId, sellerId, productId, orderId); + transaction.setWalletRpcService(new WalletRpcServiceOne()); + boolean executedResult = transaction.execute(); + assertTrue(executedResult); + assertEquals(STATUS.EXECUTED, transaction.getStatus()); + } +} diff --git a/src/test/java/com/xgh/test/spring/ApiTests.java b/src/test/java/com/xgh/test/spring/ApiTests.java new file mode 100644 index 0000000..8eecf2e --- /dev/null +++ b/src/test/java/com/xgh/test/spring/ApiTests.java @@ -0,0 +1,83 @@ +package com.xgh.test.spring; + +import com.xgh.test.spring.service.UserService; +import com.xgh.test.spring.step02.BeanFactory; +import com.xgh.test.spring.step02.config.BeanDefinition; +import com.xgh.test.spring.step02.support.DefaultListableBeanFactory; +import lombok.SneakyThrows; +import org.junit.Test; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.NoOp; + +import java.lang.reflect.Constructor; + +/** + * com.xgh.test.spring.ApiTests + * + * @author xgh
+ * @description + * @date 2021年08月12日 + */ +public class ApiTests { + + @Test + public void test_factory() { + //初始化 beanFactory + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + //注册bean + BeanDefinition beanDefinition = new BeanDefinition(UserService.class); + beanFactory.registerBeanDefinition("userService",beanDefinition); + //第一次获取 + UserService userService = (UserService)beanFactory.getBean("userService","张三"); + userService.getUserInfo(); + + //第二次 + UserService userService1 = (UserService)beanFactory.getBean("userService"); + userService1.getUserInfo(); + } + + @SneakyThrows + @Test + public void test_newInstant(){ + Class userServiceClass = UserService.class; + UserService userService = userServiceClass.newInstance(); + System.out.println(userService); + } + + @Test + public void test_constructor() throws Exception { + Class userServiceClass = UserService.class; + Constructor declaredConstructor = userServiceClass.getDeclaredConstructor(String.class); + UserService userService = declaredConstructor.newInstance("张三"); + System.out.println(userService); + } + + @Test + public void test_constructors() throws Exception { + Class userServiceClass = UserService.class; + Constructor[] declaredConstructors = userServiceClass.getDeclaredConstructors(); + Constructor constructor = null; + for (Constructor declaredConstructor : declaredConstructors) { + if(declaredConstructor.getParameterTypes().length == 1){ + constructor = declaredConstructor; + } + } + Constructor declaredConstructor = userServiceClass.getDeclaredConstructor(constructor.getParameterTypes()); + UserService userService = declaredConstructor.newInstance("李四"); + System.out.println(userService); + } + + @Test + public void test_cglib(){ + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(UserService.class); + enhancer.setCallback(new NoOp() { + @Override + public int hashCode() { + return super.hashCode(); + } + }); + Object o = enhancer.createClass(); + System.out.println(o); + } +} diff --git a/src/test/java/com/xgh/test/spring/service/UserService.java b/src/test/java/com/xgh/test/spring/service/UserService.java new file mode 100644 index 0000000..b734ca9 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/service/UserService.java @@ -0,0 +1,33 @@ +package com.xgh.test.spring.service; + +/** + * com.xgh.test.spring.service.UserService + * + * @author xgh
+ * @description + * @date 2021年08月12日 + */ +public class UserService { + private String name; + + public UserService(String name) { + this.name = name; + } + + public UserService() { + } + + public void getUserInfo(){ + System.out.println("获取用户信息"+name); + } + + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(""); + sb.append("=====").append(name); + return sb.toString(); + } + + +} diff --git a/src/test/java/com/xgh/test/spring/test03/Test03.java b/src/test/java/com/xgh/test/spring/test03/Test03.java new file mode 100644 index 0000000..08170ce --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test03/Test03.java @@ -0,0 +1,42 @@ +package com.xgh.test.spring.test03; + +import com.xgh.test.spring.step03.PropertyValue; +import com.xgh.test.spring.step03.PropertyValues; +import com.xgh.test.spring.step03.config.BeanDefinition; +import com.xgh.test.spring.step03.config.BeanReference; +import com.xgh.test.spring.step03.support.DefaultListableBeanFactory; +import org.junit.Test; + +/** + * com.xgh.test.spring.test03.Test03 + * + * @author xgh
+ * @description + * @date 2021年08月24日 + */ +public class Test03 { + + + @Test + public void test_BeanFactory(){ + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + + //注册userDao + beanFactory.registerBeanDefinition("userDao",new BeanDefinition(UserDao.class)); + + //userService + PropertyValues propertyValues = new PropertyValues(); + + propertyValues.addPropertyValue(new PropertyValue("uId","1001")); + propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao"))); + + + //userService注入 + BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues); + beanFactory.registerBeanDefinition("userService",beanDefinition); + UserService userService = (UserService)beanFactory.getBean("userService"); + userService.queryUserInfo(); + } + + +} diff --git a/src/test/java/com/xgh/test/spring/test03/UserDao.java b/src/test/java/com/xgh/test/spring/test03/UserDao.java new file mode 100644 index 0000000..f9b6c38 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test03/UserDao.java @@ -0,0 +1,29 @@ +package com.xgh.test.spring.test03; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.test03.UserDao + * + * @author xgh
+ * @description + * @date 2021年08月24日 + */ +public class UserDao { + + + private static Map hashMap = new HashMap<>(); + + + static { + hashMap.put("1001","张三"); + hashMap.put("1002","李四"); + hashMap.put("1003","王五"); + } + + + public String queryUserName(String uId){ + return hashMap.get(uId); + } +} diff --git a/src/test/java/com/xgh/test/spring/test03/UserService.java b/src/test/java/com/xgh/test/spring/test03/UserService.java new file mode 100644 index 0000000..35798d8 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test03/UserService.java @@ -0,0 +1,38 @@ +package com.xgh.test.spring.test03; + +/** + * com.xgh.test.spring.test03.UserService + * + * @author xgh
+ * @description + * @date 2021年08月24日 + */ +public class UserService { + + private String uId; + + private UserDao userDao; + + public UserService() { + } + + public void queryUserInfo(){ + System.out.println("查询用户信息:" + userDao.queryUserName(uId)); + } + + public String getuId() { + return uId; + } + + public void setuId(String uId) { + this.uId = uId; + } + + public UserDao getUserDao() { + return userDao; + } + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } +} diff --git a/src/test/java/com/xgh/test/spring/test04/Test04.java b/src/test/java/com/xgh/test/spring/test04/Test04.java new file mode 100644 index 0000000..5554c1c --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test04/Test04.java @@ -0,0 +1,55 @@ +package com.xgh.test.spring.test04; + +import cn.hutool.core.io.IoUtil; +import com.xgh.test.spring.step04.beans.factory.support.DefaultListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.xml.XmlBeanDefinitionReader; +import com.xgh.test.spring.step04.core.io.DefaultResourceLoader; +import com.xgh.test.spring.step04.core.io.Resource; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.io.InputStream; + +/** + * com.xgh.test.spring.test04.Test04 + * + * @author xgh
+ * @description + * @date 2021年08月27日 + */ +public class Test04 { + + private DefaultResourceLoader resourceLoader; + + @Before + public void init(){ + resourceLoader = new DefaultResourceLoader(); + } + + @Test + public void test_classpath() throws IOException{ + Resource resource = resourceLoader.getResource("classpath:important.properties"); + InputStream inputStream = resource.getInputStream(); + String content = IoUtil.readUtf8(inputStream); + System.out.println(content); + } + + @Test + public void test_file()throws IOException{ + Resource resource = resourceLoader.getResource("src/test/resources/important.properties"); + InputStream inputStream = resource.getInputStream(); + String content = IoUtil.readUtf8(inputStream); + System.out.println(content); + } + + @Test + public void test_xml(){ + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); + reader.loadBeanDefinitions("classpath:spring.xml"); + UserService userService = beanFactory.getBean("userService", UserService.class); + System.out.println(userService.queryUserInfo()); + } + +} diff --git a/src/test/java/com/xgh/test/spring/test04/UserDao.java b/src/test/java/com/xgh/test/spring/test04/UserDao.java new file mode 100644 index 0000000..e513f95 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test04/UserDao.java @@ -0,0 +1,29 @@ +package com.xgh.test.spring.test04; + +import java.util.HashMap; +import java.util.Map; + +/** + * com.xgh.test.spring.test03.UserDao + * + * @author xgh
+ * @description + * @date 2021年08月24日 + */ +public class UserDao { + + + private static Map hashMap = new HashMap<>(); + + + static { + hashMap.put("10001","张三"); + hashMap.put("10002","李四"); + hashMap.put("10003","王五"); + } + + + public String queryUserName(String uId){ + return hashMap.get(uId); + } +} diff --git a/src/test/java/com/xgh/test/spring/test04/UserService.java b/src/test/java/com/xgh/test/spring/test04/UserService.java new file mode 100644 index 0000000..e74a939 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test04/UserService.java @@ -0,0 +1,36 @@ +package com.xgh.test.spring.test04; + + +/** + * com.xgh.test.spring.test03.UserService + * + * @author xgh
+ * @description + * @date 2021年08月24日 + */ +public class UserService { + + private String uId; + + private UserDao userDao; + + public String queryUserInfo() { + return userDao.queryUserName(uId); + } + + public String getuId() { + return uId; + } + + public void setuId(String uId) { + this.uId = uId; + } + + public UserDao getUserDao() { + return userDao; + } + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } +} diff --git a/src/test/java/com/xgh/test/spring/test05/MyBeanFactoryPostProcessor.java b/src/test/java/com/xgh/test/spring/test05/MyBeanFactoryPostProcessor.java new file mode 100644 index 0000000..d8d69b0 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test05/MyBeanFactoryPostProcessor.java @@ -0,0 +1,20 @@ +package com.xgh.test.spring.test05; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.PropertyValue; +import com.xgh.test.spring.step04.beans.PropertyValues; +import com.xgh.test.spring.step04.beans.factory.ConfigurableListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.config.BeanDefinition; +import com.xgh.test.spring.step04.beans.factory.config.BeanFactoryPostProcessor; + +public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) + throws BeansException { + BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService" + ); + PropertyValues propertyValues = beanDefinition.getPropertyValues(); + propertyValues.addPropertyValue(new PropertyValue("company", "改为:字节跳动 ")); + } + +} diff --git a/src/test/java/com/xgh/test/spring/test05/MyBeanPostProcessor.java b/src/test/java/com/xgh/test/spring/test05/MyBeanPostProcessor.java new file mode 100644 index 0000000..becf977 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test05/MyBeanPostProcessor.java @@ -0,0 +1,20 @@ +package com.xgh.test.spring.test05; + +import com.xgh.test.spring.step04.beans.BeansException; +import com.xgh.test.spring.step04.beans.factory.config.BeanPostProcessor; + +public class MyBeanPostProcessor implements BeanPostProcessor { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + if("userService".equals(beanName)){ + UserService userService = (UserService) bean; + userService.setLocation("改为北京"); + } + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + return bean; + } +} diff --git a/src/test/java/com/xgh/test/spring/test05/Test05.java b/src/test/java/com/xgh/test/spring/test05/Test05.java new file mode 100644 index 0000000..bd3d9eb --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test05/Test05.java @@ -0,0 +1,46 @@ +package com.xgh.test.spring.test05; + +import com.xgh.test.spring.step04.beans.factory.support.DefaultListableBeanFactory; +import com.xgh.test.spring.step04.beans.factory.xml.XmlBeanDefinitionReader; +import com.xgh.test.spring.step04.context.support.ClassPathXmlApplicationContext; +import org.junit.Test; + +public class Test05 { + + + @Test + public void test_xml() { + // 1.初始化 BeanFactory + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml"); + applicationContext.registerShutdownHook(); + + // 2. 获取Bean对象调用方法 + UserService userService = applicationContext.getBean("userService", UserService.class); + String result = userService.queryUserInfo(); + System.out.println("测试结果:" + result); + } + + @Test + public void test_hook() { + Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("close!"))); + } + + @Test + public void test_BeanFactoryPostProcessorAndBeanPostProcessor() { +// 1.初始化 BeanFactory + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); +// 2. 读取配置文件&注册 Bean + XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); + reader.loadBeanDefinitions("classpath:spring.xml"); +// 3. BeanDefinition 加载完成 & Bean 实例化之前,修改 BeanDefinition 的属性值 + MyBeanFactoryPostProcessor beanFactoryPostProcessor = new MyBeanFactoryPostProcessor(); + beanFactoryPostProcessor.postProcessBeanFactory(beanFactory); +// 4. Bean 实例化之后,修改 Bean 属性信息 + MyBeanPostProcessor beanPostProcessor = new MyBeanPostProcessor(); + beanFactory.addBeanPostProcessor(beanPostProcessor); +// 5. 获取 Bean 对象调用方法 + UserService userService = beanFactory.getBean("userService", UserService.class); + String result = userService.queryUserInfo(); + System.out.println("测试结果: " + result); + } +} diff --git a/src/test/java/com/xgh/test/spring/test05/UserDao.java b/src/test/java/com/xgh/test/spring/test05/UserDao.java new file mode 100644 index 0000000..24c0967 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test05/UserDao.java @@ -0,0 +1,26 @@ +package com.xgh.test.spring.test05; + +import java.util.HashMap; +import java.util.Map; + +public class UserDao { + + private static Map hashMap = new HashMap<>(); + + public void initDataMethod(){ + System.out.println("执行:init-method"); + hashMap.put("10001", "小傅哥"); + hashMap.put("10002", "八杯水"); + hashMap.put("10003", "阿毛"); + } + + public void destroyDataMethod(){ + System.out.println("执行:destroy-method"); + hashMap.clear(); + } + + public String queryUserName(String uId) { + return hashMap.get(uId); + } + +} diff --git a/src/test/java/com/xgh/test/spring/test05/UserService.java b/src/test/java/com/xgh/test/spring/test05/UserService.java new file mode 100644 index 0000000..2934a78 --- /dev/null +++ b/src/test/java/com/xgh/test/spring/test05/UserService.java @@ -0,0 +1,64 @@ +package com.xgh.test.spring.test05; + + +import com.xgh.test.spring.step04.beans.factory.DisposableBean; +import com.xgh.test.spring.step04.beans.factory.InitializingBean; + +/** + * + */ +public class UserService implements InitializingBean, DisposableBean { + + private String uId; + private String company; + private String location; + private UserDao userDao; + + @Override + public void destroy() throws Exception { + System.out.println("执行:UserService.destroy"); + } + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println("执行:UserService.afterPropertiesSet"); + } + + public String queryUserInfo() { + return userDao.queryUserName(uId) + "," + company + "," + location; + } + + public String getuId() { + return uId; + } + + public void setuId(String uId) { + this.uId = uId; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public UserDao getUserDao() { + return userDao; + } + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } + + +} diff --git a/src/test/resources/Spring.xml b/src/test/resources/Spring.xml new file mode 100644 index 0000000..203ea83 --- /dev/null +++ b/src/test/resources/Spring.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/important.properties b/src/test/resources/important.properties new file mode 100644 index 0000000..9a02442 --- /dev/null +++ b/src/test/resources/important.properties @@ -0,0 +1 @@ +system.key=OLpj9823dz \ No newline at end of file diff --git a/test/11.txt b/test/11.txt deleted file mode 100644 index 7153628..0000000 --- a/test/11.txt +++ /dev/null @@ -1 +0,0 @@ -1111111 \ No newline at end of file