Spring:面向切面编程

MagicLee
2017-01-12 看过
当初面试完,问考官,在进入公司之前需要准备些什么。他给我推荐了spring。
在学校的时候,对于公司中使用的技术非常陌生,spring 让我对这些东西有了一点点模糊的认知。
编程的发展,许多时候是体现在对于问题的不断了解,然后不断减少重复劳动,提升效率。比如对于一个企业的工程,我们有哪些共同的功能?怎么避免重复实现这些共同的功能?这就是spring要解决的问题。

spring的解决思路是什么呢?面向切面编程。程序运行本质上还是顺序的,所有的代码编译成了字节码之后,都是有一定顺序的。面向切面就是把一些事务性的功能代码抽出来,在其他地方统一实现,实际的业务开发人员只需要关注业务代码。具体使用到的所谓依赖注入,本质上就是annocation,就是给代码添加注释,然后spring框架会在不同阶段,识别出不同的注释,进行相应的行为。

具体细节可以深入到这段源码里面去。
```
@Override
        public void refresh() throws BeansException, IllegalStateException {
                synchronized (this.startupShutdownMonitor) {
                        // Prepare this context for refreshing.
                        prepareRefresh();

                        // Tell the subclass to refresh the internal bean factory.
                        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

                        // Prepare the bean factory for use in this context.
                        prepareBeanFactory(beanFactory);

                        try {
                                // Allows post-processing of the bean factory in context subclasses.
                                postProcessBeanFactory(beanFactory);

                                // Invoke factory processors registered as beans in the context.
                                invokeBeanFactoryPostProcessors(beanFactory);

                                // Register bean processors that intercept bean creation.
                                registerBeanPostProcessors(beanFactory);

                                // Initialize message source for this context.
                                initMessageSource();

                                // Initialize event multicaster for this context.
                                initApplicationEventMulticaster();

                                // Initialize other special beans in specific context subclasses.
                                onRefresh();

                                // Check for listener beans and register them.
                                registerListeners();

                                // Instantiate all remaining (non-lazy-init) singletons.
                                finishBeanFactoryInitialization(beanFactory);

                                // Last step: publish corresponding event.
                                finishRefresh();
                        }

                        catch (BeansException ex) {
                                logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);

                                // Destroy already created singletons to avoid dangling resources.
                                destroyBeans();

                                // Reset 'active' flag.
                                cancelRefresh(ex);

                                // Propagate exception to caller.
                                throw ex;
                        }
                }
        }
```

另外,想要用好spring,可能还需要一些xml相关知识,因为spring的功能是通过xml配置的。spring的官网喜欢给出各种样例工程,便于快速实践。
对于spring的API可以参考 http://docs.spring.io/spring/docs/5.0.0.M4/javadoc-api//overview-summary.html
0 有用
0 没用

查看更多豆瓣高分好书

评论 0条

添加回应

Spring in Action(中文版)的更多书评

推荐Spring in Action(中文版)的豆列

了解更多图书信息

豆瓣
免费下载 iOS / Android 版客户端