honwhy / note Goto Github PK
View Code? Open in Web Editor NEWnotes about java open sources
License: MIT License
notes about java open sources
License: MIT License
一个开源项目要不要定义自己的异常体系,是继承自Exception
还是RuntimeException
;
要不要在项目中打印日志,使用什么日志框架,使用什么级别;
以commons-pool2
和commons-net
为例,这两者都没有打印日志,前者没有新定义异常,后者继承自IOException
定义了FTPConnectionClosedException
,这也是commons-net
唯一自定义的异常类。
项目 | 版本 | 日志框架 | 自定义异常 |
---|---|---|---|
commons-pool2 | 2.5.0 | 无 | 无 |
commons-net | 3.6 | 无 | 继承IOException |
不同的例子待补充。
预备知识
https://www.jianshu.com/p/80a2db190f42
一些基本概念:
Channel, ByteBuffer, Selector
Netty对JDK的扩展优化,ByteBuf, Promise等
EventLoopGroup
做监控管理使用
比如连接池的状态信息监控
最近阅读Spring源码深度解析一书,囫囵吞枣,走马观花。书中以3.x版本代码示例讲解,我以4.x版本源码参照,大致脉络很少改变,基于Java 8的代码工具类看起来更加舒服一些。
我以XML配置的方式去了解Spring的原理,
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactory.xml"));
MyTestBean bean = (MyTestBean) bf.getBean("myTestBean");
assertEquals("testStr", bean.getTestStr());
简单的三行代码,但是要了解底层原理可能要了解的概念有Bean
, FactoryBean
, BeanDefinition
, 各种不同类型的BeanFactory及ApplicationContext
,及BeanPostProcessor
和BeanFactoryPostProcessor
.
书中第七章开始讲解AOP相关内容了,刚好Spring AOP几乎涵盖了前面提到的术语概念,且关注AspectJ和Transactional,
标签 | example | namespaceHandler | 相关PostProcessor |
---|---|---|---|
aop | <aop:aspectj-autoproxy /> | org.springframework.aop.config.AopNamespaceHandler | AnnotationAwareAspectJAutoProxyCreator |
tx | <tx:annotation-driven transaction-manager="txManager" /> | org.springframework.transaction.config.TxNamespaceHandler | - |
涉及到的知识点,
解析自定义标签会使用到自定义的NamespaceHandler, 从BeanFactory的loadBeanDefinition入口开始到调用自定义的NamespaceHandler去解析<aop:aspectj-autoproxy/>
这些细节暂时跳过,那么aop命名空间对应的NamespaceHandler是AopNamespaceHandler,也是在这个Handler中注册了特殊的parser,
public class AopNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
//...
registerBeanDefinitionParser("aspectj-autoproxy", new AspectJAutoProxyBeanDefinitionParser());
}
}
继续跟踪这个AspectJAutoProxyBeanDefinitionParser
,
class AspectJAutoProxyBeanDefinitionParser implements BeanDefinitionParser {
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
extendBeanDefinition(element, parserContext);
return null;
}
}
关键点在AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary
这个方法调用,而其实这个AopNamespaceUtils
工具类最终是调用另外一个工具类AopConfigUtils
public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(
ParserContext parserContext, Element sourceElement) {
BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(
parserContext.getRegistry(), parserContext.extractSource(sourceElement));
useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
registerComponentIfNecessary(beanDefinition, parserContext);
}
接下来还有几个步骤,简单点说就是这个AopUtils工具类能够注册一个特殊的BeanDefinition,AUTO_PROXY_CREATOR_BEAN_NAME
public abstract class AopConfigUtils {
/**
* The bean name of the internally managed auto-proxy creator.
*/
public static final String AUTO_PROXY_CREATOR_BEAN_NAME =
"org.springframework.aop.config.internalAutoProxyCreator";
}
把它定义成最高优先级,角色是基础架构类型的Bean, 一般时候beanClass是AnnotationAwareAspectJAutoProxyCreator.class
RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
beanDefinition.setSource(source);
beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
观察AnnotationAwareAspectJAutoProxyCreator
类继承情况,就可以发现这是个BeanPostprocessor.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.