Giter Club home page Giter Club logo

note's People

Watchers

 avatar  avatar

note's Issues

自造轮子时应该如何处理异常和日志问题

一个开源项目要不要定义自己的异常体系,是继承自Exception还是RuntimeException
要不要在项目中打印日志,使用什么日志框架,使用什么级别;
commons-pool2commons-net为例,这两者都没有打印日志,前者没有新定义异常,后者继承自IOException定义了FTPConnectionClosedException,这也是commons-net唯一自定义的异常类。

项目 版本 日志框架 自定义异常
commons-pool2 2.5.0
commons-net 3.6 继承IOException

不同的例子待补充。

JMX

做监控管理使用
比如连接池的状态信息监控

Spring AOP

最近阅读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,及BeanPostProcessorBeanFactoryPostProcessor.

书中第七章开始讲解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 -

涉及到的知识点,

  • 自定义标签解析
  • Bean实例过程中的postProcessBeforeInstantiation postProcessAfterInstantiation postProcessBeforeInitialization postAfterInitialization

解析自定义标签会使用到自定义的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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.